gpt4 book ai didi

c# - Fluent NHibernate 中的 schemaExport 是什么?

转载 作者:太空狗 更新时间:2023-10-30 01:20:49 25 4
gpt4 key购买 nike

我很想知道更多关于这段代码是如何执行的以及执行时的预期结果。

        /// <summary>
/// Sets up NHibernate, and adds an ISessionFactory to the given
/// container.
/// </summary>
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<SqlCommandFactory>())
//.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<ISessionFactory>().ToConstant(sessionFactory);

// Configure a resolver method to be used for creating ISession objects
container.Bind<ISession>().ToMethod(CreateSession);

container.Bind<ICurrentSessionContextAdapter>().To<CurrentSessionContextAdapter>();
}

现在我知道大部分是做什么的,但我更有兴趣了解更多关于这部分的信息;

.ExposeConfiguration(cfg =>
{
var schemaExport = new SchemaExport(cfg);
schemaExport.Drop(true, true);
schemaExport.Create(true, true);
})

理想情况下,我希望 schemaExport.Drop(true, true); 删除数据库架构,然后 schemaExport.Create(true, true); 重新创建它。现在,是关于 Database SchemasSchemaExport我们知道吗?我问这个是因为当我使用上述配置运行我的应用程序时出现错误:

数据库中已经有一个名为“AllUsers”的对象。 at schemaExport.Create(true, true);

AllUsers 是我作为架构一部分的数据库 View 之一

按要求附加答案

为了修复这个错误,我添加了 SchemaAction.None(); 到 UserMap,如下所示。

public class UserMap : VersionedClassMap<User>
{
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<Guid>();
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 对象来创建 View 和一个实体映射来使用它。在 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 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17736546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com