自连接<EntityFramework6.0>

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

自引用publicclassPictureCategory

{

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]publicintCategoryId {get;PRivateset; }publicstringName {get;set; }publicint? ParentCategoryId {get;privateset; }publicvirtualPictureCategory ParentCategory {get;set; }publicvirtualICollection<PictureCategory> SubPictureCategories {get;set; }publicPictureCategory()

{

SubPictureCategories=newHashSet<PictureCategory>();

}

}publicclassPictureCategoryContext : DbContext

{publicvirtualDbSet<PictureCategory> PictureCategories {get;set; }publicPictureCategoryContext() :base("name=DemoContext") { }protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder)

{base.OnModelCreating(modelBuilder);

modelBuilder.Entity<PictureCategory>()

.HasKey(p=>p.CategoryId)

.HasMany(p=>p.SubPictureCategories)

.WithOptional(t=>t.ParentCategory)

.HasForeignKey(t=>t.ParentCategoryId);

}

}

怎么使用?privatestaticvoidMain()

{using(varcontext =newPictureCategoryContext())

{var_1st =newPictureCategory { Name ="第1代"};var_2st =newPictureCategory { Name ="第2代"};var_3_1st =newPictureCategory { Name ="第3_1代"};var_3_2st =newPictureCategory { Name ="第3_2代"};var_3_3st =newPictureCategory { Name ="第3_3代"};var_4st =newPictureCategory { Name ="第4代"};var_5_1st =newPictureCategory { Name ="第5_5_1代"};var_5_2st =newPictureCategory { Name ="第5_2代"};

_1st.SubPictureCategories=newList<PictureCategory>{ _2st };

_2st.SubPictureCategories=newList<PictureCategory>{ _3_1st, _3_2st, _3_3st };

_3_3st.SubPictureCategories=newList<PictureCategory>{ _4st };

_4st.SubPictureCategories=newList<PictureCategory>{ _5_1st, _5_2st };

context.PictureCategories.Add(_1st);

context.SaveChanges();

}using(varcontext=newPictureCategoryContext())

{varquery = context.PictureCategories.Where(p=>p.ParentCategory==null).ToList();

query.ForEach(t=> Print(t,1));

}

Console.ReadKey();

}privatestaticvoidPrint(PictureCategory category,intlevel)

{

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航