EF Code First关系规则及配置.docx
《EF Code First关系规则及配置.docx》由会员分享,可在线阅读,更多相关《EF Code First关系规则及配置.docx(15页珍藏版)》请在冰豆网上搜索。
![EF Code First关系规则及配置.docx](https://file1.bdocx.com/fileroot1/2022-12/15/c3a2f53c-105d-4560-b197-c5f342ac3f98/c3a2f53c-105d-4560-b197-c5f342ac3f981.gif)
EFCodeFirst关系规则及配置
1、一对多关系
关系表:
Category分类表
Product产品表
分类与产品之间的一对多关系
1>、产品实体类不指定外键属性
Domain中类定义:
Category.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.ComponentModel.DataAnnotations;
7
8namespaceNorthwind.Domain.Entities
9{
10publicclassCategory
11{
12///
13 ///分类ID
14 ///
15publicintCategoryID{get;set;}
16
17///
18 ///分类名称
19 ///
20publicstringCategoryName{get;set;}
21
22///
23 ///描述
24 ///
25publicstringDescription{get;set;}
26
27///
28 ///图片
29 ///
30publicbyte[]Picture{get;set;}
31
32///
33 ///产品
34 ///
35publicvirtualICollectionProducts{get;set;}
36}
37}
Product.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6namespaceNorthwind.Domain.Entities
7{
8publicclassProduct
9{
10///
11 ///产品ID
12 ///
13publicintProductID{get;set;}
14
15///
16 ///产品名称
17 ///
18publicstringProductName{get;set;}
19
20///
21 ///单价
22 ///
23publicdecimalUnitPrice{get;set;}
24
25///
26 ///库存
27 ///
28publicintUnitsInStock{get;set;}
29
30///
31 ///是否售完
32 ///
33publicboolDiscontinued{get;set;}
34
35///
36 ///产品分类
37 ///
38publicvirtualCategoryCategory{get;set;}
39}
40}
CategoryMap.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.Data.Entity.ModelConfiguration;
7usingSystem.ComponentModel.DataAnnotations;
8
9usingNorthwind.Domain.Entities;
10
11namespaceNorthwind.Domain.Mapping
12{
13publicclassCategoryMap:
EntityTypeConfiguration
14{
15publicCategoryMap()
16{
17this.ToTable("dbo.Category");
18this.HasKey(t=>t.CategoryID);
19
20this.Property(t=>t.CategoryName).IsRequired().HasMaxLength(15);
21this.Property(t=>t.Picture).HasColumnType("image");
22}
23}
24}
ProductMap.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.Data.Entity.ModelConfiguration;
7usingSystem.ComponentModel.DataAnnotations;
8
9usingNorthwind.Domain.Entities;
10
11namespaceNorthwind.Domain.Mapping
12{
13publicclassProductMap:
EntityTypeConfiguration
14{
15publicProductMap()
16{
17this.ToTable("dbo.Product");
18this.HasKey(t=>t.ProductID);
19
20this.Property(t=>t.ProductName).IsRequired().HasMaxLength(50);
21this.Property(t=>t.UnitPrice).HasPrecision(18,2);
22}
23}
24}
Data中类定义:
NorthwindContext.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.Data.Entity;
7
8usingNorthwind.Domain.Entities;
9usingNorthwind.Domain.Mapping;
10
11namespaceNorthwind.Data
12{
13publicclassNorthwindContext:
DbContext
14{
15publicDbSetCategories{get;set;}
16publicDbSetProducts{get;set;}
17
18protectedoverridevoidOnModelCreating(DbModelBuildermodelBuilder)
19{
20modelBuilder.Configurations.Add(newCategoryMap());
21modelBuilder.Configurations.Add(newProductMap());
22}
23}
24}
App中类定义:
Program.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingNorthwind.Data;
7usingNorthwind.Domain.Entities;
8
9usingSystem.Data.Entity;
10
11namespaceNorthwind.App
12{
13classProgram
14{
15staticvoidMain(string[]args)
16{
17//数据模型改变,删除数据库重新创建。
18Database.SetInitializer(newDropCreateDatabaseIfModelChanges());
19
20Categoryc=newCategory(){CategoryName="电子数码"};
21
22Productp=newProduct(){ProductName="笔记本电脑",UnitPrice=4500.00m,Category=c,UnitsInStock=100,Discontinued=false};
23using(NorthwindContextdb=newNorthwindContext())
24{
25db.Categories.Add(c);
26db.Products.Add(p);
27
28db.SaveChanges();
29}
30
31Console.WriteLine("Finish");
32Console.ReadKey();
33}
34}
35}
运行之后生成的数据库结构
2>、产品实体类中指定外键属性
修改Domain中Product.cs代码:
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6namespaceNorthwind.Domain.Entities
7{
8publicclassProduct
9{
10///
11 ///产品ID
12 ///
13publicintProductID{get;set;}
14
15///
16 ///产品名称
17 ///
18publicstringProductName{get;set;}
19
20///
21 ///单价
22 ///
23publicdecimalUnitPrice{get;set;}
24
25///
26 ///库存
27 ///
28publicintUnitsInStock{get;set;}
29
30///
31 ///是否售完
32 ///
33publicboolDiscontinued{get;set;}
34
35///
36 ///分类ID
37 ///
38publicintCategoryID{get;set;}
39
40///
41 ///产品分类
42 ///
43publicvirtualCategoryCategory{get;set;}
44}
45}
运行之后生成的数据库中表结构如下:
默认的外键规则:
[TargetTypeKeyName],[TargetTypeName]+[TargetTypeKeyName],或者[Navigation
PropertyName]+[TargetTypeKeyName]。
3>、使用DataAnnotations指定外键属性
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.ComponentModel.DataAnnotations;
7
8namespaceNorthwind.Domain.Entities
9{
10publicclassProduct
11{
12///
13 ///产品ID
14 ///
15publicintProductID{get;set;}
16
17///
18 ///产品名称
19 ///
20publicstringProductName{get;set;}
21
22///
23 ///单价
24 ///
25publicdecimalUnitPrice{get;set;}
26
27///
28 ///库存
29 ///
30publicintUnitsInStock{get;set;}
31
32///
33 ///是否售完
34 ///
35publicboolDiscontinued{get;set;}
36
37///
38 ///分类ID
39 ///
40publicintCategoryID{get;set;}
41
42///
43 ///产品分类
44 ///
45[ForeignKey("CategoryID")]
46publicvirtualCategoryCategory{get;set;}
47}
48}
4>、使用Fluent指定外键属性
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.Data.Entity.ModelConfiguration;
7usingSystem.ComponentModel.DataAnnotations;
8
9usingNorthwind.Domain.Entities;
10
11namespaceNorthwind.Domain.Mapping
12{
13publicclassProductMap:
EntityTypeConfiguration
14{
15publicProductMap()
16{
17//PrimaryKey
18this.HasKey(t=>t.ProductID);
19
20//Properties
21this.Property(t=>t.ProductName).IsRequired().HasMaxLength(50);
22this.Property(t=>t.UnitPrice).HasPrecision(18,2);
23
24//Table&ColumnMappings
25this.ToTable("dbo.Product");
26this.Property(t=>t.ProductID).HasColumnName("ProductID");
27this.Property(t=>t.ProductName).HasColumnName("ProductName");
28this.Property(t=>t.UnitPrice).HasColumnName("UnitPrice");
29this.Property(t=>t.UnitsInStock).HasColumnName("UnitsInStock");
30this.Property(t=>t.Discontinued).HasColumnName("Discontinued");
31this.Property(t=>t.CategoryID).HasColumnName("CategoryID");
32
33//Relationships
34this.HasRequired(t=>t.Category)
35.WithMany(t=>t.Products)
36.HasForeignKey(t=>t.CategoryID)
37.WillCascadeOnDelete(false);
38}
39}
40}
5>、示例代码附件
以上示例代码附件,并补充Product与Category及Supplier的两个外键关联。
Northwind-一对多外键.rar
二、多对多关系
表说明:
用户表:
User
角色表:
Role
用户与角色多对多,一个用户可以属于多个角色,一个角色可以有多个用户。
Domain中User.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6namespaceNorthwind.Domain.Entities
7{
8publicclassUser
9{
10///
11 ///用户ID
12 ///
13publicintUserID{get;set;}
14
15///
16 ///用户名
17 ///
18publicstringUserName{get;set;}
19
20///
21 ///密码
22 ///
23publicstringPassword{get;set;}
24
25///
26 ///角色
27 ///
28publicICollectionRoles{get;set;}
29}
30}
Role.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6namespaceNorthwind.Domain.Entities
7{
8publicclassRole
9{
10///
11 ///角色ID
12 ///
13publicintRoleID{get;set;}
14
15///
16 ///角色名称
17 ///
18publicstringRoleName{get;set;}
19
20///
21 ///用户
22 ///
23publicvirtualICollectionUsers{get;set;}
24}
25}
UserMap.cs
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6usingSystem.Data.Entity.ModelConfiguration;
7usingSystem.ComponentModel.DataAnnotations;
8
9usingNorthwind.Domain.Entities;
10
11namespaceNorthwind.Domain.Mapping
12{
13publicclassUserMap:
EntityTypeConfiguration
14{
15publicUserMap()
16{
17//PrimaryKey
18this.HasKey(t=>t.UserID);
19
20//Properties
21this.Property(t=>t.UserName).IsRequired().HasMaxLength(50);
22this.Property(t=>t.Password).IsRequired().HasMaxLength