https://www.cnblogs.com/wangsea/articles/10394410.html
转载地址:http://www.cnblogs.com/libingql/archive/2012/03/26/2418588.html
一、EF Code First默认规则(Convention)
1、表及列默认规则
EF Code First默认生成的表名为类名的复数形式,表的生成为dbo用户,列名与实体类属性名称相同。
2、主键约束
实体类中属性名为Id或[类名]Id,将作为生成表的主键。若主键为int类型,则默认为Sql Server的Identity类型。
3、字符类型属性
实体类中string类型的属性,在生成表时,对应Sql Server中nvarchar(max)类型。
4、Byte Array类型约束
实体类中byte[]类型的属性,生成表时对应Sql Server中varbinary(max)类型。
5、Boolean类型约束
实体类中bool类型的属性,在生成表是对应Sql Server中bit类型。
二、配置约束
配置约束的两种方式:attribute-based Data Annotations和strongly typed Fluent API。
1、使用Data Annotations配置
Data Annotations使用命名空间System.ComponentModel.DataAnnotations。
在上一节示例代码中Domain添加EntityFramework引用,修改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 [Key]
16 public int CategoryID { get; set; }
17
18 /// <summary>
19 /// 分类名称
20 /// </summary>
21 [Required]
22 [MaxLength(15)]
23 public string CategoryName { get; set; }
24
25 /// <summary>
26 /// 描述
27 /// </summary>
28 public string Description { get; set; }
29
30 /// <summary>
31 /// 图片
32 /// </summary>
33 [Column(TypeName="image")]
34 public byte[] Picture { get; set; }
35 }
36 }
修改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 using (NorthwindContext db = new NorthwindContext())
23 {
24 db.Categories.Add(c);
25 db.SaveChanges();
26 }
27
28 Console.WriteLine("Finish");
29 Console.ReadKey();
30 }
31 }
32 }
运行成功后生成的数据库如下:
2、使用Fluent API配置
Fluent API通过重写DbContext.OnModelCreating方法实现修改Code First默认约束。
修改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 }
修改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
10 namespace Northwind.Data
11 {
12 public class NorthwindContext : DbContext
13 {
14 public DbSet<Category> Categories { get; set; }
15
16 protected override void OnModelCreating(DbModelBuilder modelBuilder)
17 {
18 modelBuilder.Entity<Category>().ToTable("Category");
19 //modelBuilder.Entity<Category>().ToTable("dbo.Category"); // 指定用户
20
21 modelBuilder.Entity<Category>().HasKey(t => t.CategoryID);
22 modelBuilder.Entity<Category>().Property(t => t.CategoryName).IsRequired().HasMaxLength(15);
23 modelBuilder.Entity<Category>().Property(t => t.Picture).HasColumnType("image");
24 }
25 }
26 }
执行成功后生成的数据:
Fluent API重写调整解决方案结构,在Domain中添加Mapping文件夹:
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 }
Domain中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.CategoryID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); // 取消主键Identity
21 this.Property(t => t.CategoryName).IsRequired().HasMaxLength(15);
22 this.Property(t => t.Picture).HasColumnType("image");
23 }
24 }
25 }
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
17 protected override void OnModelCreating(DbModelBuilder modelBuilder)
18 {
19 modelBuilder.Configurations.Add(new CategoryMap());
20 }
21 }
22 }
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() { CategoryID = 1, CategoryName = "电子数码" };
21
22 using (NorthwindContext db = new NorthwindContext())
23 {
24 db.Categories.Add(c);
25 db.SaveChanges();
26 }
27
28 Console.WriteLine("Finish");
29 Console.ReadKey();
30 }
31 }
32 }
以上内容为EF Code First基本的默认规则及简单配置,下一节我们将进行详细的深入EF Code First规则。