即如
EF 应用于 ASP.NET
微软推出EFCore必然是要服务于ASP.NET CORE 的项目
由于.NET6.0环境下 ASP.NET CORE项目的StartUp和program结合在了一起,所以使用的方法的也会有稍有不同,
1.创建一个空的ASP.NET CORE 项目(可以是WebApi)
添加安装EFCore的组件
2.创建类库,编写实体类和数据库上下文
namespace POCO.Entity
{
//编写一个简单的一对多的实体关系
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string address { get; set; }
}
public class Pet
{
public int Id { get; set; }
public string Name { get; set; }
public Person Owener { get; set; }
}
public class PPDbContext :DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Pet> Pets { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//SqlServer的连接字符串
var Connection = "server=.;database=EFCoreTest;uid=XXXX;pwd=XXXX";
optionsBuilder.UseSqlServer(Connection);
}
}
}
类库项目也需要添加EFCore组件的依赖哦,csproj中添加或者直接右键项目在NulGet里安装即可
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
需要注意的一点是,你需要在ASP.NET CORE项目启动前,执行迁移完成建表,所以要先把
连接字符串写死,打开包管理操控台选择默认项目为类库执行建表,
当迁移建表完成后,要在ASP.NET CORE项目中使用时,我们再修改DbContext(数据库上下文)的连接方式并配置依赖注入。
3.创建测试项目,插入几条测试数据
这里用NUnit测试工具插入几条数据
namespace TheTestProject
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
PPDbContext context = new PPDbContext();
Person zhangsan = new Person();
zhangsan.Name = "法外狂徒";
zhangsan.Age = 999;
zhangsan.address = "上海";
Person lisi = new Person();
lisi.Name = "张三说刑法";
lisi.Age = 998;
lisi.address = "武汉";
Pet pet = new Pet();
pet.Name = "旺财";
pet.Owener = zhangsan;
Pet pet2 = new Pet();
pet2.Name = "阿喵";
pet2.Owener = zhangsan;
Pet pet3 = new Pet();
pet3.Name = "小黑";
pet3.Owener = lisi;
context.Persons.Add(zhangsan);
context.Persons.Add(lisi);
context.Pets.Add(pet);
context.Pets.Add(pet2);
context.Pets.Add(pet3);
context.SaveChanges();
}
}
}
查看数据库
4.创建Controller把数据返回给前端
先把连接字符串写到配置文件里、
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings" :
{
"LocalDb": "server=.;database=EFCoreTest;uid=XXXXXXXXXXX;pwd=XXXX"
}
}
再改一下DbContext
public class PPDbContext :DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Pet> Pets { get; set; }
//编写数据库上下文的改造函数以方便ASP.NET CORE将属性和依赖注入
public PPDbContext(DbContextOptions<PPDbContext> options):base(options)
{
}
}
在Progrom中添加注入,并注入配置信息
builder.Services.AddDbContext<PPDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("LocalDb")));
写一个Controller直接调用DbContext(这里为了方便就不使用持久层和业务层进行分层了)
[ApiController]
[Route("api/[Controller]/[action]")]
public EFCoreController:ControllerBase
{
private PPDbContext _context;
public EFCoreController(PPDbContext context)
{
_context = context;
}
public List<Pet> getPetsByPersonId(int id)
{
return _context.pets.Where(pet=>pet.Owner.Id == id).ToList();
}
}