[英]How to configure this one-to-zero-or-one relationship?
I know there are some answered questions on SO about 1:0..1-relationships.我知道有一些关于 1:0..1 关系的已回答问题。 I have looked at this and this , but don't think they apply to my question.
我已经看过这个和这个,但不要认为它们适用于我的问题。
I have these three (simplified) models in a CMS-system.我在 CMS 系统中有这三个(简化的)模型。
public class FrontPageItem
{
public int Id { get; set; }
public int ItemType { get; set; } // 1 = Article, 2 = WebPage, etc...
public int? ArticleId { get; set; }
public Article Article { get; set; }
public int? WebPageId { get; set; }
public WebPage WebPage { get; set; }
}
public class Article
{
public int Id { get; set; }
public string Title { get; set; }
public string Preamble { get; set; }
public string MainText { get; set; }
public int? FrontPageItemId { get; set; }
public FrontPageItem FrontPageItem { get; set; }
}
public class WebPage
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int? FrontPageItemId { get; set; }
public FrontPageItem FrontPageItem { get; set; }
}
The relationships between a FrontPageItem
and each of the different element types are one-to-zero-or-one. FrontPageItem
和每个不同元素类型之间的关系是一对零或一的关系。 An element can exist without being added as a FrontPageItem
, meaning that a FrontPageItem
has a relationship to just one element, either an Article
or a WebPage
.元素可以不作为
FrontPageItem
添加而存在,这意味着FrontPageItem
只与一个元素( Article
或WebPage
。
In an attempt to configure the relationships, I have added this bit of code:为了配置关系,我添加了以下代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Article>()
.HasOne(p => p.FrontPageItem)
.WithOne(i => i.Article)
.HasForeignKey<FrontPageItem>(b => b.Id);
modelBuilder.Entity<WebPage>()
.HasOne(p => p.FrontPageItem)
.WithOne(i => i.WebPage)
.HasForeignKey<FrontPageItem>(b => b.Id);
}
But I don't think it's correct.但我不认为这是正确的。 I haven't made the CRUD-views for
FrontPageItem
yet, but if I try to add items directly in the SQL Server Object Explorer in VS, I have to enter a value for the PK.我还没有为
FrontPageItem
CRUD 视图,但是如果我尝试直接在 VS 的 SQL Server 对象资源管理器中添加项目,我必须为 PK 输入一个值。
What am I doing wrong?我究竟做错了什么?
As you said:如你所说:
The relationships between a
FrontPageItem
and each of the different element types areone-to-zero-or-one
.FrontPageItem
和每个不同元素类型之间的关系是one-to-zero-or-one
。 An element can exist without being added as aFrontPageItem
, meaning that aFrontPageItem
has a relationship to just one element, either anArticle
or aWebPage
.元素可以不作为
FrontPageItem
添加而存在,这意味着FrontPageItem
只与一个元素(Article
或WebPage
。
Then remove ArticleId
and WebPageId
from FrontPageItem
as EF core support one-to-one-or-zero
association without foreign key in principle table:然后从
FrontPageItem
删除ArticleId
和WebPageId
因为 EF 核心支持one-to-one-or-zero
关联,原则表中没有外键:
public class FrontPageItem
{
public int Id { get; set; }
public int ItemType { get; set; } // 1 = Article, 2 = WebPage, etc...
public Article Article { get; set; }
public WebPage WebPage { get; set; }
}
Then the Fleunt API configuration for Article
and WebPage
as follows:那么
Article
和WebPage
的Fleunt API配置如下:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Article>()
.HasOne(a => a.FrontPageItem)
.WithOne(f => f.Article)
.HasForeignKey<Article>(a => a.FrontPageItemId); // <-- Here it is
modelBuilder.Entity<WebPage>()
.HasOne(wp => wp.FrontPageItem)
.WithOne(f => f.WebPage)
.HasForeignKey<WebPage>(wp => wp.FrontPageItemId); // <--Here it is
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.