c# - Mac OSX、ASP.NET 和 mySQL - 密钥长度错误
问题描述
我对 ASP.Net 非常陌生,并且正在尝试编写一个基本应用程序,只是为了在 Web 浏览器中显示 MySQL 数据库中的一些数据。
我认为我尝试创建的实体的主键被标记为太大。当我查看已连接的数据库时,我可以看到出现了一个新模式。如果我再次点击运行,页面将加载,但在第一次运行时(在创建架构之前)我收到了一个错误,我在这个问题的底部包含了这个错误。
如果在下面的代码中取消注释 entity.HasKey 行,并添加一行以将属性更改为适当的大小,则会收到相同的错误。
尝试将 [Key] 放在模型上会产生相同的错误。
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using aspNet_Sep_3_b.Models;
using aspNet_Sep_3_b;
namespace aspNet_Sep_3_b.DAL
{
public class mysqlContext : IdentityDbContext<ApplicationUser> //iddbcontext of type appuser
{
public virtual DbSet<Person> Person { get; set; }
public mysqlContext(DbContextOptions<mysqlContext> options) :base(options)
{
this.Database.EnsureCreated();//Any time we run this project, we need to create the database. So any time we change the database, we'll need to drop it, and create it with a new structure.
}
public mysqlContext()
{
this.Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder builder) //What is this?
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>().Property(u => u.Id).HasMaxLength(36);
builder.Entity<Person>(entity =>
{
entity.Property(e => e.PersonId).HasColumnType("SMALLINT");
//entity.HasKey(e => e.PersonId);
entity.Property(e => e.PersonName).IsRequired().HasMaxLength(36);
});
//builder.Entity<Person>().Property(e => e.PersonID).HasMaxLength(36);
//builder.Entity<Person>().Property(e => e.PersonName).HasMaxLength(36);
}
//Program myProgram = new Program();
}
}
错误:
MySqlException: Specified key was too long; max key length is 3072 bytes
MySql.Data.MySqlClient.MySqlStream.ReadPacket()
MySql.Data.MySqlClient.NativeDriver.GetResult(ref int affectedRow, ref long insertedId)
MySql.Data.MySqlClient.Driver.NextResult(int statementId, bool force)
MySql.Data.MySqlClient.MySqlDataReader.NextResult()
MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary<string, object> parameterValues)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary<string, object> parameterValues)
Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary<string, object> parameterValues)
Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable<MigrationCommand> migrationCommands, IRelationalConnection connection)
Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables()
Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
aspNet_Sep_3_b.DAL.mysqlContext..ctor(DbContextOptions<mysqlContext> options) in mysqlContext.cs
this.Database.EnsureCreated();//Any time we run this project, we need to create the database. So any time we change the database, we'll need to drop it, and create it with a new structure.
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSite(IServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)
lambda_method(Closure , IServiceProvider , object[] )
Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
任何帮助,将不胜感激。
解决方案
由于为 ASP.NET 创建了额外的表,这失败了。您可以将其更改为 DBContext 以消除这种情况,或者您可以将额外的约束添加到列:
builder.Entity<ApplicationUser>(entity => entity.Property
(p => p.Id).HasMaxLength(128));
builder.Entity<ApplicationUser>(entity => entity.Property
(p => p.NormalizedEmail).HasMaxLength(128));
builder.Entity<ApplicationUser>(entity => entity.Property
(p => p.NormalizedUserName).HasMaxLength(128));
builder.Entity<IdentityRole>(entity => entity.Property
(p => p.Id).HasMaxLength(128));
builder.Entity<IdentityRole>(entity => entity.Property
(p => p.NormalizedName).HasMaxLength(128));
builder.Entity<IdentityUserToken<string>>(entity => entity.Property
(p => p.LoginProvider).HasMaxLength(128));
builder.Entity<IdentityUserToken<string>>(entity => entity.Property
(p => p.UserId).HasMaxLength(128));
builder.Entity<IdentityUserToken<string>>(entity => entity.Property
(p => p.Name).HasMaxLength(128));
builder.Entity<IdentityUserRole<string>>(entity => entity.Property
(p => p.UserId).HasMaxLength(128));
builder.Entity<IdentityUserRole<string>>(entity => entity.Property
(p => p.RoleId).HasMaxLength(128));
builder.Entity<IdentityUserLogin<string>>(entity => entity.Property
(p => p.LoginProvider).HasMaxLength(128));
builder.Entity<IdentityUserLogin<string>>(entity => entity.Property
(p => p.ProviderKey).HasMaxLength(128));
builder.Entity<IdentityUserLogin<string>>(entity => entity.Property
(p => p.UserId).HasMaxLength(128));
builder.Entity<IdentityUserClaim<string>>(entity => entity.Property
(p => p.Id).HasMaxLength(128));
builder.Entity<IdentityUserClaim<string>>(entity => entity.Property
(p => p.UserId).HasMaxLength(128));
builder.Entity<IdentityRoleClaim<string>>(entity => entity.Property
(p => p.Id).HasMaxLength(128));
builder.Entity<IdentityRoleClaim<string>>(entity => entity.Property
(p => p.RoleId).HasMaxLength(128));
推荐阅读
- linux - 我正在寻找 BeagleBone Black 的默认 U-boot 配置。我在 u-boot/configs/ 文件夹中找不到 am335x_boneblack_defconfig
- mysql - 使用 SQL COUNT(*) 并仅返回具有有效计数的结果
- css - 如果我有包含我的文件的文件夹,名为 css,为什么样式不在 Laravel 7 中应用?
- flutter - 输入'列表
' 不是类型 'Map 的子类型 ' - html - 如何显示工具提示
- ruby - VSCode Ruby 调试器未列出
- velo - 如何在 Wix Corvid 网站上保存 API 密钥?
- android-studio - 如何在谷歌地图中制作地点列表视图?
- rxjs - 如何按顺序在两个 Observable 之间切换并让订阅触发两次?
- javascript - 如何根据 Google 表格中的范围自动更新 Google 表单中的下拉列表?