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

发布于 2024-12-16 12:34:41 字数 2897 浏览 0 评论 0原文

我使用 SQL CE 作为运行本地和 CI 集成测试的数据库(通常我们的站点在普通 SQL 服务器上运行)。我们使用 Fluent Nhibernate 进行映射,并让它从 Mapclass 创建我们的模式。只有两个类之间存在一对多关系。在我们的真实数据库中,我们使用非 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 ]

这些是类和关联的 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();

    }

关于流利这方面的文档非常薄弱,所以任何帮助将不胜感激

I am using SQL CE as a database for running local and CI integration tests (normally our site runs on normal SQL server). We are using Fluent Nhibernate for our mapping and having it create our schema from our Mapclasses. There are only two classes with a one to many relationship between them. In our real database we use a non dbo schema. The code would not work with this real database at first until i added schema names to the Table() methods. However doing this broke the unit tests with the error...

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

These are the classes and associatad MapClasses (simplified of course)

    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);

    }
}

And here is the fluent configuraton i am using ....

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

    }

The documentation on this aspect of fluent is pretty weak so any help would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

双马尾 2024-12-23 12:34:41

像往常一样,发帖后 10 分钟我会回答自己的问题。诀窍是不在 ClassMap 中声明模式。相反,我在流畅的配置中使用了 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();

希望其他人能从中获得一些价值......

As usual, 10 minutes after posting i answer my own questions. The trick was to not declare the schema in the the ClassMaps. Instead i used the DefaultSchema method in the fluent configuration. So my actual 'live' configuration looks like this :

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

        return configuration;

And my integration tests look like this:

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

Hopefully someone else will get somevalue out of this...

他夏了夏天 2024-12-23 12:34:41

我认为这与 SQL Server CE 的关系比与 nhibernate 的关系更大。我非常确定 Sql Server CE 根本不接受模式。它不受支持的功能。

请参阅 MSDN 上的创建表文档

I think this has more to do with SQL Server CE than nhibernate. I am pretty sure that Sql Server CE will not accept schemas at all. its not a supported feature.

See the Create Table Documentation on MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文