小编CMP*_*rez的帖子

使用NHibernate创建一个SQLite数据库,但只需一次

我一直在关注如何将NHibernate与SQLite一起使用的一些例子,其中大部分都与单元测试数据库CRUD操作有关.所以,到目前为止我用google搜索和跟踪的例子都与此有关.这很好,但问题是,每次运行我的程序时,数据库都会重新创建!我如何修改我的代码,以便如果数据库已经存在,NHibernate不会创建它?是的,我试图检查File.Exists,但它被忽略了; 我相信因为NHibernate首先访问该文件.

这是我的映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="NHibernate.Test">
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">false</property>
  </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我的完整代码:

using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using PruebaNHLite.Domain;

namespace PruebaNHLite
{
    public class Program
    {
        public static ISession sess;
        public static Configuration cfg;
        public static SQLiteConnection connection;
        private const string CONNECTION_STRING = 
                @"Data Source=nhlite.db;Pooling=true;FailIfMissing=false;
                                BinaryGUID=false;New=false;Compress=true;Version=3";

        static void Main(string[] args)
        {
            Init();
            BuildSchema();
            Insert();
            Retrieve();
            sess.Close();
            sess = null;
        } …
Run Code Online (Sandbox Code Playgroud)

c# sqlite nhibernate

9
推荐指数
1
解决办法
1万
查看次数

使用Linq调用是一个模糊的错误

首先,对于过分的通用名称类抱歉.我的雇主是偏执狂,我肯定知道他漫游这个网站.好的,所以我有这个代码:

var fooObj = new MyFooClass()
{
    fooField1 = MyEnum.Value3, 
    fooField2 = false,
    fooField3 = false,
    fooField4 = otherEntity.OneCollection.ElementAt(0) as MyBarClass
}
Run Code Online (Sandbox Code Playgroud)

其他的Entity.OneCollection是一个ISet.ISet是一个实现IEnumerable的NHibernate集合类.如果我尝试编译此代码,我会收到此错误:

Error 2     The call is ambiguous between the following methods or properties: 
'System.Linq.Enumerable.ElementAt<MyFirm.Blah.Blah.ClassyClass>    (System.Collections.Generic.IEnumerable<MyFirm.Blah.Blah.ClassyClass>, int)'
and 
'System.Linq.Enumerable.ElementAt<MyFirm.Blah.Blah.ClassyClass>(System.Collections.Generic.IEnumerable<MyFirm.Blah.Blah.ClassyClass>, int)'    
Run Code Online (Sandbox Code Playgroud)

但是,如果我在类的开头删除使用System.Linq并将代码更改为:

var fooObj = new MyFooClass()
{
    fooField1 = MyEnum.Value3, 
    fooField2 = false,
    fooField3 = false,
    fooField4 = System.Linq.Enumerable
                     .ElementAt(otherEntity.OneCollection, 0) as MyBarClass
}
Run Code Online (Sandbox Code Playgroud)

它编译和工作.(?运算符检查OneCollection是否为了清晰起见而删除了元素)

任何人都可以向我解释这个错误吗?

如果相关:我正在使用Visual Studio 2008,目标是.NET Framework 3.5并使用ReSharper 5.1.

注意 - 编辑以澄清哪个具体的IEnumerable是集合,对不起.

c# linq resharper visual-studio-2008

6
推荐指数
1
解决办法
3191
查看次数

对删除重复引用一无所知

这与这个问题相关:Call is ambigously error using Linq

相同的项目,相同的错误。每次我尝试使用 Linq 中的扩展方法时,都会收到有关不明确引用的错误。例如;

listOfThings.Where(x => x.Foo == 1);
Run Code Online (Sandbox Code Playgroud)

不编译并返回错误,而

System.Linq.Enumerable.Where(listOfThings, x => x.Foo == 1);
Run Code Online (Sandbox Code Playgroud)

运行正常。此外,当我编写 System.Linq 时...我在 Resharper 的 Intellisense 上看到两个对 System.Linq 的引用;第一:

R# 智能感知 1

第二点:

在此输入图像描述

谁能帮我追踪重复的参考文​​献?有什么地方可以看吗?我可以使用什么工具吗?谢谢!

c# linq resharper

5
推荐指数
1
解决办法
2318
查看次数

NHibernate一对多的例子.这是代码味吗?

最近我发现了一个关于NHibernate的一对多关系的例子.映射如下:

<class name="Company" table="Company">
    <id name="Id" type="Int32" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <version name="Version"/>
    <property name="Name"/>
    <set name="Customers" inverse="true" lazy="true" >
      <key column="Id"/>
      <one-to-many class="Customer"/>
    </set> 
</class>

<class name="Customer" table="Customer">
    <id name="Id" type="Int32" unsaved-value="0">
      <generator class="identity"/>
    </id>
    <version name="Version"/>
    <property name="DateOfBirth"/>
    <property name="FirstName"/>
    <property name="Surname"/>
    <many-to-one name="Company" column="CompanyId" cascade="all-delete-orphan"/>
</class>
Run Code Online (Sandbox Code Playgroud)

这是课程:

public class Company
{
    private ISet<Customer> customers = new HashedSet<Customer>();
    public int Id { get; set; } 
    public string Name { get; set; }
    public int …
Run Code Online (Sandbox Code Playgroud)

c# nhibernate nhibernate-mapping

0
推荐指数
1
解决办法
4132
查看次数