首页 文章

Fluent Nhibernate - 如何在SQL CE 4中自动生成表时指定表模式

提问于
浏览
2

我使用SQL CE作为运行本地和CI集成测试的数据库(通常我们的站点在普通的SQL服务器上运行) . 我们正在使用Fluent Nhibernate进行映射,并使用它从Mapclasses创建我们的模式 . 只有两个类之间有一对多的关系 . 在我们的真实数据库中,我们使用非dbo模式 . 在我将模式名称添加到Table()方法之前,代码首先不能与此真实数据库一起使用 . 然而,这样做打破了错误的单元测试...

System.Data.SqlServerCe.SqlCeException : There was an error parsing the query. [ Token line number = 1,Token line offset = 26,Token in error = User ]

这些是类和associatad MapClasses(当然简化)

public class AffiliateApplicationRecord
{
    public virtual int Id { get; private set; }
    public virtual string CompanyName { get; set; }
    public virtual UserRecord KeyContact { get; private set; }

    public AffiliateApplicationRecord()
    {
        DateReceived = DateTime.Now;
    }

    public virtual void AddKeyContact(UserRecord keyContactUser)
    {
        keyContactUser.Affilates.Add(this);
        KeyContact = keyContactUser;
    }
}

public class AffiliateApplicationRecordMap : ClassMap<AffiliateApplicationRecord>
{
    public AffiliateApplicationRecordMap()
    {
        Schema("myschema");
        Table("Partner");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.CompanyName, "Name");
                    References(x => x.KeyContact)
            .Cascade.All()
            .LazyLoad(Laziness.False)
            .Column("UserID");       
    }
}

public class UserRecord
{
    public UserRecord()
    {
        Affilates = new List<AffiliateApplicationRecord>();
    }

    public virtual int Id { get; private set; }
    public virtual string Forename { get; set; }
    public virtual IList<AffiliateApplicationRecord> Affilates { get; set; }
}

public class UserRecordMap : ClassMap<UserRecord>
{
    public UserRecordMap()
    {
        Schema("myschema");
        Table("[User]");//Square brackets required as user is a reserved word
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Forename);

        HasMany(x => x.Affilates);

    }
}

这是我正在使用的流畅配置....

public static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(
                MsSqlCeConfiguration.Standard
                    .Dialect<MsSqlCe40Dialect>()
                    .ConnectionString(ConnectionString)
                    .DefaultSchema("myschema"))
            .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
            .ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
            .ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close")) //This is included to deal with a SQLCE issue http://stackoverflow.com/questions/2361730/assertionfailure-null-identifier-fluentnh-sqlserverce
            .BuildSessionFactory();

    }

关于流利这方面的文档相当薄弱,所以任何帮助都会受到赞赏

2 回答

  • 6

    像往常一样,发布后10分钟我回答我自己的问题 . 诀窍是不在ClassMaps中声明架构 . 相反,我在流畅的配置中使用了DefaultSchema方法 . 所以我的实际“实时”配置如下所示:

    var configuration = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("connectionStringKey"))
                .DefaultSchema("myschema"))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<AffiliateApplicationRecord>());
    
            return configuration;
    

    我的集成测试看起来像这样:

    return Fluently.Configure()
                .Database(
                    MsSqlCeConfiguration.Standard
                        .Dialect<MsSqlCe40Dialect>()
                        .ConnectionString(ConnectionString))
                .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
                .ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
                .ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close")) //This is included to deal with a SQLCE issue http://stackoverflow.com/questions/2361730/assertionfailure-null-identifier-fluentnh-sqlserverce
                .BuildSessionFactory();
    

    希望别人能从中获得一些 Value ......

  • 0

    我认为这与SQL Server CE有关,而不是与nhibernate有关 . 我很确定Sql Server CE根本不会接受模式 . 它不是一个受支持的功能 .

    请参阅MSDN上的Create Table文档

相关问题