自反关联的EFCore实现注意事项

这样一个类图,在映射到代码的时候(使用EFCore),如何解决1:1的问题。

自反关联的EFCore实现注意事项_第1张图片

直接映射出代码,编译会出错的,需要在OnModelCreating,这样:        
modelBuilder.Entity()
            .HasOne(x => x.Below)
            .WithOne(y => y.Upper);

否则报错:

The dependent side could not be determined for the one-to-one relationship between 'Floor.Below' and 'Floor.Upper'. To identify the dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship, configure them independently via separate method chains in 'OnModelCreating'. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.

类实现:

    public class Floor
    {
        [Key]
        public int Id { get; set; }

        /// 
        /// 层名
        /// 
        public string Name { get; set; }

        /// 
        /// 层高
        /// 
        public double Height { get; set; }

        /// 
        /// 所属建筑
        /// 
        public Building Building { get; set; }

        /// 
        /// 楼层类型
        /// 
        public FloorType Type { get; set; }

        /// 
        /// 包含的结构体
        /// 
        public List Structures { get; set;}

        /// 
        /// 下一层
        /// 
        [ForeignKey("Id")]
        public int BelowId { get; set; }

        /// 
        /// 下一层
        /// 
        public Floor Below { get; set; }

        /// 
        /// 上一层
        /// 
        [ForeignKey("Id")]
        public int UpperId { get; set; }

        /// 
        /// 上一层
        /// 

        public Floor Upper { get; set; }
    }

 

你可能感兴趣的:(ASP.NET,Core,Blazor,C#,UML,html,前端)