Asp.net mvc 实现同一类的一对零或一关系以及一对多关系

Asp.net mvc 实现同一类的一对零或一关系以及一对多关系,asp.net-mvc,entity-framework,one-to-many,ef-fluent-api,fluent-interface,Asp.net Mvc,Entity Framework,One To Many,Ef Fluent Api,Fluent Interface,我在映射以下类时遇到问题 我希望main aboutpage是可选的(一对零或一),而aboutsubpage显然是一对多 理想情况下,我希望在WebsitePage类中保留WebsiteId属性 public class Website { public int Id { get; set; } public virtual WebsitePage MainAboutPage { get; set; } public ICollection<WebsitePa

我在映射以下类时遇到问题

我希望
main aboutpage
是可选的(一对零或一),而
aboutsubpage
显然是一对多

理想情况下,我希望在
WebsitePage
类中保留
WebsiteId
属性

public class Website
{
    public int Id { get; set; }

    public virtual WebsitePage MainAboutPage { get; set; }

    public ICollection<WebsitePage> AboutSubPages { get; set; }

}


public class WebsitePage
{
    public int Id { get; set; }

    public int WebsiteId { get; set; }

    public virtual Website Website { get; set; }
}
公共类网站
{
公共int Id{get;set;}
公共虚拟网站主页MainAboutPage{get;set;}
关于BPages{get;set;}的公共ICollection
}
公共类网站页面
{
公共int Id{get;set;}
公共int网站ID{get;set;}
公共虚拟网站{get;set;}
}

当我不使用fluent映射时,我得到

无法确定关系的主体端。多个添加的实体可能具有相同的主键


当我使用此fluent映射时:

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired();

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired(x => x.Website);

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
modelBuilder.Entity()
.HasMany(x=>x.aboutsubpage)
.WithRequired(x=>x.Website)
.HasForeignKey(x=>x.WebsiteId);
我得到:

无法确定“Wesbite_AboutSubPages”关系的主端。多个添加的实体可能具有相同的主键


当我使用这个流畅的映射时:

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired();

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired(x => x.Website);

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
modelBuilder.Entity()
.has可选(x=>x.main关于页面)
.WithRequired();
modelBuilder.Entity()
.HasMany(x=>x.aboutsubpage)
.WithRequired(x=>x.Website)
.HasForeignKey(x=>x.WebsiteId);
我得到:

无法确定“Website_MainAboutPage”关系的主体端。多个添加的实体可能具有相同的主键


当我使用这个流畅的映射时:

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired();

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
        modelBuilder.Entity<Website>()
           .HasOptional(x => x.MainAboutPage)
           .WithRequired(x => x.Website);

        modelBuilder.Entity<Wesbite>()
            .HasMany(x => x.AboutSubPages)
            .WithRequired(x => x.Website)
            .HasForeignKey(x => x.WebsiteId);
modelBuilder.Entity()
.has可选(x=>x.main关于页面)
.WithRequired(x=>x.Website);
modelBuilder.Entity()
.HasMany(x=>x.aboutsubpage)
.WithRequired(x=>x.Website)
.HasForeignKey(x=>x.WebsiteId);
我得到:

Wesbite_MainAboutPage_目标::多重性在关系“网站”中的角色“Wesbite_MainAboutPage_目标”中无效。因为依赖角色属性不是键属性,所以依赖角色的多重性上限必须为“*”


我一直在没完没了地阅读MS:and的配置示例

我的脑子进水了,如果我遗漏了一些明显的东西,请原谅。我真的很感激有人能给我一些建议,让我按照自己的意愿来设置


提前谢谢

我认为问题在于,您没有足够的信息让EF区分AboutSubPage和与网站相关的主要AboutPage参考。要在网站上为MainAboutPage建立1..0/1关系,需要在网站表中声明MainAboutPage

modelBuilder.Entity<Website>()
   .HasOptional(x => x.MainAboutPage)
   .WithRequired(x => x.Website)
   .HasForeignKey(x => x.MainAboutPageId);

。。。举个例子。这确保我们可以访问主页,同时确保网站只考虑分配给它的页面。可以在网站实体上设置未映射属性以从嵌入式集合返回“MainAboutPage”,但是我不建议使用未映射属性,因为它们很容易滑入Linq表达式,EF将抛出异常或执行过早执行(EF Core)解决这些问题。

这是一个非常有启发性的回答,谢谢。只有一件事。。。它不允许我将
.HasForeignKey(x=>x.MainAboutPageId)
链接到
.WithRequired()
(ForeignKeyNavigationPropertyConfiguration不包含HasForeignKey的定义)。我想我可以使用
Map.MapKey
相关问题,如果您觉得非常有帮助: