使用流畅的 nhibernate 映射进行持久性规范测试
我最近一直在玩流利的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在
PersistanceSpecification
类中使用CheckList
,而不是上面代码中的CheckReference
。You should be using
CheckList
in thePersistanceSpecification
class instead ofCheckReference
in the above code.嗯,我觉得有点傻。我从最初的帖子中排除的列之一是 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.