EFCore——控制台搭建EFCore开发环境(1)

控制台搭建EFCore开发环境

一、EFCore简介

1.是对与底层ADO.Net Core的封装,ADO.Net Core支持的数据库,EFCore不一定支持

2.EFCore支持目前市面大部分主流数据库

3.EFCore尽力在屏蔽底层数据库的差异,也就是不写具体的sql语句,
 EFCore自动将你的代码转换为对应数据库的SQL语句,当然EFCore的动作也不可预测也就是你看不到具体写的Sql语句,不知道底层发生Sql了什么
 
4.类似的工具还有Dapper,Sqlsuger、FreeSql,一般不建议使用后俩个。
5.
EFCore:
优点:开发效率高,功能强大,官方支持
缺点:复杂,入手门槛高,行为可预期弱
Dapper:
优点:简单,行为可预期强,因为是直接写sql语句,所以N分钟上手
缺点:开发效率低

应用场景:
EFCore:在团队成员稳定时用开发方便,效率高,当人员变动大的    
   时候, 付出学习成本高。
Dapper: 能直接使用Sql进行CRUD当人员变动较大时,学习成本低,只要    
  会sql就能几分钟上手(简单的Sql不会还有人不会吧)

二、实例创建

首先引入Nuget包:Microsoft.EntityFrameworkCore.SqlServer
1.创建一个实体类

Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_EF_Enviroment
{
    public class Book
    {
        public long Id { get; set; } 
        public string Tittle { get; set; }
        public DateTime PublicTime { get; set; }
        public double Price { get; set; }
        public string AuthorName { get; set; }

    }
}
2.创建Book.cs的配置类 BookConfig
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_EF_Enviroment
{
    //继承了IEntityTypeConfiguration,用来数据库查找相应的实体配置
    class BookConfig :IEntityTypeConfiguration<Book>
    {
        public void  Configure(EntityTypeBuilder<Book> builder)
        {
            //表名T_Books
            builder.ToTable("T_Books");
            //设置Tittle的最大长度,不能为空
            builder.Property(b => b.Tittle).HasMaxLength(50).IsRequired();
            //设置AuthorName的最大长度,不能为空
            builder.Property(b => b.AuthorName).HasMaxLength(20).IsRequired();
        }
    }
}

3.创建MyDbContext类,用于数据库操作

点击查看代码
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_EF_Enviroment
{
    public class MyDBContext :DbContext
    {
        public DbSet<Book> Book { get; set; }
        public DbSet<Person> Person { get; set; }
        public DbSet<Dog> Dogs { get; set; }

        //这里是配置
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //DbContextOptionsBuilder注入
            base.OnConfiguring(optionsBuilder);
            //设置连接字符串
            optionsBuilder.UseSqlServer("server=localhost;uid=sa;pwd=123456;database=demo1_Enviroment;");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //注入ModelBuilder
            base.OnModelCreating(modelBuilder);
            //获取当前程序集默认的是查找所有继承了IEntityTypeConfiguration的类
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }
    }
}

三、源码地址

源码地址

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有诗亦有远方

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值