zoukankan      html  css  js  c++  java
  • .net core3.1使用EF连接SQLserver与mysql数据库(代码优先创建数据库、数据库优先创建实体模型)

    一.代码优先 创建数据库(SQLServer2012)

    1.创建core3.1的项目

    项目结构如下
    在这里插入图片描述
    如图,我这里创建了一个core的类库用来保存数据库相关的实体
    注意:使用的类库环境必须与core项目的环境一致

    2.添加所需NUGet包

    Microsoft.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Tools
    Microsoft.EntityFrameworkCore.SqlServer
    Microsoft.EntityFrameworkCore.Design
    在这里插入图片描述

    3.创建一些实体

    添加实体注解需要引用命名空间:
    using System.ComponentModel.DataAnnotations;

    3.1用户类

     public class Customers
        {
            public int ID { get; set; }
            [StringLength(50)]
            public string Name { get; set; }
            [StringLength(2)]
            public string Sex { get; set; }
            [StringLength(11)]
            public string Phone { get; set; }
            [StringLength(200)]
            public string Address { get; set; }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.2商品类

     public class Product
        {
            public int ID { get; set; }
            [StringLength(20)]
            public string Name { get; set; }
            public decimal Price { get; set; }
            public string Desc { get; set; }
            /// <summary>
            /// 导航属性 一件商品对应一个分类
            /// </summary>
            public ProductType ProductType { get; set; }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.3商品类型类

    public  class ProductType
        {
            public int ID { get; set; }
            [StringLength(20)]
            public string TypeName { get; set; }
            /// <summary>
            /// 导航属性  一对多,一个类型可对多个商品
            /// </summary>
            public ICollection<Product> Products { get; set; }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.4上下文类

     /// <summary>
        /// 上下文类
        /// 继承系统上下文
        /// </summary>
        public class CoreMVCContext:DbContext
        {
            public CoreMVCContext()
            {
    
            }
            public CoreMVCContext(DbContextOptions option) : base(option)
            {
    
            }
            public DbSet<ProductType> ProductTypes { set; get; }
            public DbSet<Product> Product { set; get; }
            public DbSet<Customers> Customers { set; get; }
            /// <summary>
            /// 重写父类的方法 用于连接数据库
            /// </summary>
            /// <param name="optionsBuilder"></param>
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                if (!optionsBuilder.IsConfigured)
                {
                //连接字符串
                    optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=CoreMVC;Integrated Security=True");
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    4.准备代码的迁移与数据库的更新

    4.1代码迁移:Add-Migration

    4.2数据库更新:update-database

    在这里插入图片描述

    二、数据库优先 创建实体类(SQLServer2012)

    1.获得数据库的连接字符串

    在这里插入图片描述
    在这里右键添加连接,选择需要连接的服务器和数据库,本地为‘.’,,测试连接,连接成功了去“高级”里面将连接字符串复制出来,等会要用

    2.安装所需依赖,同上

    3.连接数据库

    Scaffold-DbContext "Data Source=.;Initial Catalog=UU;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Model -Context UUDBContext
    在这里插入图片描述
    若启动项目不对
    在这里插入图片描述

    4.成功

    有警告,注释掉那一行即可
    在这里插入图片描述

    三、数据库优先 创建实体类(MySQL 5.7)

    本以为连接mysql会麻烦不少,记得年初连接mysql时还不支持ef6.x的框架,要连接还需要使用低配的依赖包才可以,但是这次却直接可以了,感觉有点神奇

    1.获得连接字符串

    同上,如果是知道连接的可以跳过

    2.安装依赖包

    因为这是mysql的,使用依赖包和上面的有所不同
    Microsoft.EntityFrameworkCore.Tools
    Microsoft.VisualStudio.Web.CodeGeneration.Design
    MySql.Data.EntityFrameworkCore
    Pomelo.EntityFrameworkCore.MySql
    在这里插入图片描述

    3.连接数据库并迁移

    NuGet包控制台:
    Scaffold-DbContext "server=数据库服务器;uid=数据库用户名;pwd=数据库密码;database=数据库名;" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Force
    .Net Core CLi:
    dotnet ef dbcontext scaffold "server=数据库服务器;uid=数据库用户名;pwd=数据库密码;database=数据库名;" Pomelo.EntityFrameworkCore.MySql -o Models -f
    在这里插入图片描述
    MySQL的代码优先与上差不多

    四、补充(代码参数说明)

    补充:其它数据库提供程序请参考:https://docs.microsoft.com/zh-cn/ef/core/providers/

    代码参数说明:
    -OutputDir (-o) *** 实体文件所存放的文件目录
    -ContextDir *** DbContext文件存放的目录
    -Context *** DbContext文件名
    -Schemas *** 需要生成实体数据的数据表所在的模式
    -Tables(-t) *** 需要生成实体数据的数据表的集合
    -DataAnnotations
    -UseDatabaseNames 直接使用数据库中的表名和列名(某些版本不支持)
    -Force (-f) 强制执行,重写已经存在的实体文件

    五、更新

    如果出现如下错误:

    CS1705 C# 标识为“ShopMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”的程序集“ShopMode”所使用的“Microsoft.EntityFrameworkCore, Version=3.1.7.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”版本高于所引用的标识为“Microsoft.EntityFrameworkCore, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60”的程序集“Microsoft.EntityFrameworkCore”
    
    • 1

    则将Microsoft.EntityFrameworkCore.Tools的版本号进行降级,降到3.1.0即可
    错误原因是该依赖包的版本要比项目文件的版本高

  • 相关阅读:
    next_permutation
    P1087 FBI树
    P4047 [JSOI2010]部落划分
    买礼物
    P2121 拆地毯
    Nebula Graph 在大规模数据量级下的实践和定制化开发
    深入了解kafka系列-消费者
    一分钟教你搭建WebRTC流媒体服务器Janus-gateway
    什么是"前端工程化"?
    斗鱼Juno 监控中心的设计与实现
  • 原文地址:https://www.cnblogs.com/zxtceq/p/13719884.html
Copyright © 2011-2022 走看看