简体   繁体   English

如何在Entity Framework Core中设置实体关系

[英]How to setup entity relations in Entity Framework Core

Here we are on EF Core and got 3 tables: 这是我们在EF Core上的3张桌子:

  1. News 新闻
  2. Items 物品
  3. Links 链接

And more (except News, Items): Content, Post, Form, etc. 以及更多(新闻,项目除外):内容,帖子,表格等。

And my model definitions 还有我的模型定义

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public Link Link { get; set; }
}

public class News
{
    public int Id { get; set; }
    public string Header { get; set; }
    public string Content { get; set; }
    public Link Link { get; set; }
}

public class Link
{
    public int Id { get; set; }
    public string Type { get; set; }
    public int RowId  { get; set; }
    public string Url { get; set; }
}

Table Links describe URL for every News and every Item. 表格Links描述了每个新闻和每个项目的URL。 This means that Links has 4 columns: 这意味着Links具有4列:

  1. Id ID
  2. Type - news or item 类型-新闻或项目
  3. RowId - contains ID of Item or News (depends on the Type) RowId-包含项目或新闻的ID(取决于类型)
  4. URL 网址

How to setup the relationships? 如何建立关系? Keep in mind that we need to resolve Entity by URL in Links table. 请记住,我们需要通过链接表中的URL来解析实体。

Dapper. 精致的 Just Dapper that allows write custom queries. Just Dapper允许编写自定义查询。

Update 更新资料


I read your question once more and well to be honest you don't have to create another table for Links, just add a Column to News and Item and so on.. and you will be fine. 我再次阅读了您的问题,说实话,您不必为链接创建另一个表,只需在新闻和项目中添加一列,依此类推..您就可以了。

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Url { get; set; }
}

public class News
{
    public int Id { get; set; }
    public string Header { get; set; }
    public string Content { get; set; }
    public string Url { get; set; }
}

I'd change Link to use nullable int , and separate out the foreign keys for these tables: 我将Link更改为使用nullable int ,并为这些表分离出外键:

public class Link
{
    public int Id { get; set; }
    public string Type { get; set; }
    public int? NewsId  { get; set; }
    public int? ItemId  { get; set; }
    public string Url { get; set; }
}

I think you can remove the rowid from the links table and add a foreign key column in the other 2 tables referencing the id column in the links table. 我认为您可以从链接表中删除rowid,并在其他2个表中添加外键列,这些表引用了链接表中的id列。

Now, with you having the URL, you have the type and id, you can just query the content from respective table. 现在,有了URL,就拥有了类型和ID,您只需从相应表中查询内容即可。

In order to have the property NewRow, and the constraints to the other two tables, you cold implement it like this: 为了拥有属性NewRow以及对其他两个表的约束,您可以像这样冷实施它:

public int RowId { 
    public get {
        return this.Type.equals("news") ? NewsId.Value : ItemId.Value;
    };   
    private set {
        if(this.Type.equals("news")){
          NewsId = value;
        }
        else{
          ItemId = value;
        }
    }
}

And then set this property as not mapped on the ContextDB. 然后将此属性设置为未映射到ContextDB上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM