使用流畅的 nhibernate 映射进行持久性规范测试

发布于 2024-12-10 06:41:44 字数 2210 浏览 0 评论 0原文

我最近一直在玩流利的 nhibernate &更具体地说,持久性规范测试。

但是,在构建此行测试的架构时,我在使用 nunit 运行相对简单的测试时不断遇到 sql lite 错误:(SessionSource.BuildSchema(Session))。

System.Data.SQLite.SQLiteException:“/”附近的 SQLite 错误:语法 错误

寻求一些关于我做错了什么的指导,因为我对流利来说相对较新。有没有更简单的方法来解决此错误消息?

public class Contact
{
    public int Id { get; protected set; }
    // some other properties 
    public IList<Note> Notes { get; set; }
}

public ContactMapping()
{
    Not.LazyLoad();
    Id(m => m.Id).GeneratedBy.Identity();
    HasMany(x => x.Notes).KeyColumns.Add("ContactId");
}

public class Note
{
    public int Id { get; protected set; }
    public Contact Contact { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

public NoteMapping()
{
    Not.LazyLoad();
    Id(m => m.Id).GeneratedBy.Identity();
    Map(m => m.Title).Not.Nullable().Length(250);
    Map(m => m.Description).Not.Nullable().Length(2500);
    References(x => x.Contact).Column("ContactId").Cascade.All();
}

配置:

public void SetupContext()
{
    var cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard
            .ShowSql()
            .InMemory
            );
    SessionSource = new SessionSource(cfg.BuildConfiguration()
                                            .Properties, PersistenceModel());
    Session = SessionSource.CreateSession();
    SessionSource.BuildSchema(Session);
}

private static PersistenceModel PersistenceModel()
{
    var model = new PersistenceModel();
    model.AddMappingsFromAssembly(typeof(Contact).Assembly);
    return model;
}

最后是持久性测试:

new PersistenceSpecification<Contact>(Session)
    .CheckProperty(c => c.Id, 1)
    .CheckProperty(c => c.First, "Coding")
    .CheckProperty(c => c.Last, "Quiz")
    .CheckProperty(c => c.Email, "[email protected]")
    .CheckReference(c => c.Notes, new Note { Title = "Title", Description = "Description" })
    .VerifyTheMappings();

I've recently been playing around with fluent nhibernate & more specifically persistence specification testing.

However, I keep running into a sql lite error while running a relatively simple test with nunit when building the schema for the test on this line: (SessionSource.BuildSchema(Session)).

System.Data.SQLite.SQLiteException : SQLite error near "/": syntax
error

Seeking some some guidance on what I am doing wrong as I'm relatively new to fluent. Is there an easier way to troubleshoot this error message?

public class Contact
{
    public int Id { get; protected set; }
    // some other properties 
    public IList<Note> Notes { get; set; }
}

public ContactMapping()
{
    Not.LazyLoad();
    Id(m => m.Id).GeneratedBy.Identity();
    HasMany(x => x.Notes).KeyColumns.Add("ContactId");
}

public class Note
{
    public int Id { get; protected set; }
    public Contact Contact { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

public NoteMapping()
{
    Not.LazyLoad();
    Id(m => m.Id).GeneratedBy.Identity();
    Map(m => m.Title).Not.Nullable().Length(250);
    Map(m => m.Description).Not.Nullable().Length(2500);
    References(x => x.Contact).Column("ContactId").Cascade.All();
}

Config:

public void SetupContext()
{
    var cfg = Fluently.Configure()
        .Database(SQLiteConfiguration.Standard
            .ShowSql()
            .InMemory
            );
    SessionSource = new SessionSource(cfg.BuildConfiguration()
                                            .Properties, PersistenceModel());
    Session = SessionSource.CreateSession();
    SessionSource.BuildSchema(Session);
}

private static PersistenceModel PersistenceModel()
{
    var model = new PersistenceModel();
    model.AddMappingsFromAssembly(typeof(Contact).Assembly);
    return model;
}

And finally the persistence test:

new PersistenceSpecification<Contact>(Session)
    .CheckProperty(c => c.Id, 1)
    .CheckProperty(c => c.First, "Coding")
    .CheckProperty(c => c.Last, "Quiz")
    .CheckProperty(c => c.Email, "[email protected]")
    .CheckReference(c => c.Notes, new Note { Title = "Title", Description = "Description" })
    .VerifyTheMappings();

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

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

发布评论

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

评论(2

美人如玉 2024-12-17 06:41:44

您应该在 PersistanceSpecification 类中使用 CheckList,而不是上面代码中的 CheckReference

You should be using CheckList in the PersistanceSpecification class instead of CheckReference in the above code.

扭转时空 2024-12-17 06:41:44

嗯,我觉得有点傻。我从最初的帖子中排除的列之一是 DateTime 字段,该字段设置了不正确的默认值,从而在构建架构时生成无效的 sql。

构建架构时输出表的配置突出显示了我的错误。

Well, I feel a little silly. One of the columns I had excluded from my initial post was DateTime field which had an incorrect default value set which in turn was generating invalid sql when building the schema.

Outputting the configuration of the table when building the schema highlighted my error.

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