流畅的NHibernate - 从映射中删除模式以便使用SQLite进行测试

时间:2011-04-28 17:50:23

标签: c# nhibernate sqlite fluent-nhibernate

我正在尝试使用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

2 个答案:

答案 0 :(得分:3)

如何使用default_schema配置属性(我知道它在配置中,不知道如何将其设置为流畅),而不是将其放在类映射中。

答案 1 :(得分:3)

来自Fluent NHibernate wiki

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