实体框架无法确定关系的主要结束。多个添加的实体可以具有相同的主键

时间:2016-02-23 21:52:38

标签: c# entity-framework

我遇到了EF的一些问题。 迁移很顺利,但是当我尝试运行update-database时出现错误:

Unable to determine the principal end of the 'LeagueInsight.Models.Image_Passive' relationship. Multiple added entities may have the same primary key.

以下是我的模型和配置:

Passive.cs

public class Passive
{
    public long Id { get; set; }

    public string Description { get; set; }
    public string Name { get; set; }
    public string SanitizedDescription { get; set; }

    // Navigation
    public virtual Image Image { get; set; }
    public virtual Champion Champion { get; set; }
}

Image.cs

public class Image
{
    public long Id { get; set; }

    public string Full { get; set; }
    public string Group { get; set; }
    public int H { get; set; }
    public string Sprite { get; set; }
    public int W { get; set; }
    public int X { get; set; }
    public int Y { get; set; }

    // Navigation
    public virtual Champion Champion { get; set; }
    public virtual ChampionSpell ChampionSpell { get; set; }
    public virtual Passive Passive { get; set; }
}

DBContext配置部分:

modelBuilder.Entity<Passive>()
    .HasRequired(p => p.Champion)
    .WithRequiredPrincipal(c => c.Passive);
modelBuilder.Entity<Info>()
    .HasRequired(i => i.Champion)
    .WithRequiredPrincipal(c => c.Info);
modelBuilder.Entity<Image>()
    .HasOptional(i => i.Champion)
    .WithRequired(c => c.Image);
modelBuilder.Entity<Image>()
    .HasOptional(i => i.Passive)
    .WithRequired(p => p.Image);

base.OnModelCreating(modelBuilder);

两者之间应该只是一个1-1的关系,我无法弄清楚为什么这里会有多个具有相同Id的实体。

编辑:我被要求参加冠军班:

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

    public string Blurb { get; set; }
    public string Key { get; set; }
    public string Lore { get; set; }
    public string Name { get; set; }
    public string Partype { get; set; }
    public string Title { get; set; }

    // Navigation
    [InverseProperty("Ally")]
    public virtual ICollection<Tip> Allytips { get; set; }
    [InverseProperty("Enemy")]
    public virtual ICollection<Tip> Enemytips { get; set; }
    public virtual Image Image { get; set; }
    public virtual Info Info { get; set; }
    public virtual Passive Passive { get; set; }
    public virtual ICollection<Recommended> Recommended { get; set; }
    public virtual ICollection<Skin> Skins { get; set; }
    public virtual Stats Stats { get; set; }
    public virtual ICollection<ChampionSpell> Spells { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

0 个答案:

没有答案