EF Code First關系規則及配置


1、一對多關系

關系表:

Category 分類表

Product 產品表

分類與產品之間的一對多關系

1>、產品實體類不指定外鍵屬性

Domain中類定義:

Category.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ComponentModel.DataAnnotations;
7
8 namespace Northwind.Domain.Entities
9 {
10 public class Category
11 {
12 /// <summary>
13      /// 分類ID
14      /// </summary>
15 public int CategoryID { get; set; }
16
17 /// <summary>
18      /// 分類名稱
19      /// </summary>
20 public string CategoryName { get; set; }
21
22 /// <summary>
23      /// 描述
24      /// </summary>
25 public string Description { get; set; }
26
27 /// <summary>
28      /// 圖片
29      /// </summary>
30 public byte[] Picture { get; set; }
31
32 /// <summary>
33      /// 產品
34      /// </summary>
35 public virtual ICollection<Product> Products { get; set; }
36 }
37 }

Product.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Northwind.Domain.Entities
7 {
8 public class Product
9 {
10 /// <summary>
11      /// 產品ID
12      /// </summary>
13 public int ProductID { get; set; }
14
15 /// <summary>
16      /// 產品名稱
17      /// </summary>
18 public string ProductName { get; set; }
19
20 /// <summary>
21     /// 單價
22      /// </summary>
23 public decimal UnitPrice { get; set; }
24
25 /// <summary>
26     /// 庫存
27      /// </summary>
28 public int UnitsInStock { get; set; }
29
30 /// <summary>
31      /// 是否售完
32      /// </summary>
33 public bool Discontinued { get; set; }
34
35 /// <summary>
36      /// 產品分類
37      /// </summary>
38 public virtual Category Category { get; set; }
39 }
40 }

CategoryMap.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity.ModelConfiguration;
7 using System.ComponentModel.DataAnnotations;
8
9 using Northwind.Domain.Entities;
10
11 namespace Northwind.Domain.Mapping
12 {
13 public class CategoryMap : EntityTypeConfiguration<Category>
14 {
15 public CategoryMap()
16 {
17 this.ToTable("dbo.Category");
18 this.HasKey(t => t.CategoryID);
19
20 this.Property(t => t.CategoryName).IsRequired().HasMaxLength(15);
21 this.Property(t => t.Picture).HasColumnType("image");
22 }
23 }
24 }

ProductMap.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity.ModelConfiguration;
7 using System.ComponentModel.DataAnnotations;
8
9 using Northwind.Domain.Entities;
10
11 namespace Northwind.Domain.Mapping
12 {
13 public class ProductMap : EntityTypeConfiguration<Product>
14 {
15 public ProductMap()
16 {
17 this.ToTable("dbo.Product");
18 this.HasKey(t => t.ProductID);
19
20 this.Property(t => t.ProductName).IsRequired().HasMaxLength(50);
21 this.Property(t => t.UnitPrice).HasPrecision(18, 2);
22 }
23 }
24 }

Data中類定義:

NorthwindContext.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity;
7
8 using Northwind.Domain.Entities;
9 using Northwind.Domain.Mapping;
10
11 namespace Northwind.Data
12 {
13 public class NorthwindContext : DbContext
14 {
15 public DbSet<Category> Categories { get; set; }
16 public DbSet<Product> Products { get; set; }
17
18 protected override void OnModelCreating(DbModelBuilder modelBuilder)
19 {
20 modelBuilder.Configurations.Add(new CategoryMap());
21 modelBuilder.Configurations.Add(new ProductMap());
22 }
23 }
24 }

App中類定義:

Program.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using Northwind.Data;
7 using Northwind.Domain.Entities;
8
9 using System.Data.Entity;
10
11 namespace Northwind.App
12 {
13 class Program
14 {
15 static void Main(string[] args)
16 {
17 // 數據模型改變,刪除數據庫重新創建。
18 Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NorthwindContext>());
19
20 Category c = new Category() { CategoryName = "電子數碼" };
21
22 Product p = new Product() { ProductName = "筆記本電腦", UnitPrice = 4500.00m, Category = c, UnitsInStock = 100, Discontinued = false };
23 using (NorthwindContext db = new NorthwindContext())
24 {
25 db.Categories.Add(c);
26 db.Products.Add(p);
27
28 db.SaveChanges();
29 }
30
31 Console.WriteLine("Finish");
32 Console.ReadKey();
33 }
34 }
35 }

運行之后生成的數據庫結構

2>、產品實體類中指定外鍵屬性

修改Domain中Product.cs代碼:

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Northwind.Domain.Entities
7 {
8 public class Product
9 {
10 /// <summary>
11     /// 產品ID
12      /// </summary>
13 public int ProductID { get; set; }
14
15 /// <summary>
16      /// 產品名稱
17      /// </summary>
18 public string ProductName { get; set; }
19
20 /// <summary>
21      /// 單價
22      /// </summary>
23 public decimal UnitPrice { get; set; }
24
25 /// <summary>
26      /// 庫存
27      /// </summary>
28 public int UnitsInStock { get; set; }
29
30 /// <summary>
31      /// 是否售完
32      /// </summary>
33 public bool Discontinued { get; set; }
34
35 /// <summary>
36      /// 分類ID
37      /// </summary>
38 public int CategoryID { get; set; }
39
40 /// <summary>
41      /// 產品分類
42      /// </summary>
43 public virtual Category Category { get; set; }
44 }
45 }

