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:代码如下:

Author
 1 Author

 2  using System;

 3  using System.Collections.Generic;

 4  using System.Linq;

 5  using System.Text;

 6  

 7  namespace EFCodeFirstWalkthroughModel

 8  {

 9      public class Author : Person

10      {   

11          public int AuthorId { get; set; }

12          public virtual ICollection<Book> Books { get; set; }

13      }

14  }
Book
 1 Book

 2  using System;

 3  using System.Collections.Generic;

 4  using System.Linq;

 5  using System.Text;

 6  

 7  namespace EFCodeFirstWalkthroughModel

 8  {

 9      public class Book

10      {

11          public string ISBN { get; set; }

12          public string Title { get; set; }

13          public DateTime FirstPublished { get; set; }

14          public bool IsFiction { get; set; }

15          public virtual Publisher Publisher { get; set; }

16          public virtual Author Author { get; set; }

17      }    

18  }
Person
 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Text;

 

 namespace EFCodeFirstWalkthroughModel

 {

     public class Person

     {

         public int PersonId { get; set; }

         public string FirstName { get; set; }

         public string LastName { get; set; }

     }

 }
Publisher
 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Text;

 

 namespace EFCodeFirstWalkthroughModel

 {

     public class Publisher

     {

         public int PublisherId { get; set; }

         public string Name { get; set; }

         public virtual ICollection<Book> Books { get; set; }

     }

 

 }
 
6.在Cosole 项目EFCodeFirstWalkthrough 中添加类文件:BookCatalog.cs,BookConfiguration.cs ,并修改Program.cs 的代码。如下:
 
BookCatalog
 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Text;

 using EFCodeFirstWalkthroughModel;

 using System.Data.Objects;

 using System.Data.EntityClient;

 

 

 namespace EFCodeFirstWalkthrough

 {

     public class BookCatalog : ObjectContext

     {

         private ObjectSet<Book> _books;

         private ObjectSet<Person> _people;

         private ObjectSet<Publisher> _publishers;

 

         public BookCatalog(EntityConnection connection)

             : base(connection)

         { }

 

         public ObjectSet<Book> Books

         {

             get

             {

                 return this._books == null

                     ? this._books = this.CreateObjectSet<Book>()

                     : this._books;

             }

         }

         

         public ObjectSet<Person> People     

         {

             get

             {

                 return this._people == null

                     ? this._people = this.CreateObjectSet<Person>()

                     : this._people;

             }

         }  

 

         public ObjectSet<Publisher> Publishers

         {

 

             get

             {

                 return this._publishers == null

                     ? this._publishers = this.CreateObjectSet<Publisher>()

                     : this._publishers;

             }

         }

     }

 }
BookConfiguration
 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Text;

 using System.Data.Entity.ModelConfiguration;

 

 namespace EFCodeFirstWalkthroughModel

 {

     public class BookConfiguration : EntityConfiguration<Book>

     {

         public BookConfiguration()

         {

             this.HasKey(b => b.ISBN);

             this.Property(b => b.Title).IsRequired();

             this.HasRequired(b => b.Author).WithMany(a => a.Books);

         }

     }

 }
Program
 using System;

 using System.Collections.Generic;

 using System.Linq;

 using System.Text;

 using EFCodeFirstWalkthroughModel;

 using System.Data.Entity.ModelConfiguration;

 using System.Data.SqlClient;

 

 namespace EFCodeFirstWalkthrough

 {

     class Program

     {

         static void Main(string[] args)

         {

             var builder = new ModelBuilder();

             builder.Configurations.Add(new BookConfiguration());

             builder.Entity<Person>();

             builder.Entity<Publisher>().Property(p => p.Name).IsRequired().HasMaxLength(50);

             var model = builder.CreateModel();

             using (var connection = new SqlConnection(@"Server=.\SQLEXPRESS;Database=CodeFirstWalkthrough;Trusted_Connection=True;"))

             {

                 using (var context = model.CreateObjectContext<BookCatalog>(connection))

                 {

                     if (!context.DatabaseExists())

                     {

                         context.CreateDatabase();

                     }

                     var book = new Book

                     {

                         ISBN = "1111",

                         Title = "Intro to Code First",

                         FirstPublished = DateTime.Today,

                         IsFiction = false,

                         Author = new Author { FirstName = "Rowan", LastName = "Miller" },

                         Publisher = new Publisher { Name = "EF Books" }

                     };

                     context.Books.AddObject(book);

                     context.SaveChanges();

                 }

             }

         }

     }

 }
 
编译项目,执行。

然后观察数据库:会发现生成一个名为CodeFirstWalkthrough的数据库文件。并且在里面有数据表生成。

注意:在此项目演示过程中,我们并未指定SqlConnection所用的连接字符串,但是EF4会利用我们指定给SqlConnection的参数去生连接特定的数据库服务器,并生成数据库。

好了,Code First 先到这里吧。休息咯!!!

你可能感兴趣的:(framework)