Luk*_*und 6 c# entity-framework-core asp.net-core-mvc .net-core
我想将数据播种到我的数据库中。数据没有键或 ID 值。但由于某种原因我收到了错误:
\n\nSystem.NullReferenceException: Object reference not set to an instance of an object.\n at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.get_IsKeyUnknown()\n at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PropagateToUnknownKey(EntityState oldState, EntityState entityState, Boolean adding, Nullable`1 forceStateWhenUnknownKey)\n at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)\n at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.Microsoft.EntityFrameworkCore.Update.IUpdateEntry.set_EntityState(EntityState value)\n at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IModel source, IModel target)\n at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IModel source, IModel target, DiffContext diffContext)\n at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)\n at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)\n at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()\n at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)\nObject reference not set to an instance of an object.\nRun Code Online (Sandbox Code Playgroud)\n\n如果我尝试使用密钥添加示例数据,它会起作用。是否无法在没有 key/id 的情况下将示例数据添加到数据库中?
\n\n模型构建器扩展.cs
\n\nusing Microsoft.EntityFrameworkCore;\nusing workbranch.Models.Db.Helper;\nusing workbranch.Models.HonorarTool;\n\nnamespace workbranch.Models.Db\n{\n public static class ModelBuilderExtensions\n {\n public static void Seed(this ModelBuilder modelBuilder)\n {\n modelBuilder.Entity<HonorarToolLists>()\n .HasNoKey()\n .HasData(\n new HonorarToolLists\n {\n Name = "TGA-MT",\n Year = 2013,\n ChargeableCosts = 5000,\n Zone = 1,\n MinRate = 2132,\n MaxRate = 2547\n }\n );\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n荣誉工具列表.cs
\n\nusing System.ComponentModel.DataAnnotations.Schema;\nusing System;\nusing System.Collections.Generic;\nusing System.ComponentModel.DataAnnotations;\nusing System.Linq;\nusing System.Threading.Tasks;\n\nnamespace workbranch.Models.HonorarTool\n{\n public partial class HonorarToolLists\n {\n public string Name { get; set; }\n\n public int Year { get; set; }\n\n public int ChargeableCosts { get; set; }\n\n public int Zone { get; set; }\n\n public int MinRate { get; set; }\n\n public int MaxRate { get; set; }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nAppDbContext.cs
\n\nusing System.Net.Sockets;\nusing System.ComponentModel.DataAnnotations;\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\nusing Microsoft.AspNetCore.Identity;\nusing Microsoft.AspNetCore.Identity.EntityFrameworkCore;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.EntityFrameworkCore;\nusing workbranch.Models.StatusReporting;\nusing workbranch.Models.HonorarTool;\n\nnamespace workbranch.Models.Db\n{\n public class AppDbContext : IdentityDbContext\n {\n public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)\n { }\n public DbSet<HonorarToolLists> HonorarToolLists { get; set; }\n\n protected override void OnModelCreating(ModelBuilder builder)\n {\n base.OnModelCreating(builder);\n\n builder.Seed();\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1543 次 |
| 最近记录: |