EFCore-1搭建开发环境

使用SQL Server数据库作为学习的目标数据库,EFCore使用Code First模式进行项目初始化。

项目整体目录如图所示:

 

1.创建实体类

using System;

namespace EFCoreProject
{
    /// <summary>
    /// Book实体类
    /// </summary>
    public class Book
    {
        public long Id { get; set; }

        public string Title { get; set; }

        public DateTime PubTime { get; set; }

        public double Price { get; set; }

        /// <summary>
        /// 作者姓名
        /// </summary>
        public string AuthorName { get; set; }
    }
}
namespace EFCoreProject
{
    /// <summary>
    /// 人员实体类
    /// </summary>
    public class Person
    {
        public long Id { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }
    }
}

 

2.创建DbContext

添加程序集引用:Microsoft.EntityFrameworkCore.SqlServer

using Microsoft.EntityFrameworkCore;

namespace EFCoreProject
{
    public class MyDbContext : DbContext
    {
        //定义有哪些实体
        public DbSet<Book> Books { get; set; }
        public DbSet<Person> Peoples { get; set; }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //连接的数据库
            string connStr = "Server=.;Database=EFCoreDemo1;Trusted_Connection=True;MultipleActiveResultSets=true";
            optionsBuilder.UseSqlServer(connStr);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //从当前程序集中加载 实现了IEntityTypeConfiguration接口的实体配置类
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }
    }
}

3.创建实现了 IEntityTypeConfiguration 接口的实体配置类,配置实体类和数据库表的对应关系(非必须,建议每个实体类都创建对应的实体配置类)

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EFCoreProject
{
    /// <summary>
    /// Book实体配置类
    /// </summary>
    public class BookEntityConfig : IEntityTypeConfiguration<Book>
    {
        public void Configure(EntityTypeBuilder<Book> builder)
        {
            builder.ToTable("T_Books");//对应到数据库的表名称

            //配置字段相关属性
            builder.Property(b => b.Title).HasMaxLength(50).IsRequired();
            builder.Property(b => b.AuthorName).HasMaxLength(20).IsRequired();
        }
    }
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace EFCoreProject
{
    /// <summary>
    /// Person实体配置类
    /// </summary>
    public class PersonEntityConfig : IEntityTypeConfiguration<Person>
    {
        public void Configure(EntityTypeBuilder<Person> builder)
        {
            builder.ToTable("T_Persons");//对应到数据库的表名称
        }
    }
}

4.创建DbContext

创建类MyDbContext,继承DbContext。加载实体,定义数据库连接字符串等。

using Microsoft.EntityFrameworkCore;

namespace EFCoreProject
{
    public class MyDbContext : DbContext
    {
        //定义有哪些实体
        public DbSet<Book> Books { get; set; }
        public DbSet<Person> Peoples { get; set; }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //连接的数据库
            string connStr = "Server=.;Database=EFCoreDemo1;Trusted_Connection=True;MultipleActiveResultSets=true";
            optionsBuilder.UseSqlServer(connStr);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //从当前程序集中加载 实现了IEntityTypeConfiguration接口的实体配置类
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }
    }
}

5.生成数据库及表

使用Migration工具生成数据库对象(迁移)

添加程序集引用: Microsoft.EntityFrameworkCore.Tools

在vs的程序包管理器控制台中执行生成脚本和更新脚本的命令,将实体映射到数据库,生成对应的数据库和表。

 

生成脚本命令:

add-migration 自定义变量名

例如

add-migration Init

代表第一次初始化

更新脚本命令:

update-database 

 

命令执行完成后,在项目的目录中会生成Migrations的文件夹,记录每次执行的脚本信息

 

数据库也可以看到新增的库及表

到此为止EFCore开发环境的搭建已经完成,接下去就可以使用EFCore来操作数据库了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值