Fork me on GitHub

自连接<EntityFramework6.0>

 自引用

复制代码
 public class PictureCategory
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CategoryId { get; private set; }
        public string Name { get; set; }
        public int? ParentCategoryId { get; private set; }
        public virtual PictureCategory ParentCategory { get; set; }
        public virtual ICollection<PictureCategory> SubPictureCategories { get; set; }

        public PictureCategory()
        {
            SubPictureCategories = new HashSet<PictureCategory>();
        }
    }

    public class PictureCategoryContext : DbContext
    {
        public virtual DbSet<PictureCategory> PictureCategories { get; set; }
        public PictureCategoryContext() : base("name=DemoContext") { }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<PictureCategory>()
                .HasKey(p=>p.CategoryId)
                .HasMany(p => p.SubPictureCategories)
                .WithOptional(t => t.ParentCategory)
                .HasForeignKey(t=>t.ParentCategoryId);
        }
    }
复制代码

怎么使用?

复制代码
private static void Main()
        {
            using (var context = new PictureCategoryContext())
            {
                var _1st = new PictureCategory { Name = "第1代" };
                var _2st = new PictureCategory { Name = "第2代" };
                var _3_1st = new PictureCategory { Name = "第3_1代" };
                var _3_2st = new PictureCategory { Name = "第3_2代" };
                var _3_3st = new PictureCategory { Name = "第3_3代" };
                var _4st = new PictureCategory { Name = "第4代" };
                var _5_1st = new PictureCategory { Name = "第5_5_1代" };
                var _5_2st = new PictureCategory { Name = "第5_2代" };
                _1st.SubPictureCategories = new List<PictureCategory> { _2st };
                _2st.SubPictureCategories = new List<PictureCategory> { _3_1st, _3_2st, _3_3st };
                _3_3st.SubPictureCategories = new List<PictureCategory> { _4st };
                _4st.SubPictureCategories = new List<PictureCategory> { _5_1st, _5_2st };
                context.PictureCategories.Add(_1st);
                context.SaveChanges();
            }

            using (var context=new PictureCategoryContext())
            {
               var query = context.PictureCategories.Where(p=>p.ParentCategory==null).ToList();
               query.ForEach(t => Print(t,1));
            }

            Console.ReadKey();
        }

        private static void Print(PictureCategory category, int level)
        {
            Console.WriteLine("{0}--{1}", category.Name, level);
            category.SubPictureCategories.ToList().ForEach(t=>Print(t,level+1));
        }
复制代码

效果:

模型如下:

 

 

再次我们分析一下该关系模型所涉及到degree, multiplicity, and direction:
degree【度】:  一元                   

multiplicity【复合度,在UML中很常见,也就是重复度】:  0..1和*;因为一个Parent有N个children,而每一个child只能有1个Parent

direction【流向】:   双向

        

这三个术语详细的介绍看这里

Database relationships are characterized by degree, multiplicity, and direction. Degreeis the number of entity types that participate in the relationship. Unary and binary relationships are the most common. Tertiary and n-place relationships are more theoretical than practical.
Multiplicityis the number of entity types on each end of the relationship. You have seen the multiplicities 0..1 (zero or 1), 1 (one), and * (many).
Finally, the directionis either one-way or bidirectional.
The Entity Data Model supports a particular kind of database relationship called an Association Type. 
An Association Type relationship has either unary or binary degree, multiplicities 0..1, 1, or *, and a direction that is bidirectional

posted @   Halower  阅读(933)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示