简体   繁体   English

如何配置这种一对零或一的关系?

[英]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只与一个元素( ArticleWebPage

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 are one-to-zero-or-one . FrontPageItem和每个不同元素类型之间的关系是one-to-zero-or-one 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只与一个元素( ArticleWebPage

Then remove ArticleId and WebPageId from FrontPageItem as EF core support one-to-one-or-zero association without foreign key in principle table:然后从FrontPageItem删除ArticleIdWebPageId因为 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:那么ArticleWebPageFleunt 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.

相关问题 EF代码优先-在不共享PK / FK的情况下配置一对零或一对关系 - EF code first - configure One-to-Zero-or-One relationship without shared PK/FK 使用CodeFirst的C#一对零或一个关系 - C# One-To-Zero-Or-One relationship using CodeFirst Code First EF5:One-to-Zero-One关系不起作用 - Code First EF5: One-to-Zero-or-One relationship is not working 如何使用两端的导航属性正确创建EF一对零或一个关系? - How to properly create an EF One-to-Zero-or-One relationship with navigation properties on both ends? 使用EF7映射一对一或一 - Mapping One-to-Zero-or-One with EF7 实体框架(EF)代码优先级联删除一对一或零关系 - Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship 一对一或零关系,其中FK是依赖表上的复合PK的一部分 - One-to-Zero-or-One relationship where the FK is part of a compoun PK on dependent tables 列不是主键时与EF Code First的一对零或一对关系 - One-to-Zero-or-One relationship with E.F. Code First when column is not primary key 如何配置模型中具有一对零或一种关系的外键 - How to configure a foreign key that is defined in the model with one to zero or one relationship 具有许多一对零或一对一关系的实体框架 - Entity framework with many one-to-zero-or-one relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM