如何在Entity Framework Core 3中无需密钥即可播种数据?

时间:2019-12-06 12:42:36

标签: c# .net-core asp.net-core-mvc entity-framework-core

说明

我想将数据播种到我的数据库中。数据没有键或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.

如果我尝试使用键添加示例数据,它将起作用。没有键/ 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();
        }
    }
}

0 个答案:

没有答案