从映射中删除Schema,用SQLite进行测试
本文关键字:SQLite 测试 映射 删除 Schema | 更新日期: 2023-09-27 17:53:26
我正在尝试使用SQLite对我的映射运行一些测试。我的映射如下所示:
public class UserMap : BaseValidatableDomainMap<User>
{
public UserMap()
{
Table("blanka.[User]");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.UserName);
Map(x => x.FirstName);
Map(x => x.MiddleName);
Map(x => x.LastName);
Map(x => x.EmailAddress);
Map(x => x.OtherEmailAddress);
Map(x => x.PhoneNumber);
Map(x => x.City);
References(x => x.Company, "CompanyId");
References(x => x.State, "StateId");
}
}
这样做的问题是blanka会导致下面的配置失败。如果我从我的映射中删除blanka
模式,我的测试通过了,但显然我的映射在我的实际应用程序中停止工作。是否有一种方法可以从我的映射中删除blanka
模式在下面的设置代码中的某个地方?或者有一种方法来设置SQLite与它一起工作?
static ISessionFactory BuildSessionFactory()
{
return Fluently.Configure()
.Database(SQLiteConfiguration.Standard.UsingFile(DB_FILE_NAME))
.Mappings(m => m.FluentMappings.Add<UserMap>())
.Mappings(m => m.FluentMappings.Add<CompanyMap>())
.Mappings(m => m.FluentMappings.Add<StateMap>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
static void BuildSchema(NHibernate.Cfg.Configuration cfg)
{
if (File.Exists(DB_FILE_NAME))
File.Delete(DB_FILE_NAME);
new SchemaExport(cfg).Create(false, true);
}
更新我最终是这样修复的:
我从UserMap中删除了模式,使其看起来像这样:
public class UserMap : BaseValidatableDomainMap<User>
{
public UserMap()
{
Table("[User]");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.UserName);
Map(x => x.FirstName);
Map(x => x.MiddleName);
Map(x => x.LastName);
Map(x => x.EmailAddress);
Map(x => x.OtherEmailAddress);
Map(x => x.PhoneNumber);
Map(x => x.City);
References(x => x.Company, "CompanyId");
References(x => x.State, "StateId");
}
}
然后我改变了我的应用配置,将默认模式设置为blanka,如下所示:
private static ISessionFactory CreateSessionFactory()
{
var config = Fluently.Configure();
config = config.Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("BLANKADB")).DefaultSchema("blanka"))
.ExposeConfiguration( c => c.SetProperty("current_session_context_class", "web"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BlankaObject>());
return config.BuildSessionFactory();
}
我留下我的SQLite测试配置单独,因为它讨厌模式。div = D
使用default_schema配置属性(我知道它在配置中,不确定如何设置它流畅)而不是将其放在类映射中。
from Fluent NHibernate wiki:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Schema("alternativeSchema");
}
}