Entity Framework Core对Web项目生成数据库表
一、引言
这篇文章中我们讲解如何在Web项目中使用EntityFrameworkCore,并生成数据库表,这里以ASP.NET Core WebApi为例讲解。还是采用分层的结构。创建后的项目整体结构如下图所示:

项目结构:
- EFCoreWeb.API:ASP.NET Core WebApi项目,用来提供Web功能,在项目中会引用EFCoreWeb.Data。
- EFCoreWeb.Data:类库项目,基于.NET Core的类库。存放的是与EFCore相关的操作。
- EFCoreWeb.Model:类库项目,基于.NET Core的类库。存放的是实体类。
1、添加实体类
我们在EFCoreWeb.Model类库项目里面添加Student实体类:
namespace EFCoreWeb.Model
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public int Gender { get; set; }
}
}2、添加Mircosoft.EntityFrameworkCore
因为要使用Student实体类,首先要在EFCoreWeb.Data项目里面添加对EFCoreWeb.Model的引用。
然后在EFCoreWeb.Data类库项目里面添加Mircosoft.EntityFrameworkCore包,直接在NuGet里面安装:

由于我们使用的是SQLServer数据库,所以我们还要安装Microsoft.EntityFrameworkCore.sqlServer包,同样也是直接在NuGet里面安装:

安装完上面的两个包以后,在EFCoreWeb.Data类库项目里面添加Mapping文件夹,用来存放Fluent API的配置文件,Student类的配置伙伴类代码如下:
using EFCoreWeb.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace EFCoreWeb.Data.Mapping
{
/// <summary>
/// Student配置伙伴类,继承自IEntityTypeConfiguration<T>泛型接口
/// </summary>
public class StudentMap : IEntityTypeConfiguration<Student>
{
/// <summary>
/// 实现接口里面的Configure方法,用来配置生成数据库表结构
/// </summary>
/// <param name="builder"></param>
public void Configure(EntityTypeBuilder<Student> builder)
{
// 设置主键
builder.HasKey(p => p.Id);
// 设置生成的表名
builder.ToTable("T_Student");
// 设置Name列的最大长度
builder.Property("Name").HasMaxLength(64);
// 设置Name列是必须的
builder.Property("Name").IsRequired();
}
}
}添加一个Context文件夹,然后添加数据上下文类,继承自DbContext:
using EFCoreWeb.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace EFCoreWeb.Data.Mapping
{
/// <summary>
/// Student配置伙伴类,继承自IEntityTypeConfiguration<T>泛型接口
/// </summary>
public class StudentMap : IEntityTypeConfiguration<Student>
{
/// <summary>
/// 实现接口里面的Configure方法,用来配置生成数据库表结构
/// </summary>
/// <param name="builder"></param>
public void Configure(EntityTypeBuilder<Student> builder)
{
// 设置主键
builder.HasKey(p => p.Id);
// 设置生成的表名
builder.ToTable("T_Student");
// 设置Name列的最大长度
builder.Property("Name").HasMaxLength(64);
// 设置Name列是必须的
builder.Property("Name").IsRequired();
}
}
}二、生成数据库表
这里我们使用程序包管理器控制台迁移的方式来生成数据库表。需要在EFCoreWeb.Data项目里面安装Microsoft.EntityFrameworkCore.Tools包。在EFCoreWeb.API项目里面安装Microsoft.EntityFrameworkCore.Tools、Microsoft.EntityFrameworkCore两个包。EFCoreWeb.API项目添加对EFCoreWeb.Data项目的引用。
首先在EFCoreWeb.API项目的appsettings.json文件里面添加数据库连接字符串:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionString": {
"DbConnection": "Data Source=.;Initial Catalog=EFTestDb;User ID=sa;Password=123456;"
}
}在Startup类的ConfigureServices方法里面添加数据库连接:
using EFCoreWeb.Data.Context;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace EFCoreWeb.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
#region 数据库连接
services.AddDbContext<EFCoreDbContext>(options => {
// options.UseSqlServer(Configuration.GetConnectionString("DbConnection"));
options.UseSqlServer(Configuration.GetSection("ConnectionString").GetSection("DbConnection").Value);
});
#endregion
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}上面步骤配置完成以后,在程序包管理器控制台里面开始迁移,使用下面的命令添加迁移:
Add-Migration Init
如下图所示:

执行完命令以后就会生成迁移文件:

添加迁移之后,执行下面的命令更新数据库:
Update-Database
如下图所示:

执行完以后去查看数据库:

可以看到,表里面Name列的长度是根据代码里面设置的长度生成的,而且不为null,种子数据也插入进去了。
到此这篇关于Entity Framework Core对Web项目生成数据库表的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
ASP.NET MVC4 HtmlHelper扩展类,实现分页功能
本文主要做了一个HtmHelper类的分页扩展函数,方便在视图中调用,有需要的朋友可以参考一下,希望对大家有所帮助。2016-03-03
.NET 7 AOT 的使用及 .NET 与 Go 互相调用的过程
本文主要介绍如何在.NET和Go语言中如何生成系统(Windows)动态链接库,又如何从代码中引用这些库中的函数,在文章中会演示.NET和Go相互调用各自生成的动态链接库,以及对比两者之间的差异,感兴趣的朋友一起看看吧2024-12-12
使用asp.net mvc,boostrap及knockout.js开发微信自定义菜单编辑工具(推荐)
这篇文章主要介绍了使用asp.net mvc,boostrap及knockout.js开发微信自定义菜单编辑工具,非常不错,具有参考借鉴价值,需要的朋友可以参考下2017-05-05
Net5 WorkService 继承 Quarzt 及 Net5处理文件上传功能
这篇文章主要介绍了Net5 WorkService 继承 Quarzt 以及 Net5处理文件上传,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-02-02
ASP.NET Core MVC解决控制器同名Action请求不明确的问题
这篇文章主要介绍了ASP.NET Core MVC解决控制器同名Action请求不明确的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-03-03


最新评论