实体框架核心:通用对象表之间的多对多关系 [英] Entity Framework Core: many-to-many relationship between generic object tables

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

问题描述

我有一个Cat and Dog表,我想与Image表建立多对多关系。我不想为每个表(DogImage,CatImage,BirdImage等)创建一个联接表,所以我想我将创建一个EntityImage联接表,然后使用Type字段来知道它是什么类型的图像。这是我对以下模型的尝试,但是这会在EntityImage表中创建CatId,DogId等外键,而不是使用我尝试定义的EntityId。有谁知道如何使用最新的Entity Framework Core正确处理此问题?
谢谢!

I have a Cat and Dog table and I want to create many-to-many relationships to an Image table. I don't want to create a join table for every table (DogImage, CatImage, BirdImage, etc) so I figured I would create a single EntityImage join table and then use a Type field to know what type of image it is. Here is my attempt at the model below, however this creates CatId, DogId, etc foreign keys in the EntityImage table, instead of using the EntityId I tried to define. Does anyone know how to handle this properly with the latest Entity Framework Core? Thanks!

public class Cat
{
    [Key] public int CatId { get; set; }
    public string CatName { get; set; }
    public ICollection<EntityFile> Files { get; } = new List<EntityFile>();
}

public class Dog
{
    [Key] public int DogId{ get; set; }
    public string DogName { get; set; }
    public ICollection<EntityImage> Images { get; } = new List<EntityImage>();
}

public class Image
{
    [Key] public int ImageId { get; set; }
    public string ImageName{ get; set; }
    public Byte[] Content { get; set; }
}

public class EntityImage
{
    public int EntityId { get; set; }
    public int ImageId { get; set; }
    public int ImageType { get; set; }
    public Image Image { get; set; }
}

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    builder.Entity<EntityImage>().ToTable("EntityImage").HasKey(t => new { t.EntityId, t.ImageId });
}


推荐答案

EF Core没有

在您的特定示例中,您可以只使用

In your particular example, you could just use

public class Animal {
    public string Name {get; set;}
    public byte[] Image {get; set;}
}

public class Cat : Animal {
    public int Id {get; set;}
}

public class Dog : Animal {
    public int Id {get; set;}
}

在EF 6中,您可以使用Animal,Dog&猫都去不同的桌子。 EF将设置密钥和您的导航字段。对象的类型(狗/猫)将控制您在动物中看到的记录。请注意,这是一对一关系的特殊情况(分层)。

In EF 6, you could have Animal, Dog & Cat all go to different tables. EF would set up the keys & navigation fields for you. The type of object (Dog/Cat) would control which records in Animal you would see. Note that this is a specialized case of one-to-one relationship (hierarchical).

您提到了很多对很多,但我发现两只动物共享的可能性很小相同的图像,并且如果跨物种重复使用名称是一个问题,请将名称移至派生类(狗/猫)。

You mentioned many to many, but I find it not very likely that two animals share the same image and if duplication of Name across species is an issue, move Name to the derived classes (Dog/Cat).

在某些时候,EF Core可能会这样做,但我还没有看到它(DataAnnotation / Fluent)。

At some point EF Core will probably do this, I just haven't seen it yet (DataAnnotation/Fluent).

这篇关于实体框架核心:通用对象表之间的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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