什么是Fluent NHibernate中的schemaExport?
我很想知道更多关于这段代码以及执行时的预期。
/// /// Sets up NHibernate, and adds an ISessionFactory to the given /// container. /// private void ConfigureNHibernate(IKernel container) { // Build the NHibernate ISessionFactory object var sessionFactory = FluentNHibernate .Cfg.Fluently.Configure() .Database( MsSqlConfiguration.MsSql2008.ConnectionString( c => c.FromConnectionStringWithKey("ServicesDb"))) .CurrentSessionContext("web") .Mappings(m => m.FluentMappings.AddFromAssemblyOf()) //.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true)) .ExposeConfiguration(cfg => { var schemaExport = new SchemaExport(cfg); schemaExport.Drop(true, true); schemaExport.Create(true, true); }) .BuildSessionFactory(); // Add the ISessionFactory instance to the container container.Bind().ToConstant(sessionFactory); // Configure a resolver method to be used for creating ISession objects container.Bind().ToMethod(CreateSession); container.Bind().To(); }
现在我知道它的大部分内容,但我更想知道更多关于这一部分的内容;
.ExposeConfiguration(cfg => { var schemaExport = new SchemaExport(cfg); schemaExport.Drop(true, true); schemaExport.Create(true, true); })
理想我希望schemaExport.Drop(true, true);
删除数据库模式和schemaExport.Create(true, true);
重新创建它。 现在,我们知道SchemaExport
是关于数据库模式的吗? 我问这个,因为当我使用上述配置运行我的应用程序时,我收到一个错误:
There is already an object named 'AllUsers' in the database.
at schemaExport.Create(true, true);
AllUsers
是我作为模式的一部分的数据库视图之一
根据要求添加答案
为了解决这个问题,我添加了SchemaAction.None();
到UserMap如下所示。
public class UserMap : VersionedClassMap { public UserMap() { Table("AllUsers"); //This is the database view which was causing the error SchemaAction.None(); // This was added to fix the porblem Id(x => x.UserId).CustomType(); Map(x => x.Firstname).Not.Nullable(); Map(x => x.Lastname).Not.Nullable(); Map(x => x.Email).Nullable(); Map(x => x.Username).Not.Nullable(); } }
错误说Schemaexport尝试两次创建AllUsers,很可能是因为有一个AuxiliaryDatabase对象来创建视图,一个实体映射来使用它。 在AllUsers映射中设置SchemaAction.None()
。
也:
.ExposeConfiguration(cfg => { var schemaExport = new SchemaExport(cfg); schemaExport.Drop(true, true); schemaExport.Create(true, true); })
可以缩短为
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
因为Create在创建之前总是会丢弃它会复制丢弃的内容。
上述就是C#学习教程:什么是Fluent NHibernate中的schemaExport?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/956971.html