Microsoft.AspNetCore.Identity 使用 mysql 报错处理
2024-08-30 10:03:02
1.使用mysql 首先要确定mysql connector 支的版本,正面是链接
https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core.html
Table 9.2 Supported versions of Entity Framework Core
Connector/NET | EF Core 1.1 | EF Core 2.0 | EF Core 2.1 |
---|---|---|---|
6.10.4 | .NET Standard 1.3 or .NET Framework 4.5.2 (and later) | Not supported | Not supported |
6.10.5 to 6.10.7 | .NET Standard 1.3 or .NET Framework 4.5.2 (and later) | .NET Standard 2.0 only (.NET Framework is not supported)
Scaffolding is not supported |
Not supported |
6.10.8 | .NET Standard 1.3 or .NET Framework 4.5.2 | .NET Standard 2.0 or .NET Framework 4.6.1 (and later) | Not supported |
8.0.11 to 8.0.12 | .NET Standard 1.6 or .NET Framework 4.5.2 (and later) | .NET Standard 2.0 only (.NET Framework is not supported)
Scaffolding is not supported |
Not supported |
8.0.13 | .NET Standard 1.6 or .NET Framework 4.5.2 | Not supported | .NET Standard 2.0 or .NET Framework 4.6.1 (and later) |
2.配置数据库连接字符串
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebMvc-6350BA27-C046-416F-B717-F8342091E6E4;Trusted_Connection=True;MultipleActiveResultSets=true",
"MysqlConnection": "server=localhost;database=mydb;uid=root;pwd=123456;" },
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
3 修改StartUP
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("MysqlConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); // Add application services.
services.AddTransient<IEmailSender, EmailSender>(); services.AddMvc();
}
4 生成Migration ,数据迁移过程中有一个bug就是原生 clr bool类型为强转成short类型,如果要程序不报错需要做一个强制转换,在使用Add-Migration InitialCreate
方法对生成的代码需要加以下注释
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebMvc.Data; namespace WebMvc.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20181210165422_Update")]
partial class Update
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.0-rtm-35687"); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd().HasMaxLength(50); b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken(); b.Property<string>("Name")
.HasMaxLength(256); b.Property<string>("NormalizedName")
.HasMaxLength(256); b.HasKey("Id"); b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex"); b.ToTable("AspNetRoles");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd().HasMaxLength(50); b.Property<string>("ClaimType"); b.Property<string>("ClaimValue"); b.Property<string>("RoleId")
.IsRequired().HasMaxLength(50); b.HasKey("Id"); b.HasIndex("RoleId"); b.ToTable("AspNetRoleClaims");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd().HasMaxLength(50); b.Property<string>("ClaimType"); b.Property<string>("ClaimValue"); b.Property<string>("UserId")
.IsRequired(); b.HasKey("Id"); b.HasIndex("UserId"); b.ToTable("AspNetUserClaims");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider").HasMaxLength(50); b.Property<string>("ProviderKey").HasMaxLength(50); b.Property<string>("ProviderDisplayName"); b.Property<string>("UserId")
.IsRequired(); b.HasKey("LoginProvider", "ProviderKey"); b.HasIndex("UserId"); b.ToTable("AspNetUserLogins");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId").HasMaxLength(50); b.Property<string>("RoleId").HasMaxLength(50); b.HasKey("UserId", "RoleId"); b.HasIndex("RoleId"); b.ToTable("AspNetUserRoles");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId").HasMaxLength(50); b.Property<string>("LoginProvider").HasMaxLength(50); b.Property<string>("Name").HasMaxLength(50); b.Property<string>("Value"); b.HasKey("UserId", "LoginProvider", "Name"); b.ToTable("AspNetUserTokens");
}); modelBuilder.Entity("WebMvc.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd(); b.Property<int>("AccessFailedCount"); b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken(); b.Property<string>("Email")
.HasMaxLength(256); b.Property<short>("EmailConfirmed")
.HasColumnType("bit"); b.Property<short>("LockoutEnabled")
.HasColumnType("bit"); b.Property<DateTimeOffset?>("LockoutEnd"); b.Property<string>("NormalizedEmail")
.HasMaxLength(256); b.Property<string>("NormalizedUserName")
.HasMaxLength(256); b.Property<string>("PasswordHash"); b.Property<string>("PhoneNumber"); b.Property<short>("PhoneNumberConfirmed")
.HasColumnType("bit"); b.Property<string>("SecurityStamp"); b.Property<short>("TwoFactorEnabled")
.HasColumnType("bit"); b.Property<string>("UserName")
.HasMaxLength(256); b.HasKey("Id"); b.HasIndex("NormalizedEmail")
.HasName("EmailIndex"); b.HasIndex("NormalizedUserName")
.IsUnique()
.HasName("UserNameIndex"); b.ToTable("AspNetUsers");
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("WebMvc.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("WebMvc.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade); }); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNetCore.Identity.IdentityRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade); b.HasOne("WebMvc.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
}); modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("WebMvc.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade); });
#pragma warning restore 612, 618
}
}
}
源码下载
最新文章
- Spring JdbcTemplate
- 键盘事件触发的兼容tips
- 常用webshell提权方法总结
- 夺命雷公狗jquery---3普通选择器
- Java基础01 ------ 从HelloWorld到面向对象
- Go Deeper(2010成都现场赛题)(2-sat)
- ASP.NET - 多文件上传,纯代码,不使用插件
- Objective-C非正式协议与正式协议
- android 设计模式学习
- 201521123048 《Java程序设计》第9周学习总结
- Python 练习册,每天一个小程序----第0000题
- python3写入csv多一个空行
- vue--";卡片层叠"; 组件 开发小记
- maven 使用 国内镜像的方法 解决依赖下载慢
- 初学UML之-------用例图
- rac添加新节点的步骤与方法(官方步骤与自我测试)
- ASP.NET MVC学习笔记-----Filter(2)
- POJ1061 青蛙的约会(扩展欧几里得)题解
- Twenproxy介绍
- CSS3字体火焰燃烧效果
热门文章
- LeetCode OJ——Word Break
- Codeforces 551E GukiZ and GukiZiana(分块思想)
- GitHub 上受欢迎的 Android UI Library 整理二
- codevs——1700 施工方案第二季
- Django-自己写的py文件调用models&;Non-ASCII character报错&;url接收参数
- Codeforces Gym 100203H Highways 最小生成树
- PyTorch学习笔记之DataLoaders
- soursTree新建过程.md
- Interface Builder中的技巧
- win7 更改同步时间的网址