Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate按代码映射,无以下内容的持久化器:_C#_Nhibernate - Fatal编程技术网

C# NHibernate按代码映射,无以下内容的持久化器:

C# NHibernate按代码映射,无以下内容的持久化器:,c#,nhibernate,C#,Nhibernate,所以我已经为此挣扎了半天了。。 我将介绍eclipselink和java,对于一个项目,我将用NHibernate和c#编写一些代码。 不幸的是,我想nhibernate上关于map by code的文档有点缺乏。我找不到关于我收到的错误的任何来源 出于测试目的,我正在使用Northwind数据库,因此我创建了一个类Customer public class Customer{ public virtual string CustomerID { get; set; } publ

所以我已经为此挣扎了半天了。。 我将介绍eclipselink和java,对于一个项目,我将用NHibernate和c#编写一些代码。 不幸的是,我想nhibernate上关于map by code的文档有点缺乏。我找不到关于我收到的错误的任何来源

出于测试目的,我正在使用Northwind数据库,因此我创建了一个类Customer

public class Customer{
    public virtual string CustomerID { get; set; }
    public virtual string CompanyName { get; set; }
    public virtual string ContactName { get; set; }
    public virtual string ContactTitle { get; set; }
    public virtual string Address { get; set; }
    public virtual string City { get; set; }
    public virtual string Region { get; set; }
    public virtual string PostalCode { get; set; }
    public virtual string Country { get; set; }
    public virtual string Phone { get; set; }
    public virtual string Fax { get; set; }
}
也是一个映射类:

public class CustomerMap: ClassMapping<Customer>
{
    public CustomerMap() {
        Schema("dbo");
        Lazy(true);
        Id(x => x.CustomerID, map => map.Generator(Generators.Assigned));
        Property(x => x.CompanyName, map => map.NotNullable(true));
        Property(x => x.ContactName);
        Property(x => x.ContactTitle);
        Property(x => x.Address);
        Property(x => x.City);
        Property(x => x.Region);
        Property(x => x.PostalCode);
        Property(x => x.Country);
        Property(x => x.Phone);
        Property(x => x.Fax);
    }
}
公共类CustomerMap:ClassMapping
{
公共客户映射(){
模式(“dbo”);
懒惰(真);
Id(x=>x.CustomerID,map=>map.Generator(Generators.Assigned));
属性(x=>x.CompanyName,map=>map.NotNullable(true));
属性(x=>x.ContactName);
属性(x=>x.ContactTitle);
属性(x=>x.Address);
房地产(x=>x.City);
属性(x=>x.Region);
属性(x=>x.PostalCode);
财产(x=>x.Country);
属性(x=>x.Phone);
属性(x=>x.Fax);
}
}
但是我得到了错误的无持久性

ModelMapper mm = new ModelMapper();
mm.AddMapping<CustomerMap>();
HbmMapping hbmm = mm.CompileMappingForAllExplicitlyAddedEntities();
XmlSerializer xml = new XmlSerializer(hbmm.GetType());
xml.Serialize(Console.Out, hbmm);
ModelMapper mm=newmodelmapper();
mm.AddMapping();
HbmMapping hbmm=mm.CompileMappingForAllExplicitlyAddedEntities();
XmlSerializer xml=新的XmlSerializer(hbmm.GetType());
序列化(Console.Out,hbmm);
这是上面代码生成的xml

<?xml version="1.0" encoding="Windows-1252"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="NHibernate.Classes.Domain" assembly="NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="urn:nhibernate-mapping-2.2">
      <class name="Customer" schema="dbo">
        <id name="CustomerID" type="String">
          <generator class="assigned" />
        </id>
        <property name="CompanyName" not-null="true" />
        <property name="ContactName" />
        <property name="ContactTitle" />
        <property name="Address" />
        <property name="City" />
        <property name="Region" />
        <property name="PostalCode" />
        <property name="Country" />
        <property name="Phone" />
        <property name="Fax" />
      </class>
    </hibernate-mapping>


由于我正在使用代码映射的东西,我不想自己创建这个xml,所以我不知道我可能做错了什么。我已经在谷歌搜索过了。

这是我对实体地图的要求

_mapper.AddMappings(MappingAssembly.GetExportedTypes());
           Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain");
其中_mapper是ModelMapper。我还显式地添加了一个配置设置来告诉应用程序我的映射在哪里。因此,我加载程序集,然后调用GetExportedTypes()

这是我的整个NhibernateInitializer课程

public abstract class NHibernateInitializer : IDomainMapper
{
    protected Configuration Configure;
    private ISessionFactory _sessionFactory;
    private readonly ModelMapper _mapper = new ModelMapper();
    private Assembly _mappingAssembly;
    private readonly String _mappingAssemblyName;
    private readonly String _connectionString;

    protected NHibernateInitializer(String connectionString, String mappingAssemblyName)
    {
        if (String.IsNullOrWhiteSpace(connectionString))
            throw new ArgumentNullException("connectionString", "connectionString is empty.");

        if (String.IsNullOrWhiteSpace(mappingAssemblyName))
            throw new ArgumentNullException("mappingAssemblyName", "mappingAssemblyName is empty.");

        _mappingAssemblyName = mappingAssemblyName;
        _connectionString = connectionString;
    }

