通过 Fluent NHibernate 使用实体框架 (v3.5) 模型中的实体

发布于 2024-09-18 06:56:20 字数 1687 浏览 4 评论 0原文

使用 Microsoft 实体框架设计器 (v3.5),我使用生成的 *.Designer.cs 类创建了实体模型 (*.edmx)。我可以使用该模型将我的对象持久保存到 MS SQL Server,没有任何问题。


我是 NHibernate 的新手,出于好奇,我现在想使用我的模型以及 Fluent NHibernate 和 SQLite 作为数据库。我已阅读 Fluent NHibernate wiki 上的自动映射文章。我确实调整了 Examples.FirstAutomappedProject 并调整了 ExampleAutomappingConfiguration 来映射我的实体。 (我只使用了 ShouldMapIsId 覆盖)。我的实体相当简单,并且模型中不包含对彼此的显式引用。
现在,当我构建会话工厂时,出现以下异常:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

出现内部异常,并显示消息

An association from the table XXX refers to an unmapped class: System.Data.EntityKey

None of myEntity' properties are of type System.Data.EntityKey< /代码>。

[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="MyStorageModel", Name="XXX")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class XXX: global::System.Data.Objects.DataClasses.EntityObject
{
  //...
     [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
     [global::System.Runtime.Serialization.DataMemberAttribute()]
     public global::System.Guid XXXID
     {
        //...
     }
  //...
}

如您所见,我的实体类经过大量装饰(当然是由使用过的设计者),其中的属性可能引用此类型。 这会导致这些麻烦吗?

Using Microsoft's designer for the Entity Framework (v3.5), I have created an Entity Model (*.edmx) with a generated *.Designer.cs class. I can persist my objects to MS SQL Server using the model without any problems.


I am new to NHibernate, and out of curiosity, I now would like to use my model with Fluent NHibernate and SQLite as database. I have read the Auto mapping article on the Fluent NHibernate wiki. I did adapt the Examples.FirstAutomappedProject and have adapted the ExampleAutomappingConfiguration to map my entities. (I used only the ShouldMap and IsId overrides). My entities are rather simple and do not contain explicit references to each other in the model.

Now, when I build the Session Factory, i get the following exception:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

with an inner exception with the message

An association from the table XXX refers to an unmapped class: System.Data.EntityKey

None of my entities' properties are of type System.Data.EntityKey.

[global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName="MyStorageModel", Name="XXX")]
[global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)]
[global::System.Serializable()]
public partial class XXX: global::System.Data.Objects.DataClasses.EntityObject
{
  //...
     [global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
     [global::System.Runtime.Serialization.DataMemberAttribute()]
     public global::System.Guid XXXID
     {
        //...
     }
  //...
}

As you see, my entity classes are heavily decorated (by the used designer of course) with a attributes that probably refer to this type. Can this cause these troubles?

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

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

发布评论

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

评论(2

忆依然 2024-09-25 06:56:25

使用数据库优先 EntityFramework 代码生成向导将生成与 EntityFramework 紧密耦合且不能轻松与 NHibernate 一起使用的模型类。

更糟糕的是,EntityFramework 3.5 并不真正支持 POCO 对象,而 POCO 对象(可能)可以与 NHibernate 一起使用。 EF 4.0 具有更好的 POCO 支持,但最终我不确定它是否会按照您想要的方式工作。

如果您真的想使用 Fluent NHibernate,我会扔掉 EntityFramework 模型类并重新开始。

以下是一些与 NHibernate 模型类代码生成相关的问题:

NHibernate 生成器
NHibernate 的最佳代码生成器是什么?

Using the database first EntityFramework code generation wizard will generate model classes that are tightly coupled to EntityFramework and that can't easily be used with NHibernate.

To make things worse, EntityFramework 3.5 doesn't really support POCO objects which would (likely) work with NHibernate. EF 4.0 has much better POCO support, but ultimately I'm not sure it would work as you want it to.

I would throw away the EntityFramework model classes and start over if you really want to work with Fluent NHibernate.

Here are some questions related to NHibernate model class code generation:

NHibernate Generators
what is the best code generator for NHibernate?

绝影如岚 2024-09-25 06:56:24

问题可能是您的实体是从 EntityObject 继承的。 EntityObject 声明两个公共属性:EntityKey 和 EntityState。 EntityKey 是 EntityKey 类型,它的处理方式类似于与您未提供给 NHibernate 的类的关联。我认为除非您使用 EF 4.0 和 POCO(不是从 EntityObject 继承),否则您将无法执行此操作。

The problem probably is that your entity is inherited from EntityObject. EntityObject declares two public properties: EntityKey and EntityState. EntityKey is of type EntityKey and it is handled like association to class which you didn't provide to NHibernate. I think you will not be able to do this unless you use EF 4.0 and POCO which didn't inherit from EntityObject.

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