Fluent NHibernate - Remove Schema from Mappings for Testing With SQLite

孤人 提交于 2020-01-14 16:35:06

问题


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


回答1:


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.




回答2:


from Fluent NHibernate wiki:

public class PersonMap : ClassMap<Person>
{
  public PersonMap()
  {
    Schema("alternativeSchema");
  }
}


来源:https://stackoverflow.com/questions/5822909/fluent-nhibernate-remove-schema-from-mappings-for-testing-with-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!