Entity Framework4.0 (三)概述(EF4 的Code First方法)
EF4支持三种构建方法:1. Database First方法。2.Model First方法。3.Code First 方法。开发人员可根据具体的项目情况,选择任一种方法。
上次快速演示了Model First的方法。这次演示Code First方法。
EF4.0引入CTP4可支持Code First了。Microsoft在EF4.1中引入EntityFramework4.1/4.2加强Code First的支持。(EF4.1的DataAnnotation, Fluent API 更完善,对POCO的支持更直接方便。关于EF4.1/4.2的使用,我在后面博文中我会补上的。)
在此,给出EF4.0的使用方法以快速演示:
首先,我们要下载并安装CTP4:URL http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=8812。
当安装完成CTP4后。(安装CTP4之前最好把Visual Studio2010关掉。)
1. 启动Visual Studio 2010.
2. 创建一个Console Application : 名称:EFCodeFirstWalkthrough
3. 在解决方案上右键,选择:添加一个类库项目:名称:EFCodeFirstWalkthroughModel。
4. 右键EFCodeFirstWalkthrough项目,添加.NET类库引用:Microsoft.Data.Entity.CTP; 添加.NET类库引用:System.Data.Entity.添加项目引用:EFCodeFirstWalkthroughModel.
5.在类库项目中添加类文件:Author.cs,Book.cs,Person.cs,Publisher.cs:代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EFCodeFirstWalkthroughModel
7 {
8 public class Author : Person
9 {
10 public int AuthorId { get; set; }
11 public virtual ICollection<Book> Books { get; set; }
12 }
13 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EFCodeFirstWalkthroughModel
7 {
8 public class Book
9 {
10 public string ISBN { get; set; }
11 public string Title { get; set; }
12 public DateTime FirstPublished { get; set; }
13 public bool IsFiction { get; set; }
14 public virtual Publisher Publisher { get; set; }
15 public virtual Author Author { get; set; }
16 }
17 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EFCodeFirstWalkthroughModel
7 {
8 public class Person
9 {
10 public int PersonId { get; set; }
11 public string FirstName { get; set; }
12 public string LastName { get; set; }
13 }
14 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace EFCodeFirstWalkthroughModel
7 {
8 public class Publisher
9 {
10 public int PublisherId { get; set; }
11 public string Name { get; set; }
12 public virtual ICollection<Book> Books { get; set; }
13 }
14
15 }
6.在Cosole 项目EFCodeFirstWalkthrough 中添加类文件:BookCatalog.cs,BookConfiguration.cs ,并修改Program.cs 的代码。如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using EFCodeFirstWalkthroughModel;
6 using System.Data.Objects;
7 using System.Data.EntityClient;
8
9
10 namespace EFCodeFirstWalkthrough
11 {
12 public class BookCatalog : ObjectContext
13 {
14 private ObjectSet<Book> _books;
15 private ObjectSet<Person> _people;
16 private ObjectSet<Publisher> _publishers;
17
18 public BookCatalog(EntityConnection connection)
19 : base(connection)
20 { }
21
22 public ObjectSet<Book> Books
23 {
24 get
25 {
26 return this._books == null
27 ? this._books = this.CreateObjectSet<Book>()
28 : this._books;
29 }
30 }
31
32 public ObjectSet<Person> People
33 {
34 get
35 {
36 return this._people == null
37 ? this._people = this.CreateObjectSet<Person>()
38 : this._people;
39 }
40 }
41
42 public ObjectSet<Publisher> Publishers
43 {
44
45 get
46 {
47 return this._publishers == null
48 ? this._publishers = this.CreateObjectSet<Publisher>()
49 : this._publishers;
50 }
51 }
52 }
53 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.Entity.ModelConfiguration;
6
7 namespace EFCodeFirstWalkthroughModel
8 {
9 public class BookConfiguration : EntityConfiguration<Book>
10 {
11 public BookConfiguration()
12 {
13 this.HasKey(b => b.ISBN);
14 this.Property(b => b.Title).IsRequired();
15 this.HasRequired(b => b.Author).WithMany(a => a.Books);
16 }
17 }
18 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using EFCodeFirstWalkthroughModel;
6 using System.Data.Entity.ModelConfiguration;
7 using System.Data.SqlClient;
8
9 namespace EFCodeFirstWalkthrough
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 var builder = new ModelBuilder();
16 builder.Configurations.Add(new BookConfiguration());
17 builder.Entity<Person>();
18 builder.Entity<Publisher>().Property(p => p.Name).IsRequired().HasMaxLength(50);
19 var model = builder.CreateModel();
20 using (var connection = new SqlConnection(@"Server=.\SQLEXPRESS;Database=CodeFirstWalkthrough;Trusted_Connection=True;"))
21 {
22 using (var context = model.CreateObjectContext<BookCatalog>(connection))
23 {
24 if (!context.DatabaseExists())
25 {
26 context.CreateDatabase();
27 }
28 var book = new Book
29 {
30 ISBN = "1111",
31 Title = "Intro to Code First",
32 FirstPublished = DateTime.Today,
33 IsFiction = false,
34 Author = new Author { FirstName = "Rowan", LastName = "Miller" },
35 Publisher = new Publisher { Name = "EF Books" }
36 };
37 context.Books.AddObject(book);
38 context.SaveChanges();
39 }
40 }
41 }
42 }
43 }
编译项目,执行。
然后观察数据库:会发现生成一个名为CodeFirstWalkthrough的数据库文件。并且在里面有数据表生成。
注意:在此项目演示过程中,我们并未指定SqlConnection所用的连接字符串,但是EF4会利用我们指定给SqlConnection的参数去生连接特定的数据库服务器,并生成数据库。
好了,Code First 先到这里吧。休息咯!!!