none
CodeFirst实现表自我外键引用的问题 RRS feed

  • 问题

  • CodrFirst想实现以下关系:

    表Structure有主键ID列,外键DirParentStructureID 引用自己,外键InDirParentStructure 引用自己,请问这个如何实现?

    即:员工A有直线汇报主管B,并且有虚线汇报主管C。

    以下写法出错:Unable to determine the principal end of an association between the types 'XXX.Model.Structure' and 'XXX.Model.Structure'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

    public class Structure : EntityModel
        {
            public Structure()
            {

            }

            public Guid ID { get; set; }

            public Guid? DirParentStructureID { get; set; }

            [ForeignKey("DirParentStructureID")]

            public Structure DirParentStructure { get; set; }


            public Guid? InDirParentStructureID { get; set; }

            [ForeignKey("InDirParentStructureID")]
            public Structure InDirParentStructure { get; set; }

        }

            
    2014年9月1日 6:36

答案



  • Hello,

    >>Structure有主键ID列,外键DirParentStructureID 引用自己,外键InDirParentStructure 引用自己,请问这个如何实现?

    即:员工A有直线汇报主管B,并且有虚线汇报主管C

    员工和主管的关系是什么,一个主管有多个员工吗, 一个 员工只属于一个直线主管和一个虚线主管?如果是的话,你需要在OnModelCreating方法里明确指出他们的关系:

    public class Sample20140902 : DbContext
    
        {
    
            public DbSet<Structure> Structures { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.DirParentStructure).WithMany().HasForeignKey(s => s.DirParentStructureID);
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.InDirParentStructure).WithMany().HasForeignKey(s => s.InDirParentStructureID);
    
            }
    
        }
    
        public class Structure
    
        {
    
            public Guid ID { get; set; }
    
            //[ForeignKey("DirParentStructureID")]
    
            public Guid? DirParentStructureID { get; set; }
    
            public Structure DirParentStructure { get; set; }
    
            //[ForeignKey("InDirParentStructureID")]
    
            public Guid? InDirParentStructureID { get; set; }
    
            public Structure InDirParentStructure { get; set; }
    
        }

    如果是1:1的关系你需要把你的model 改成这样并在OnModelCreating方法里明确指出他们的关系:

    public class Sample20140902 : DbContext
    
        {
    
            public DbSet<Structure> Structures { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.DirParentStructure).WithOptional().Map(m=>m.MapKey("DirParentStructureID"));
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.InDirParentStructure).WithOptional().Map(m => m.MapKey("InDirParentStructureID"));
    
            }
    
        }
    
        public class Structure
    
        {
    
            public Guid ID { get; set; }
    
            public Structure DirParentStructure { get; set; }
    
            public Structure InDirParentStructure { get; set; }
    
        }

    Regards.



    2014年9月2日 9:12
    版主

全部回复



  • Hello,

    >>Structure有主键ID列,外键DirParentStructureID 引用自己,外键InDirParentStructure 引用自己,请问这个如何实现?

    即:员工A有直线汇报主管B,并且有虚线汇报主管C

    员工和主管的关系是什么,一个主管有多个员工吗, 一个 员工只属于一个直线主管和一个虚线主管?如果是的话,你需要在OnModelCreating方法里明确指出他们的关系:

    public class Sample20140902 : DbContext
    
        {
    
            public DbSet<Structure> Structures { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.DirParentStructure).WithMany().HasForeignKey(s => s.DirParentStructureID);
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.InDirParentStructure).WithMany().HasForeignKey(s => s.InDirParentStructureID);
    
            }
    
        }
    
        public class Structure
    
        {
    
            public Guid ID { get; set; }
    
            //[ForeignKey("DirParentStructureID")]
    
            public Guid? DirParentStructureID { get; set; }
    
            public Structure DirParentStructure { get; set; }
    
            //[ForeignKey("InDirParentStructureID")]
    
            public Guid? InDirParentStructureID { get; set; }
    
            public Structure InDirParentStructure { get; set; }
    
        }

    如果是1:1的关系你需要把你的model 改成这样并在OnModelCreating方法里明确指出他们的关系:

    public class Sample20140902 : DbContext
    
        {
    
            public DbSet<Structure> Structures { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
    
            {
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.DirParentStructure).WithOptional().Map(m=>m.MapKey("DirParentStructureID"));
    
                modelBuilder.Entity<Structure>().HasRequired(s => s.InDirParentStructure).WithOptional().Map(m => m.MapKey("InDirParentStructureID"));
    
            }
    
        }
    
        public class Structure
    
        {
    
            public Guid ID { get; set; }
    
            public Structure DirParentStructure { get; set; }
    
            public Structure InDirParentStructure { get; set; }
    
        }

    Regards.



    2014年9月2日 9:12
    版主
  • 非常感谢,依照该思路修改了下代码:

    modelBuilder.Entity<Structure>()
                    .HasOptional(s => s.DirParentStructure)
                    .WithMany()
                    .Map(m => m.MapKey("DirParentStructureID"));

                modelBuilder.Entity<Structure>()
                   .HasOptional(s => s.InDirParentStructure)
                    .WithMany()
                    .Map(m => m.MapKey("InDirParentStructureID"));

    这样就通过了。

    另外,这种方式应该是有Data Anna的方式来写吧?请问下这种方式该如何实现呢?API的方式还是不太形象化。

    2014年9月3日 2:36
  • Hello,

    你的情况有点特殊,是自己引用自己,我试了下发现好像不行。还是建议用fleunt API来配置。

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年9月3日 6:30
    版主