gpt4 book ai didi

c# - Mac OSX、ASP.NET 和 mySQL - key 长度错误

转载 作者:行者123 更新时间:2023-11-30 21:27:58 24 4
gpt4 key购买 nike

我是 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));

关于c# - Mac OSX、ASP.NET 和 mySQL - key 长度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57930922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com