本文介绍了如何配置这种一对零或一对一的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道关于1:0..1-关系有一些已回答的问题。我看过和,但不要认为它们适用于我的问题

I know there are some answered questions on SO about 1:0..1-relationships. I have looked at this and this, but don't think they apply to my question.

我在CMS系统中拥有这三个(简化的)模型。

I have these three (simplified) models in a CMS-system.

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; }
}

FrontPageItem ,每个不同的元素类型都是一对零或一个。元素可以存在而无需添加为 FrontPageItem ,这意味着 FrontPageItem 仅与一个元素相关,或者文章网页

The relationships between a FrontPageItem and each of the different element types are 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.

为了配置关系,我添加了以下代码:

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);
}

但我认为这是不正确的。我还没有为 FrontPageItem 创建CRUD视图,但是如果我尝试直接在VS的SQL Server Object Explorer中添加项目,则必须输入一个值

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 ArticleId WebPageId >作为EF核心支持一对一或零在原则表中没有外键的关联:

Then remove ArticleId and WebPageId from FrontPageItem as EF core support one-to-one-or-zero association without foreign key in principle table:

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; }
}

然后为<$>配置 Fleunt API c $ c>文章网页如下:

Then the Fleunt API configuration for Article and WebPage as follows:

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
}

这篇关于如何配置这种一对零或一对一的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 20:28