简体   繁体   English

EF 4.1编码第一个多个一对多关联

[英]EF 4.1 Code first multiple one-to-many associations

Here is my model 这是我的模特

public class Horse
{
    public int HorseId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public LegType LegType { get; set; }
    public Character Character { get; set; }
    public int Hearts { get; set; }
    public bool Retired { get; set; }
    // Parents
    public Horse Sire { get; set; }
    public Horse Dam { get; set; }
    // Internals
    public int Stamina { get; set; }
    public int Speed { get; set; }
    public int Sharp { get; set; }
    // Special
    public int Dirt { get; set; }
    // Externals
    public int Start { get; set; }
    public int Corner { get; set; }
    public int OutOfTheBox { get; set; }
    public int Competing { get; set; }
    public int Tenacious { get; set; }
    public int Spurt { get; set; }
    //Races
    public virtual ICollection<Race> RaceResults { get; set; }
    //Training
    public virtual ICollection<Training> TrainingResults { get; set; }
}

public class Race
{
    public int RaceId { get; set; }
    public int Favorite { get; set; }
    public LegType LegType { get; set; }
    public int Players { get; set; }
    public DateTime Split { get; set; }
    public DateTime Final { get; set; }
    public int Position { get; set; }

    public virtual int TrackId { get; set; }
    public virtual Track Track { get; set; }

    public virtual int LinkedHorseId { get; set; }
    public virtual Horse LinkedHorse { get;set; }
}

public class Training
{
    public int TrainingId { get; set; }
    public string Type { get; set; }
    public string Result { get; set; }
    public string Food { get; set; }
    public int Start { get; set; }
    public int Corner { get; set; }
    public int Outofthebox { get; set; }
    public int Competing { get; set; }
    public int Tenacious { get; set; }
    public int Spurt { get; set; }

    public virtual int LinkedHorseId { get; set; }
    public virtual Horse LinkedHorse { get; set; }
}

public class Track
{
    public int TrackId { get; set; }
    public string Name { get; set; }
    public int Distance { get; set; }
    public bool G1 { get; set; }
    public int Prize { get; set; }
}

And here is my fluent API code.

public class HorseTracker : DbContext
{
    public DbSet<Horse> Horses { get; set; }
    public DbSet<LegType> LegTypes { get; set; }
    public DbSet<Character> Characters { get; set; }
    public DbSet<Training> TrainingResults { get; set; }
    public DbSet<Track> Tracks { get; set; }
    public DbSet<Race> Races { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Race>()
            .HasRequired(r => r.LinkedHorse)
            .WithMany(h => h.RaceResults)
            .HasForeignKey(r => r.LinkedHorseId);

        modelBuilder.Entity<Training>()
            .HasRequired(t => t.LinkedHorse)
            .WithMany(t => t.TrainingResults)
            .HasForeignKey(t => t.LinkedHorseId);

        modelBuilder.Entity<Race>()
            .HasRequired(r => r.Track)
            .WithMany()
            .HasForeignKey(r => r.TrackId)
            .WillCascadeOnDelete(false);
    }
}

I keep getting this error: Unable to determine the principal end of an association between the types 'DOCCL.Models.Horse' and 'DOCCL.Models.Horse'. 我不断收到此错误:无法确定类型'DOCCL.Models.Horse'和'DOCCL.Models.Horse'之间的关联的主要终点。 The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations. 必须使用关系流利的API或数据注释显式配置此关联的主要端。

Any clue what i'm doing wrong. 任何提示我在做什么错。 I've been playing around with no foreign keys. 我一直在玩,没有外键。 making one of the required lists optional. 使所需列表之一成为可选。 they all result in different errors. 它们都会导致不同的错误。 mostly saying that the relation needs to be a 1:1 relation. 大多说关系必须是1:1关系。 And once it said that it had a non nullable field. 并且一旦它说它有一个不可为空的字段。

I made that nullable int? 我做了那个可空的诠释? and then i got the first error again. 然后我又遇到了第一个错误。

I think you need to setup self-referencing relationships manually (specifically, the Horse class properties Sire and Dam are causing an issue). 我认为您需要手动设置自引用关系(特别是Horse类属性SireDam引起了问题)。

Try this (in the answer): 试试这个(答案):

What is the syntax for self referencing foreign keys in EF Code First? EF Code First中自引用外键的语法是什么?

You could add two more int IDs representing the foreign keys (SireId, DamId). 您可以再添加两个表示外键的int ID(SireId,DamId)。

If you add this to your model configuration it should work: 如果将其添加到模型配置中,它将可以正常工作:

modelBuilder.Entity<Horse>()
            .HasRequired(h => h.Dam)    // or HasOptional
            .WithMany();

modelBuilder.Entity<Horse>()
            .HasRequired(h => h.Sire)   // or HasOptional
            .WithMany();

The problem is that the mapping conventions try to create a one-to-one relationship between Dam and Sire and therefore cannot determine what's the principal and what's the dependent because both are optional. 问题在于映射约定试图在DamSire之间建立一对一关系,因此无法确定主体是什么,从属是什么,因为两者都是可选的。 Anyway I guess you don't want a one-to-one relationships but actually two one-to-many relationships (the many-side (the "children") not being exposed in the model). 无论如何,我猜您不是想要一对一的关系,而是实际上想要两个一对多的关系(模型中没有暴露多面(“孩子”))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM