第一步:创建两个项目(类库:CodeFirst_Entity 控制台:Test)
第二步:在控制台项目右键-管理NuGet程序包中安装EntityFramework
第三步:在控制台app.config中添加节点
<connectionStrings>
<add name="EFContext" connectionString="server=.;uid=sa;pwd=******;database=test_db"
providerName="System.Data.SqlClient"/>
</connectionStrings>
第四步:在类库项目中应用EntityFramework.dll //单层开发该步骤省略
第五步:在类库项目(Entity)中创建实体类
(注意属性:产品类外键必须是产品类型类名+Id 如:ProductTypeId)
第六步:添加自定义上下文类EFContext 并继承框架的接口DbContext
提示:关联实体类 public DbSet<Product> Products { get; set; }
public class EFContext : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
}
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo_02.Entity.Models
{
public class ProductType
{
public int ProductTypeId { get; set; }//主表主键
/// <summary>
/// 类型名称
/// </summary>
public string TypeName { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo_02.Entity.Models
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public double Price { get; set; }
public string Description { get; set; }
public DateTime CreateTime { get; set; }
public int ProductTypeId { get; set; }
/// <summary>
/// 一对一关系
/// </summary>
public ProductType ProductType { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo_02.Entity.Models
{
public class EFContext:DbContext//数据库上下文,全局配置
{
public DbSet<Product> Products { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
}
}
using EF_Demo_02.Entity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo_02.Entity.DAL
{
public class ProductTypeService
{
public bool Add(ProductType productType)
{
int result = 0;
using (EFContext db = new EFContext())
{
db.ProductTypes.Add(productType);
result = db.SaveChanges();
}
return result > 0;
}
}
}
using EF_Demo_02.Entity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo_02.Entity.DAL
{
public class ProductServices
{
public bool Add(Product product)
{
int result = 0;
using (EFContext db = new EFContext())
{
db.Products.Add(product);
result = db.SaveChanges();
}
return result > 0;
}
}
}
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<connectionStrings>
<add name="EFContext" connectionString="server=.;uid=sa;pwd=adminsystem;database=TaoBao" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
using EF_Demo_02.Entity.DAL;
using EF_Demo_02.Entity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EF_Demo
{
class Program
{
static void Main(string[] args)
{
ProductTypeService productTypeService = new ProductTypeService();
ProductServices productServices = new ProductServices();
productTypeService.Add(new ProductType() { ProductTypeId=1,TypeName="手机"});
productServices.Add(new Product() { ProductId=1001,ProductName="华为p20",Description="测试描述",CreateTime=DateTime.Now,Price=1000,ProductTypeId=1});
}
}
}