Fluent NHibernate - 从映射中删除架构以使用 SQLite 进行测试
我正在尝试使用 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 测试配置因为它讨厌模式。 =D
I am trying to run some tests on my mapping using SQLite. My mappings look like the following:
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");
}
}
The problem with this is that blanka causes the configuration below to fail. If I remove the blanka
schema from my mapping my tests pass, but obviously my mappings stop working in my real app. Is there a way to remove the blanka
schema from my mapping in the somewhere in the setup code below? Or is there a way to setup SQLite to work with it?
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);
}
Update
Here is how I ended up fixing this:
I removed the schema from my UserMap so that it looked like this:
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");
}
}
Then I changed my app configuration to set the default schema to blanka which looks like this:
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();
}
And I left my SQLite test config alone because it hates schemas. =D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何使用 default_schema 配置属性(我知道它在配置中,不确定如何流畅地设置它)而不是将其放入类映射中。
What about using the default_schema configuration property (I know it's in the config, not sure offhand how to set it fluently) instead of putting it in class mapping.
来自 Fluent NHibernate wiki:
from Fluent NHibernate wiki: