如何使用 NHibernate 映射多个接口实现

发布于 2024-10-29 19:36:06 字数 893 浏览 1 评论 0原文

我搜索了很多关于如何使用 EntityFramework 或 NHibernate 映射多重继承或多接口植入的信息,但我没有找到任何有用的东西。

我只是想使用 NHibernate 映射此结构:

    public interface IA
    {
        string A { get; set; }
    }

    public interface IB
    {
        string B { get; set; }
    }

    public class C : IA, IB
    {
        string A { get; set; }
        string B { get; set; }            
    }

据我所知,将此结构映射到关系数据库意味着只需具有与接口主键相关的外键,因此接口应该具有如下所示的键:

public interface IA
{
    Guid AId { get; set; }
    string A { get; set; }
}

public interface IB
{
    Guid BId { get; set; }
    string B { get; set; }
}

public class C : IA, IB
{
   public virtual Guid AId { get; set; }
   public virtual Guid BId { get; set; }
   public virtual string A { get; set; }
   public virtual string B { get; set; }
}

但是如何映射此结构使用NHibernate或EntityFramework,我不知道为什么他们的文档中没有提到多接口映射!

I searched a lot about how to map multiple inheritance or multiple interface implantation using EntityFramework or NHibernate But I didn't find anything useful.

I Simply want to map this structure using NHibernate:

    public interface IA
    {
        string A { get; set; }
    }

    public interface IB
    {
        string B { get; set; }
    }

    public class C : IA, IB
    {
        string A { get; set; }
        string B { get; set; }            
    }

As far as i know mapping this structure to a relational database means just to have foreign keys related with the interfaces primary keys, therefore the interfaces should have Keys like these:

public interface IA
{
    Guid AId { get; set; }
    string A { get; set; }
}

public interface IB
{
    Guid BId { get; set; }
    string B { get; set; }
}

public class C : IA, IB
{
   public virtual Guid AId { get; set; }
   public virtual Guid BId { get; set; }
   public virtual string A { get; set; }
   public virtual string B { get; set; }
}

But how to map this structure using NHibernate Or EntityFramework,and I don't know why multiple interface mapping is not mentioned in their documentation!

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

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

发布评论

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

评论(3

兲鉂ぱ嘚淚 2024-11-05 19:36:07

正如我所发现的,由于这个概念,在关系数据库中不可能有多重继承,迭戈所说的在“接口不持久的场景”中是正确的。

As what I've founded it's not possible to have multiple inheritance in a relational database due to the concept and what Diego said is true in a "not interfaces get persisted scenario".

許願樹丅啲祈禱 2024-11-05 19:36:06

在 NHibernate 中,您只需映射 C 就好像接口不存在

由于隐式多态性,您仍然可以在接口上进行查询。

In NHibernate, you'll just map C as if the interfaces didn't exist.

You'll still be able to query on the interfaces, thanks to implicit polymorphism.

泪痕残 2024-11-05 19:36:06

您将把它映射为任何其他类,因为这不是继承映射。此外,您的代码无法编译,因为您必须实现类 C 中的所有属性,因此您将得到:

public interface IA
{
    Guid AId { get; set; }
    string A { get; set; }
}

public interface IB
{
    Guid BId { get; set; }
    string A { get; set; }
}

public class C : IA, IB
{
    public virtual Guid AId { get; set; }
    public virtual Guid BId { get; set; }
    public virtual string A { get; set; }
}

现在您的代码可以编译,并且您拥有与其他任何类一样的类。您将 AId 和 BId 映射为复合键(取决于使用的 ORM),然后就完成了。这不是继承,因为您只有单个实体而没有基本实体。至少这是它与实体框架一起工作的方式。

You will map it as any other class because this is not inheritance mapping. Moreover your code cannot be compiled because you must implement all properties in class C so you will get:

public interface IA
{
    Guid AId { get; set; }
    string A { get; set; }
}

public interface IB
{
    Guid BId { get; set; }
    string A { get; set; }
}

public class C : IA, IB
{
    public virtual Guid AId { get; set; }
    public virtual Guid BId { get; set; }
    public virtual string A { get; set; }
}

Now your code can be compiled and you have class as any other. You will map AId and BId as composite key (depending on used ORM) and you are done. This is not inheritance because you have just single entity and no base enity. At least this is how it works with Entity framework.

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