    public ISessionFactory SessionFactory
    {
        get
        {
            return _sessionFactory ?? (_sessionFactory = Configure.BuildSessionFactory());
        }
    }

    private Assembly MappingAssembly
    {
        get
        {
            return _mappingAssembly ?? (_mappingAssembly = Assembly.Load(_mappingAssemblyName));
        }
    }

    public void Initialize()
    {
        Configure = new Configuration();
        Configure.EventListeners.PreInsertEventListeners = new IPreInsertEventListener[] { new EventListener() };
        Configure.EventListeners.PreUpdateEventListeners = new IPreUpdateEventListener[] { new EventListener() };
        Configure.SessionFactoryName(System.Configuration.ConfigurationManager.AppSettings["SessionFactoryName"]);
        Configure.DataBaseIntegration(db =>
                                      {
                                          db.Dialect<MsSql2008Dialect>();
                                          db.Driver<SqlClientDriver>();
                                          db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                                          db.IsolationLevel = IsolationLevel.ReadCommitted;
                                          db.ConnectionString = _connectionString;
                                          db.BatchSize = 20;
                                          db.Timeout = 10;
                                          db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
                                      });
        Configure.SessionFactory().GenerateStatistics();

        Map();
    }

    private void Map()
    {
        _mapper.AddMappings(MappingAssembly.GetExportedTypes());
        Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain");
    }

    public HbmMapping HbmMapping
    {
        get { return _mapper.CompileMappingFor(MappingAssembly.GetExportedTypes()); }
    }

    public IList<HbmMapping> HbmMappings
    {
        get { return _mapper.CompileMappingForEach(MappingAssembly.GetExportedTypes()).ToList(); }
    }

    /// <summary>
    /// Gets the domain entities.
    /// </summary>
    /// <returns></returns>
    /// <remarks>by default anything that derives from EntityBase and isn't abstract or generic</remarks>
    protected virtual IEnumerable<System.Type> GetDomainEntities()
    {
        List<System.Type> domainEntities = (from t in MappingAssembly.GetExportedTypes()
                                            where typeof(EntityBase<Guid>).IsAssignableFrom(t)
                                            && (!t.IsGenericType || !t.IsAbstract)
                                            select t
                                           ).ToList();

        return domainEntities;
    }
}
公共抽象类NHibernateInitializer:IDomainMapper
{
保护配置;
私人ISessionFactory(sessionFactory);;
私有只读模型映射器_mapper=new ModelMapper();
专用汇编(mappingAssembly);
私有只读字符串_mappingAssemblyName;
私有只读字符串_connectionString;
受保护的NHibernateInitializer(字符串连接字符串、字符串映射程序集名称)
{
if(String.IsNullOrWhiteSpace(connectionString))
抛出新ArgumentNullException(“connectionString”,“connectionString为空”);
if(String.IsNullOrWhiteSpace(mappingAssemblyName))
抛出新ArgumentNullException(“mappingAssemblyName”,“mappingAssemblyName为空”);
_mappingAssemblyName=mappingAssemblyName;
_connectionString=connectionString;
}
公共会话工厂会话工厂
{
得到
{
返回_sessionFactory??(_sessionFactory=Configure.BuildSessionFactory());
}
}
私有程序集映射程序集
{
得到
{
返回_mappingAssembly??(_mappingAssembly=Assembly.Load(_mappingAssemblyName));
}
}
公共无效初始化()
{
Configure=新配置();
Configure.EventListeners.PreInsertEventListeners=new-IPreInsertEventListener[]{new-EventListener()};
Configure.EventListeners.PreUpdateEventListeners=new-IPreUpdateEventListener[]{new-EventListener()};
Configure.SessionFactoryName(System.Configuration.ConfigurationManager.AppSettings[“SessionFactoryName”]);
Configure.DataBaseIntegration(db=>
{
db.方言();
db.Driver();
db.KeywordsAutoImport=Hbm2DDLKeyWords.AutoQuote;
db.IsolationLevel=IsolationLevel.ReadCommitted;
db.ConnectionString=\u ConnectionString;
db.BatchSize=20;
db.超时=10;
db.hqltosqlssubstitutions=“true 1,false 0,yes'Y',no'N';
});
Configure.SessionFactory().GenerateStatistics();
Map();
}
私有void映射()
{
_AddMappings(MappingAssembly.GetExportedTypes());
Configure.AddDeserializedMapping(_mapper.compileMappingForAllExplicitlyAddIdentity(),“MyWholeDomain”);
}
公共HbmMapping HbmMapping
{
获取{return}mapper.CompileMappingFor(MappingAssembly.GetExportedTypes());}
}
公共IList HbmMappings
{
获取{return}mapper.CompileMappingForEach(MappingAssembly.GetExportedTypes()).ToList();}
}
/// 
///获取域实体。
/// 
/// 
///默认情况下,派生自EntityBase且不是抽象或泛型的任何内容
受保护的虚拟IEnumerable GetDomainEntities()
{
列出域实体=(来自MappingAssembly.GetExportedTypes()中的t)
其中typeof(EntityBase).IsAssignableFrom(t)
&&(!t.IsGenericType | |!t.IsAbstract)
选择t
).ToList();
返回域实体;
}
}