Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(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

like image 253
Jason Avatar asked Oct 23 '25 03:10

Jason


2 Answers

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.

like image 163
Rich Avatar answered Oct 25 '25 18:10

Rich


from Fluent NHibernate wiki:

public class PersonMap : ClassMap<Person>
{
  public PersonMap()
  {
    Schema("alternativeSchema");
  }
}
like image 35
Nelson Miranda Avatar answered Oct 25 '25 18:10

Nelson Miranda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!