EFCodeFirst-Net
来源:互联网 发布:linux开源项目 编辑:程序博客网 时间:2024/06/05 11:28
EFCodeFirst-Net
开发工具:VS2015+SQL2008R2
解决方案:
运行后数据库结构自动创建如下:
项目:CodeFirstDemo
类:Program
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Model;
- using DataAccess;
- namespace CodeFirstDemo
- {
- class Program
- {
- static void Main(string[] args)
- {
- //如果实体类有变化,那么就重新生成一下数据库(慎用,表会被清空)
- //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataAccess.DemoContext>());
- //将此下行代码放如下主法中: protected void Application_Start() ;(推荐使用)
- Database.SetInitializer<DemoContext>(null);
- using (var db = new DemoContext())
- {
- Order order = new Order();
- order.OrderID = Guid.NewGuid();
- order.OrderName2 = "X";
- order.Items = new List<Item> { new Item {
- ItemID=Guid.NewGuid(),
- Product=new Product {
- ProductID=Guid.NewGuid(),
- ProductName="A"
- }
- } };
- db.Orders.Add(order);
- db.SaveChanges();
- }
- Console.Write("ok");
- Console.ReadKey();
- }
- }
- }
App.config
- <?xml version="1.0" encoding="utf-8"?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
- </startup>
- <connectionStrings>
- <add name="DemoContext" connectionString="data source=.;initial catalog=test2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
- </connectionStrings>
- </configuration>
项目:DataAccess(可添加一个空的EFCodeFirst,自动引用必须的程序集)
类:DemoContext
- namespace DataAccess
- {
- using Model;
- using System;
- using System.Data.Entity;
- using System.Linq;
- public class DemoContext : DbContext
- {
- //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
- //使用“Model1”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的
- //“DataAccess.Model1”数据库。
- //
- //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“Model1”
- //连接字符串。
- public DemoContext()
- : base("name=DemoContext")
- {
- }
- //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First 模型
- //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。
- public virtual DbSet<Order> Orders { get; set; }
- public virtual DbSet<Product> Products { get; set; }
- public virtual DbSet<Item> Items { get; set; }
- }
- }
项目:Model
类:Order
类:Product
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Model
- {
- public class Product
- {
- public Guid ProductID { get; set; }
- public string ProductName { get; set; }
- }
- }
类:Item
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Model
- {
- public class Item
- {
- public Guid ItemID { get; set; }
- public Product Product { get; set; }
- }
- }
0 0
- EFCodeFirst-Net
- asp.net MVC EFCodeFirst 生成数据库注意事项
- 在ASP.NET MVC3中使用EFCodeFirst 1.0
- 在ASP.NET MVC3中使用EFCodeFirst 1.0
- mysql+EFCodeFirst
- VS2010安装EFCodeFirst后无法显示ADO.NET DbContext Generator项目的解决方案
- EFCodeFirst安装失败(包括EntityFrameWork安装) 转载自 http://blog.csdn.net/yangzhencheng_001/article/details/6684
- EFCodeFirst package 安装方法
- EFCodeFirst安装解决方案
- EFCodeFirst级联删除
- 如何解决EFCodeFirst安装失败
- vs2010 利用NuGet来安装EFCodeFirst
- EFcodeFirst+T4=操纵任意数据库
- EFCodeFirst安装失败(包括EntityFrameWork安装),这样解决。。。
- EFCodeFirst安装失败(包含EntityFrameWork安装)解决规划
- EFCodeFirst安装失败(包含EntityFrameWork安装)解决规划
- EFCodeFirst安装失败(包括EntityFrameWork安装),这样解决
- EFCodeFirst安装失败(包括EntityFrameWork安装),这样解决。。。
- ABC三栏布局,AC固定宽度,B自适应
- C#中Uri操作
- DotNet处理服务器路径的方法
- DotNet项目中的一些常用验证操作
- C#中DataTable转化为List<T>解析
- EFCodeFirst-Net
- 将DataTable转换成CSV文件
- Oracle常用的SQL方法总结
- HTTP在.NET中的一些应用和解析
- 动态宏的使用
- ManagementClass类解析和C#如何获取硬件的相关信息
- 解析Exception和C#处理Exception的常用方法总结
- HuffmanTree的浅析和在C#中的算法实现
- 【u253】售货厅