zoukankan      html  css  js  c++  java
  • ASP.NET Core中使用EF Core(MySql)Code First

    ⒈添加依赖

      MySql.Data.EntityFrameworkCore

    ⒉在appsettings.json配置文件中配置数据库连接字符串

     1 {
     2   "Logging": {
     3     "LogLevel": {
     4       "Default": "Warning"
     5     }
     6   },
     7   "ConnectionStrings": {
     8     "MySqlConnection": "server=localhost;port=3306;database=blog;user=root;password=admin"
     9   },
    10   "AllowedHosts": "*"
    11 }

    ⒊编写数据表实体类及上下文

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 
     6 namespace NewDb.Models
     7 {
     8     public class Blog
     9     {
    10         public int BlogId { get; set; }
    11         public string Url { get; set; }
    12 
    13         public ICollection<Post> Posts { get; set; }
    14 
    15     }
    16 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 
     6 namespace NewDb.Models
     7 {
     8     public class Post
     9     {
    10         public int PostId { get; set; }
    11         public string Title { get; set; }
    12         public string Content { get; set; }
    13 
    14         public int BlogId { get; set; }
    15         public Blog Blog { get; set; }
    16     }
    17 }
     1 using Microsoft.EntityFrameworkCore;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Threading.Tasks;
     6 
     7 namespace NewDb.Models
     8 {
     9     public class BloggingDbContext : DbContext
    10     {
    11         public BloggingDbContext(DbContextOptions<BloggingDbContext> options) : base(options) { }
    12 
    13         public DbSet<Blog> Blogs { get; set; }
    14         public DbSet<Post> Posts { get; set; }
    15     }
    16 }

    ⒋使用依赖注入将上下文注册为服务

     1         public void ConfigureServices(IServiceCollection services)
     2         {
     3             services.Configure<CookiePolicyOptions>(options =>
     4             {
     5                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.
     6                 options.CheckConsentNeeded = context => true;
     7                 options.MinimumSameSitePolicy = SameSiteMode.None;
     8             });
     9 
    10 
    11             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    12 
    13             var connection = Configuration["ConnectionStrings:MySqlConnection"];
    14 
    15             services.AddDbContext<BloggingDbContext>(options =>
    16             {
    17                 options.UseMySQL(connection);
    18             });
    19         }

    ⒌使用迁移创建数据库

      第一种方式:"Visual Studio 2019" >“工具”>“NuGet 包管理器”>“程序包管理器控制台”,执行以下命令

    1 Add-Migration InitialCreate -v #搭建迁移基架,以便为模型创建一组初始表
    2 Update-Database -v #创建数据库并向其应用新的迁移

      第二种方式:使用.Net Core CLI,执行以下命令

    1 dotnet ef migrations add InitialCreate -v #搭建迁移基架,以便为模型创建一组初始表
    2 dotnet ef database update -v #创建数据库并向其应用新的迁移
  • 相关阅读:
    关于postman与shiro权限验证问题
    springboot对shiro进行mock单元测试
    深入理解spring注解之@ComponentScan注解
    springboot项目启动,但是访问报404错误
    通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool
    重装系统后ORACLE数据库恢复的方法
    ORA-03113: end-of-file on communication channel 解决方法
    ORA-03113:通信通道的文件结尾-完美解决方案
    由于Windows和Linux行尾标识引起脚本无法运行的解决
    在cmd命令行中弹出Windows对话框(使用mshta.exe命令)
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/10809461.html
Copyright © 2011-2022 走看看