1、首先需要从gitHub上下载源码:
https://github.com/aspnet/EntityFramework6
2、添加新项目,并且把EntityFramework 项目和EntityFramework.SqlServer添加的新添加的解决方案里
3、去掉EntityFramework和EntityFramework.SqlServer 中的签名
4、修改新项目的配置文件
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral"
requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
5、编写代码,使用CodeFirst模式调制程序
public class TestDBDbContext:DbContext
{
public TestDBDbContext() : base("data source=127.0.0.1;initial catalog=TestDB;user id=sa;password=123456;multipleactiveresultsets=True;application name=EntityFramework")
{
}
public virtual DbSet<TB_City> City { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
[Table("TB_City")]
public class TB_City
{
[Key]
public int ID { get; set; }
public string CityName { get; set; }
public string ZipCode { get; set; }
public int PrivinceID { get; set; }
}
TestDBDbContext db = new TestDBDbContext();
public void Create()
{
db.City.Add(new TB_City() { CityName = "济南", PrivinceID = 1, ZipCode = "200000" });
db.SaveChanges();
}
5、开始调用
static void Main(string[] args)
{
CityDAL dal = new ConsoleApplication.CityDAL();
dal.Create();
Console.Read();
}