实体框架多态关联 [英] Entity Framework Polymorphic associations

查看:146
本文介绍了实体框架多态关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要尽快使用实体框架的预约系统(从头做起)。

I'm going to use Entity Framework soon for a Booking System ( made from scratch ).

我一直在做一些原型,试图找出是什么我想要做的项目启动之前(我仍然在讨论与客户的要求等)

I have been doing a few Prototypes, trying to figure out what I want to do before the project is started (I'm still discussing requirements etc. with the client).

考虑这种情况:

我有一个预订,而预订可以有关联的Ressources,可以预约,但这些的ressource可以不同,而有不同的领域等。

I have a Booking, and a booking can have associated Ressources, that can be booked, but these ressource can be different, and have different Fields etc.

我从来没有真正使用EF之前,所以我不知道我怎么能实现这种多态的,那我在其他项目中使用(这是我们一直使用原始的SQL)。

I have never really used EF before, so I don't know how I can achieve this kind of Polymorphism, that I am use to in other projects (where we have been using raw SQL).

C#的模式是这样的:

The C# Model would look like this:

public class Booking
{
    public int BookingID { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
    public IRessource Ressources { get; set; }
    ...
}

public interface IRessource
{
    public int RessourceTypeId { get; set; }
}

public class Room : IRessource
{
    public int RoomID { get; set; }
    public int RessourceTypeId { get; set; }
    ...
}

public class Car : IRessource
{
    public int CarID { get; set; }
    public int RessourceTypeId { get; set; }
    ...
}



这是可以实现的,如果是,那么如何?
的查询怎么会连看?

Is this achievable, and if yes, then how? How would the querying even look?

推荐答案

是的,绝对。

这听起来像你真的想要一个的表每个类型(TPT)关系。这代表了标准的是-A / HAS-A 外键关系。注意,在默认情况下,实体框架采用表每层次(TPH)

It sounds like you really want a Table Per Type (TPT) relationship. This represents the standard is-a / has-a foreign key relationships. Note that by default, Entity Framework utilizes Table Per Hierarchy (TPH).

下面有一些链接:

我强烈建议你去通过一些你自己的这些链接,但主要的一点提取的是,你使用的东西这样的:

I highly recommend you go through some of these links on your own, but the main point to extract is that you use something like:

modelBuilder.Entity<IRessource>().ToTable("IRessources");
modelBuilder.Entity<Room>().ToTable("Rooms");
modelBuilder.Entity<Car>().ToTable("Cars");



这两者从 IRessource 在继承你的证明了上面的代码。然后,你可以做这些单独的表和中央之间的连接 IRessource 表让所有的有关的个别实体的信息。

Both of which inherit from IRessource in your demonstrated code above. Then, you can do a join between these individual tables and the central IRessource table to get all of the information pertaining to the individual entities.

是这样的: SELECT * FROM IRessource我同室R ON i.id == r.id

不过,你经常会发现时间,如果你使用的Web API 控制器,如果你只是把整个客房 POCO,你会立刻得到其所有属性,而不加入了!

However, you'll often times find that if you use a Web API controller, if you just pull the entire Room POCO, you'll get all of its properties at once without the join at all!

在我的经验中,我还发现,通常事情顺畅,如果你的不使用接口或抽象类的,因为如果你这样做,你就必须让数据传输对象(DTO的)偶尔传输数据,因为你不能实例化这些类型的对象。事情可能会变得稍微有点乱 - 特别是如果你不知道自己在做什么。为了帮助你理解,认为一个控制器与您想通过一个通用的 IRressource - 它会尝试在收到他们到一个对这些对象的转换从而可以'T被实例化。坏消息!所以,最简单的办法就是使两者的基类,继承类普通类。

In my experience, I've also found that typically things go smoother if you do not use interfaces or abstract classes because if you do, you'll have to make Data Transfer Objects (DTOs) to transfer data occasionally since you can't instantiate objects of those types. Things can get a little bit messy -- especially if you're not sure exactly what you're doing. To help you understand, think of a controller with which you want to pass in a generic IRressource -- it'll try to convert those objects as it receives them into one which can't be instantiated. Bummer! So, the easy solution is just to make both the base class and the inherited classes normal classes.

这篇关于实体框架多态关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