運行之后生成的數據庫中表結構如下:

  默認的外鍵規則:[Target Type Key Name], [Target Type Name] + [Target Type Key Name], 或者 [Navigation
Property Name] + [Target Type Key Name]。
3>、使用Data Annotations指定外鍵屬性

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.ComponentModel.DataAnnotations;
7
8 namespace Northwind.Domain.Entities
9 {
10 public class Product
11 {
12 /// <summary>
13      /// 產品ID
14      /// </summary>
15 public int ProductID { get; set; }
16
17 /// <summary>
18      /// 產品名稱
19      /// </summary>
20 public string ProductName { get; set; }
21
22 /// <summary>
23      /// 單價
24      /// </summary>
25 public decimal UnitPrice { get; set; }
26
27 /// <summary>
28      /// 庫存
29      /// </summary>
30 public int UnitsInStock { get; set; }
31
32 /// <summary>
33      /// 是否售完
34      /// </summary>
35 public bool Discontinued { get; set; }
36
37 /// <summary>
38      /// 分類ID
39      /// </summary>
40 public int CategoryID { get; set; }
41
42 /// <summary>
43      /// 產品分類
44      /// </summary>
45 [ForeignKey("CategoryID")]
46 public virtual Category Category { get; set; }
47 }
48 }

4>、使用Fluent指定外鍵屬性

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity.ModelConfiguration;
7 using System.ComponentModel.DataAnnotations;
8
9 using Northwind.Domain.Entities;
10
11 namespace Northwind.Domain.Mapping
12 {
13 public class ProductMap : EntityTypeConfiguration<Product>
14 {
15 public ProductMap()
16 {
17 // Primary Key
18 this.HasKey(t => t.ProductID);
19
20 // Properties
21 this.Property(t => t.ProductName).IsRequired().HasMaxLength(50);
22 this.Property(t => t.UnitPrice).HasPrecision(18, 2);
23
24 // Table & Column Mappings
25 this.ToTable("dbo.Product");
26 this.Property(t => t.ProductID).HasColumnName("ProductID");
27 this.Property(t => t.ProductName).HasColumnName("ProductName");
28 this.Property(t => t.UnitPrice).HasColumnName("UnitPrice");
29 this.Property(t => t.UnitsInStock).HasColumnName("UnitsInStock");
30 this.Property(t => t.Discontinued).HasColumnName("Discontinued");
31 this.Property(t => t.CategoryID).HasColumnName("CategoryID");
32
33 // Relationships
34 this.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

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Northwind.Domain.Entities
7 {
8 public class User
9 {
10 /// <summary>
11      /// 用戶ID
12      /// </summary>
13 public int UserID { get; set; }
14
15 /// <summary>
16      /// 用戶名
17      /// </summary>
18 public string UserName { get; set; }
19
20 /// <summary>
21      /// 密碼
22      /// </summary>
23 public string Password { get; set; }
24
25 /// <summary>
26      /// 角色
27      /// </summary>
28 public ICollection<Role> Roles { get; set; }
29 }
30 }

Role.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Northwind.Domain.Entities
7 {
8 public class Role
9 {
10 /// <summary>
11      /// 角色ID
12      /// </summary>
13 public int RoleID { get; set; }
14
15 /// <summary>
16      /// 角色名稱
17      /// </summary>
18 public string RoleName { get; set; }
19
20 /// <summary>
21      /// 用戶
22      /// </summary>
23 public virtual ICollection<User> Users { get; set; }
24 }
25 }

UserMap.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity.ModelConfiguration;
7 using System.ComponentModel.DataAnnotations;
8
9 using Northwind.Domain.Entities;
10
11 namespace Northwind.Domain.Mapping
12 {
13 public class UserMap : EntityTypeConfiguration<User>
14 {
15 public UserMap()
16 {
17 // Primary Key
18 this.HasKey(t => t.UserID);
19
20 // Properties
21 this.Property(t => t.UserName).IsRequired().HasMaxLength(50);
22 this.Property(t => t.Password).IsRequired().HasMaxLength(50);
23
24 // Table & Column Mappings
25 this.ToTable("dbo.User");
26 this.Property(t => t.UserID).HasColumnName("UserID");
27 this.Property(t => t.UserName).HasColumnName("UserName");
28 this.Property(t => t.Password).HasColumnName("Password");
29
30 // Relationships
31 this.HasMany(t => t.Roles)
32 .WithMany(t => t.Users)
33 .Map(t => t.MapLeftKey("RoleID").MapRightKey("UserID"));
34 }
35 }
36 }

RoleMap.cs

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Data.Entity.ModelConfiguration;
7 using System.ComponentModel.DataAnnotations;
8
9 using Northwind.Domain.Entities;
10
11 namespace Northwind.Domain.Mapping
12 {
13 public class RoleMap : EntityTypeConfiguration<Role>
14 {
15 public RoleMap()
16 {
17 // Primary Key
18 this.HasKey(t => t.RoleID);
19
20 // Properties
21 this.Property(t => t.RoleName).IsRequired().HasMaxLength(50);
22
23 // Table & Column Mappings
24 this.ToTable("dbo.Role");
25 this.Property(t => t.RoleID).HasColumnName("RoleID");
26 this.Property(t => t.RoleName).HasColumnName("RoleName");
27
28 // Relationships
29 this.HasMany(t => t.Users)
30 .WithMany(t => t.Roles)
31 .Map(t => t.MapLeftKey("UserID").MapRightKey("RoleID"));
32 }
33 }
34 }

Data中NorthwindContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;

using Northwind.Domain.Entities;
using Northwind.Domain.Mapping;

namespace Northwind.Data
{
public class NorthwindContext : DbContext
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
modelBuilder.Configurations.Add(new ProductMap());
modelBuilder.Configurations.Add(new SupplierMap());
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new RoleMap());
}
}
}

運行成功后生成的數據表:

三、一對一關系

待補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM