Fluent NHibernate - Remove Schema from Mappings for Testing With SQLite
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(SQLiteCo开发者_Go百科nfiguration.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
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.
from Fluent NHibernate wiki:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Schema("alternativeSchema");
}
}
精彩评论