Automapping and Fluent NHibernate
我从 Fluent NHibernate GitHub 文档中复制了用于自动映射的示例,但它在我的 ASP.NET MVC 4 应用程序中不起作用。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Product
{ public virtual int Id { get; set; } public virtual string Name { get; set; } public virtual decimal Price { get; set; } } public class Shelf public Shelf() |
是模型。当我添加
1
2 |
.Mappings(m => m.AutoMappings
.Add(AutoMap.AssemblyOf<Product>())) |
对于我的配置,我得到错误 No parameterless constructor defined for this object.。没有它,我的会话工作正常,并且我一一定义映射,一切正常。只是自动映射不起作用。什么问题?
问题在于,在包含 Product 的程序集中,可能是一些对象/实体,具有这样的”缺少无参数构造函数”(一些辅助对象)。您必须更具体,尝试使用 .Where() 来缩小自动映射对象/实体的集合
1
2 3 4 |
.Mappings(m =>
m.AutoMappings .Add(AutoMap.AssemblyOf<Product>() .Where(t => t.Namespace ==… |
…或类似的东西。准确地说应该自动映射的内容。
- 它有效…非常感谢。我需要等待接受答案:)
- 伟大的。享受 NHibernate ;)
来源:https://www.codenong.com/21088750/