如何在 Entity Framework Core 中设置实体关系
How to setup entity relations in Entity Framework Core
我们在 EF Core 上得到了 3 tables:
- 新闻
- 项目
- 链接
以及更多(新闻、项目除外):内容、Post、表格等
还有我的模型定义
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
描述每个新闻和每个项目的 URL。
这意味着 Links
有 4 列:
- 编号
- 类型 - 新闻或项目
- RowId - 包含项目或新闻的 ID(取决于类型)
- URL
如何建立关系?
请记住,我们需要通过 URL 在 Links table.
中解析 Entity
更新
我再次阅读了你的问题,老实说,你不必为链接创建另一个 table,只需在新闻和项目中添加一个专栏等等..你会没事的.
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; }
}
我会更改 Link
以使用可为空的 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; }
}
我认为您可以从链接 table 中删除 rowid,并在其他 2 table 中添加一个外键列,引用链接 table 中的 id 列。
现在,有了 URL,有了类型和 ID,就可以从各自的 table.
中查询内容了
为了拥有 属性 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;
}
}
}
然后将此 属性 设置为未映射到 ContextDB。
小巧玲珑。只是允许编写自定义查询的 Dapper。
我们在 EF Core 上得到了 3 tables:
- 新闻
- 项目
- 链接
以及更多(新闻、项目除外):内容、Post、表格等
还有我的模型定义
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
描述每个新闻和每个项目的 URL。
这意味着 Links
有 4 列:
- 编号
- 类型 - 新闻或项目
- RowId - 包含项目或新闻的 ID(取决于类型)
- URL
如何建立关系? 请记住,我们需要通过 URL 在 Links table.
中解析 Entity更新
我再次阅读了你的问题,老实说,你不必为链接创建另一个 table,只需在新闻和项目中添加一个专栏等等..你会没事的.
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; }
}
我会更改 Link
以使用可为空的 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; }
}
我认为您可以从链接 table 中删除 rowid,并在其他 2 table 中添加一个外键列,引用链接 table 中的 id 列。
现在,有了 URL,有了类型和 ID,就可以从各自的 table.
中查询内容了为了拥有 属性 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;
}
}
}
然后将此 属性 设置为未映射到 ContextDB。
小巧玲珑。只是允许编写自定义查询的 Dapper。