asp.net-core - .net core 2.0 - 值不能为空。参数名称:key
问题描述
添加迁移时,我在包管理器控制台中有这样的错误
Entity Framework Core 2.0.1-rtm-125 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=Project.Data
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName)
at Microsoft.EntityFrameworkCore.RelationalMetadataExtensions.Relational(IKey key)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<Add>d__35.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__34`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<Add>d__26.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<DiffCollection>d__50`1.MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null.
Parameter name: key
我正在尝试添加组织实体:
public class Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}
这是我的 AppDbContext
public class ApplicationDbContext : IdentityUserContext<AppUser>
{
public virtual DbSet<Organization> Organizations { get; set; }
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
当我运行应用程序时,它工作正常,但是当我尝试添加迁移时,它告诉我参数名称 - 键不能为空。有人可以在这里点亮一些灯吗?谢谢
这是我的启动代码
public class Startup
{
public IConfiguration _configuration { get; }
// Inject project configuration
public Startup(IConfiguration configuration)
{
_configuration = configuration;
}
// This method gets called by the runtime.
// Use this method to add services to the conteiner.
public void ConfigureServices(IServiceCollection services)
{
#region Database
// Add Database Context
services.AddDatabase(_configuration);
#endregion
...
#region Identity
// Add Identity
// TODO: Extract to external extension method .AddIdentity()
var builder = services.AddIdentityCore<AppUser>(o =>
{
// Configure Identity options
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
});
builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
builder.AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
#endregion
...
services.AddAutoMapper();
services.AddMvc();
}
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// TODO: Add development configuration
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
...
// Configure application for usage as API
// with default route of '/api/[Controller]'
app.UseMvcWithDefaultRoute();
// Configures application to serve the index.html file from /wwwroot
// when you access the server from a browser
app.UseDefaultFiles();
app.UseStaticFiles();
}
}
解决方案
不应该
dbContext
被称为IdentityDbContext<AppUser>
notIdentityUserContext<AppUser>
吗?构造函数不应该
ApplicationDbContext
采用DbContextOptions
typeApplicationDbContext
吗?public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
你不需要
virtual
关键字吗?public DbSet<Organization> Organizations { get; set; }
改用
AddIdentity
?services.AddIdentity<AppUser, AppRole>(options => options.User.RequireUniqueEmail = true/false; options.Password.RequireDigit = true/false; ... }) .AddEntityFrameworkStores<ApplicationDbContext>() // Components that generate confirm email and reset password tokens. .AddDefaultTokenProviders();
我不知道你从哪里得到的代码,但它有点凌乱且难以阅读。
推荐阅读
- javascript - 如何计算反应钩子状态变化的百分比
- node.js - 使用多租户 GraphDB (Neo4J / ArangoDB) 设计和实现 SaaS 应用程序
- deep-learning - Darknet YOLOv4 训练 - 它是否打印每张图像、每批、每一个细分?得到奇怪的数字
- sql - 当行丢失时,用 Oracle SQL PIVOT 结果中的自定义值替换 NULL
- javascript - 如何遍历类别以获取子类别并在其中创建嵌套的子类别数组?
- css - 在 SASS 模块之间引用类名
- android - Linkify.addLinks(textView、Linkify.WEB_URLS 或 Linkify.EMAIL_ADDRESSES)不能在 Android 中一起使用
- database - 谁能帮助如何为默认的颤振计数器应用程序实现 Sqflite?
- google-analytics - 目标中缺少一步
- javascript - 如何将单元格范围内的值更改为谷歌 AppScript 中的数据数组?