EFCodeFirst-Net

来源:互联网 发布:linux开源项目 编辑:程序博客网 时间:2024/06/05 11:28

EFCodeFirst-Net

开发工具:VS2015+SQL2008R2

解决方案:

 

 

运行后数据库结构自动创建如下:

 

项目:CodeFirstDemo
类:Program

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data.Entity;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Model;  
  8. using DataAccess;  
  9. namespace CodeFirstDemo  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.   
  16.             //如果实体类有变化,那么就重新生成一下数据库(慎用,表会被清空)  
  17.             //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataAccess.DemoContext>());  
  18.   
  19.             //将此下行代码放如下主法中: protected void Application_Start() ;(推荐使用)  
  20.             Database.SetInitializer<DemoContext>(null);  
  21.   
  22.             using (var db = new DemoContext())  
  23.             {  
  24.                 Order order = new Order();  
  25.                 order.OrderID = Guid.NewGuid();  
  26.                 order.OrderName2 = "X";  
  27.                 order.Items = new List<Item> { new Item {  
  28.                      ItemID=Guid.NewGuid(),  
  29.                       Product=new Product {  
  30.                            ProductID=Guid.NewGuid(),  
  31.                            ProductName="A"  
  32.                       }  
  33.   
  34.                 } };  
  35.   
  36.                 db.Orders.Add(order);  
  37.                 db.SaveChanges();  
  38.   
  39.             }  
  40.             Console.Write("ok");  
  41.   
  42.             Console.ReadKey();  
  43.   
  44.         }  
  45.     }  
  46. }  

App.config

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <configuration>  
  3.   <startup>  
  4.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />  
  5.   </startup>  
  6.   <connectionStrings>  
  7.     <add name="DemoContext" connectionString="data source=.;initial catalog=test2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  
  8.   </connectionStrings>  
  9. </configuration>  

项目:DataAccess(可添加一个空的EFCodeFirst,自动引用必须的程序集)
类:DemoContext

  1. namespace DataAccess  
  2. {  
  3.     using Model;  
  4.     using System;  
  5.     using System.Data.Entity;  
  6.     using System.Linq;  
  7.   
  8.     public class DemoContext : DbContext  
  9.     {  
  10.         //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)  
  11.         //使用“Model1”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的  
  12.         //“DataAccess.Model1”数据库。  
  13.         //   
  14.         //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“Model1”  
  15.         //连接字符串。  
  16.         public DemoContext()  
  17.             : base("name=DemoContext")  
  18.         {  
  19.         }  
  20.   
  21.         //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型  
  22.         //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。  
  23.   
  24.         public virtual DbSet<Order> Orders { getset; }  
  25.         public virtual DbSet<Product> Products { getset; }  
  26.         public virtual DbSet<Item> Items { getset; }  
  27.   
  28.     }  
  29.   
  30.   
  31. }  

项目:Model
类:Order

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Model  
  8. {  
  9.     public class Order  
  10.     {  
  11.         public Guid OrderID { getset; }  
  12.   
  13.         public string OrderName { getset; }  
  14.   
  15.         public string OrderName2 { getset; }  
  16.   
  17.         public List<Item>Items { getset; }  
  18.   
  19.     }  
  20. }  
  21.  

类:Product

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Model  
  8. {  
  9.     public class Product  
  10.     {  
  11.   
  12.         public Guid ProductID { getset; }  
  13.         public string ProductName { getset; }  
  14.   
  15.     }  
  16. }  

类:Item

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Model  
  8. {  
  9.     public class Item  
  10.     {  
  11.         public Guid ItemID { getset; }  
  12.   
  13.         public Product Product { getset; }  
  14.   
  15.   
  16.     }  
  17. }  
0 0