.net core 实现 data first的相关操作
先看一下demo目录
一:准备操作:
1、先新建三个类:具有一对多的关系,添加了导航属性(可以不加)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Data
{
public enum Grade
{
A, B, C, D, F
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public Grade? Grade { get; set; }
public Course Course { get; set; }
public Student Student { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Data
{
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Enrollment> Enrollments { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Data
{
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public ICollection<Enrollment> Enrollments { get; set; }
}
}
2、新建TestDbContext.cs(数据上下文)
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Data
{
public class TestDbContext:DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
public DbSet<User> Uesrs { get; set; }
public DbSet<Student> Student { get; set; }
public DbSet<Enrollment> Enrollment { get; set; }
public DbSet<Course> Course { get; set; }
}
}
3、配置appsettings.json连接字符串(ConnectionStrings)
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"SqlServer": "Data Source=PC-20160722VLKH\\SQL2014;Initial Catalog=ZjTestDataBase;User Id=zhangjian;Password=123;"
}
}
4、在startup.cs中添加如下代码
----------------------------------------------
var connection = Configuration.GetConnectionString("SqlServer");
services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(connection, b => b.MigrationsAssembly("WebApplication1")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
------------------------------------------------
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
var connection = Configuration.GetConnectionString("SqlServer");
services.AddDbContext<TestDbContext>(options =>
options.UseSqlServer(connection, b => b.MigrationsAssembly("WebApplication1")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
5、右键你的web工程,编辑webapplication.csproj文件如下:
新增:Version为EntityFrameworkCore的版本,可以去negut中查看
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.1" />
</ItemGroup>
最终如下:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.1.1" />
</ItemGroup>
</Project>
到这边为止,前期工作已经做完了,现在我们开始下一步:
二、生成到数据库操作
1、主要是两个命令:
dotnet ef migrations add v1 这个命令的作用的是:当我添加了类,就要改变v1成vn,相当于版本,这样下面的命令才能知道实体被修改了,才能更新。如果实体修改了,这边不修改,讲不能更新。
注意点:要cd到项目的文件夹再执行,如图所示:
如果不报错再执行下面的命令。
dotnet ef database update 这个命令的作用的是:把修改的更新到指定到数据库。
注意点,不要使用sa,新建一个用户,给与他创建数据库的能力,如图:
这样就做完了。
最终效果图如下:对比一下,这就是code first.
后面将继续更新.net core学习,待续。。。有疑问联系我:qq:1057359832