c# - 如何在 Entity Framework Core 3 中没有密钥的情况下播种数据?
问题描述
描述
我想将数据播种到我的数据库中。数据没有键或 id 值。但由于某种原因,我得到了错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.get_IsKeyUnknown()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PropagateToUnknownKey(EntityState oldState, EntityState entityState, Boolean adding, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.Microsoft.EntityFrameworkCore.Update.IUpdateEntry.set_EntityState(EntityState value)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IModel source, IModel target)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IModel source, IModel target, 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, String language)
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_0.<.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)
Object reference not set to an instance of an object.
如果我尝试使用键添加示例数据,它会起作用。没有 key / id 就不能将示例数据添加到数据库中吗?
代码
ModelBuilderExtensions.cs
using Microsoft.EntityFrameworkCore;
using workbranch.Models.Db.Helper;
using workbranch.Models.HonorarTool;
namespace workbranch.Models.Db
{
public static class ModelBuilderExtensions
{
public static void Seed(this ModelBuilder modelBuilder)
{
modelBuilder.Entity<HonorarToolLists>()
.HasNoKey()
.HasData(
new HonorarToolLists
{
Name = "TGA-MT",
Year = 2013,
ChargeableCosts = 5000,
Zone = 1,
MinRate = 2132,
MaxRate = 2547
}
);
}
}
}
HonorarToolLists.cs
using System.ComponentModel.DataAnnotations.Schema;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace workbranch.Models.HonorarTool
{
public partial class HonorarToolLists
{
public string Name { get; set; }
public int Year { get; set; }
public int ChargeableCosts { get; set; }
public int Zone { get; set; }
public int MinRate { get; set; }
public int MaxRate { get; set; }
}
}
AppDbContext.cs
using System.Net.Sockets;
using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using workbranch.Models.StatusReporting;
using workbranch.Models.HonorarTool;
namespace workbranch.Models.Db
{
public class AppDbContext : IdentityDbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{ }
public DbSet<HonorarToolLists> HonorarToolLists { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Seed();
}
}
}
解决方案
推荐阅读
- javascript - AngularJS,谷歌浏览器阻止我的应用程序发出发布请求,需要帮助添加标题
- java - 在 TinyMCE 中为流 Vaadin 组件使用自定义皮肤时遇到问题
- spring-boot - 收到错误 java.nio.file.AccessDeniedException
- c - 如何通过套接字连接从命令行传递字符串
- javascript - 减少 React 表单验证中的 if 数量
- powershell - 所有 AD 用户的 Powershell 删除
- amazon-web-services - Terraform 错误:运行 terraform init 时模块的无关标签
- jquery - require.js 和 jquery UI 小部件的淘汰赛可排序问题
- azure - Azure:从应用服务访问 Key Vault 时,如何解决“策略要求调用者'...'使用代表 (OBO) 流”?
- mysql - MySQL:将单行显示为列(简单地说,通过 SELECT)