自数据库创建以来支持“...”上下文的模型已更改

Model backing the '...' context has changed since the database was created

我也有 enable-migration enablemigration=true。但是我有 table 定义来保存所有表单字段。它不断抛出这个错误。 " System.InvalidOperationException HResult=0x80131509 Message=支持 'eNtsaRegistration' 上下文的模型自数据库创建以来已更改。考虑使用代码优先迁移来更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)”。我想要实现的是当我从表单发送所有字段时,他们必须将其发送到我的 table 定义。我可以得到加载表单时所有国家/地区的列表以及 select 它们。我到底错过了什么?

        // Migration Folder. 
        using System;
        using Microsoft.EntityFrameworkCore.Migrations;
        
        namespace eNtsaRegistrationTraining.Migrations
        {
            public partial class eNtsaRegistration : Migration
            {
                protected override void Up(MigrationBuilder migrationBuilder)
                {
                    migrationBuilder.CreateTable(
                        name: "__MigrationHistory",
                        columns: table => new
                        {
                            MigrationId = table.Column<string>(maxLength: 150, nullable: false),
                            ContextKey = table.Column<string>(maxLength: 300, nullable: false),
                            Model = table.Column<byte[]>(nullable: false),
                            ProductVersion = table.Column<string>(maxLength: 32, nullable: false)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_dbo.__MigrationHistory", x => new { x.MigrationId, x.ContextKey });
                        });
        
                    migrationBuilder.CreateTable(
                        name: "AspNetRoles",
                        columns: table => new
                        {
                            Id = table.Column<string>(maxLength: 128, nullable: false),
                            Name = table.Column<string>(maxLength: 256, nullable: false),
                            DisplayName = table.Column<string>(nullable: true),
                            Description = table.Column<string>(nullable: true)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_AspNetRoles", x => x.Id);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "AspNetUsers",
                        columns: table => new
                        {
                            Id = table.Column<string>(maxLength: 128, nullable: false),
                            FirstName = table.Column<string>(nullable: true),
                            LastName = table.Column<string>(nullable: true),
                            LocalAddress = table.Column<string>(nullable: true),
                            PermanentAddress = table.Column<string>(nullable: true),
                            Email = table.Column<string>(maxLength: 256, nullable: true),
                            EmailConfirmed = table.Column<bool>(nullable: false),
                            PasswordHash = table.Column<string>(nullable: true),
                            SecurityStamp = table.Column<string>(nullable: true),
                            PhoneNumber = table.Column<string>(nullable: true),
                            PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                            TwoFactorEnabled = table.Column<bool>(nullable: false),
                            LockoutEndDateUtc = table.Column<DateTime>(type: "datetime", nullable: true),
                            LockoutEnabled = table.Column<bool>(nullable: false),
                            AccessFailedCount = table.Column<int>(nullable: false),
                            UserName = table.Column<string>(maxLength: 256, nullable: false),
                            ProfilePicPath = table.Column<string>(nullable: true)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_AspNetUsers", x => x.Id);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "Dietary",
                        columns: table => new
                        {
                            None = table.Column<string>(maxLength: 10, nullable: false),
                            Vegetarian = table.Column<string>(maxLength: 10, nullable: true),
                            Vegan = table.Column<string>(maxLength: 10, nullable: true),
                            Halaal = table.Column<string>(maxLength: 10, nullable: true),
                            Other = table.Column<string>(maxLength: 50, nullable: true)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_Dietary", x => x.None);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "Locations",
                        columns: table => new
                        {
                            Name = table.Column<string>(maxLength: 50, nullable: false),
                            Latitute = table.Column<decimal>(type: "numeric(18, 0)", nullable: true),
                            Longitute = table.Column<decimal>(type: "numeric(18, 0)", nullable: true),
                            Description = table.Column<string>(nullable: true)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_Locations", x => x.Name);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "TbTrainingForm",
                        columns: table => new
                        {
                            ID = table.Column<int>(nullable: true),
                            Title = table.Column<string>(maxLength: 50, nullable: false),
                            FirstName = table.Column<string>(maxLength: 50, nullable: false),
                            LastName = table.Column<string>(maxLength: 50, nullable: false),
                            Position = table.Column<string>(maxLength: 50, nullable: false),
                            Company = table.Column<string>(maxLength: 50, nullable: false),
                            StreetAddress = table.Column<string>(nullable: false),
                            StreetAddressLine = table.Column<string>(nullable: false),
                            City = table.Column<string>(maxLength: 50, nullable: false),
                            StateProvince = table.Column<string>(name: "State/Province", maxLength: 50, nullable: false),
                            ZipCode = table.Column<int>(name: "Zip/Code", nullable: false),
                            Email = table.Column<string>(maxLength: 50, nullable: false),
                            CellNumber = table.Column<int>(nullable: false),
                            DietaryRequirement = table.Column<string>(maxLength: 50, nullable: false)
                        },
                        constraints: table =>
                        {
                        });
        
                    migrationBuilder.CreateTable(
                        name: "AspNetUserClaims",
                        columns: table => new
                        {
                            Id = table.Column<int>(nullable: false)
                                .Annotation("SqlServer:Identity", "1, 1"),
                            UserId = table.Column<string>(maxLength: 128, nullable: false),
                            ClaimType = table.Column<string>(nullable: true),
                            ClaimValue = table.Column<string>(nullable: true)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
                            table.ForeignKey(
                                name: "FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId",
                                column: x => x.UserId,
                                principalTable: "AspNetUsers",
                                principalColumn: "Id",
                                onDelete: ReferentialAction.Cascade);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "AspNetUserLogins",
                        columns: table => new
                        {
                            LoginProvider = table.Column<string>(maxLength: 128, nullable: false),
                            ProviderKey = table.Column<string>(maxLength: 128, nullable: false),
                            UserId = table.Column<string>(maxLength: 128, nullable: false)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_dbo.AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey, x.UserId });
                            table.ForeignKey(
                                name: "FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId",
                                column: x => x.UserId,
                                principalTable: "AspNetUsers",
                                principalColumn: "Id",
                                onDelete: ReferentialAction.Cascade);
                        });
        
                    migrationBuilder.CreateTable(
                        name: "AspNetUserRoles",
                        columns: table => new
                        {
                            UserId = table.Column<string>(maxLength: 128, nullable: false),
                            RoleId = table.Column<string>(maxLength: 128, nullable: false)
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_dbo.AspNetUserRoles", x => new { x.UserId, x.RoleId });
                            table.ForeignKey(
                                name: "FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId",
                                column: x => x.RoleId,
                                principalTable: "AspNetRoles",
                                principalColumn: "Id",
                                onDelete: ReferentialAction.Cascade);
                            table.ForeignKey(
                                name: "FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId",
                                column: x => x.UserId,
                                principalTable: "AspNetUsers",
                                principalColumn: "Id",
                                onDelete: ReferentialAction.Cascade);
                        });
        
                    migrationBuilder.CreateIndex(
                        name: "RoleNameIndex",
                        table: "AspNetRoles",
                        column: "Name",
                        unique: true);
        
                    migrationBuilder.CreateIndex(
                        name: "IX_UserId",
                        table: "AspNetUserClaims",
                        column: "UserId");
        
                    migrationBuilder.CreateIndex(
                        name: "IX_UserId",
                        table: "AspNetUserLogins",
                        column: "UserId");
        
                    migrationBuilder.CreateIndex(
                        name: "IX_RoleId",
                        table: "AspNetUserRoles",
                        column: "RoleId");
        
                    migrationBuilder.CreateIndex(
                        name: "IX_UserId",
                        table: "AspNetUserRoles",
                        column: "UserId");
        
                    migrationBuilder.CreateIndex(
                        name: "UserNameIndex",
                        table: "AspNetUsers",
                        column: "UserName",
                        unique: true);
                }
        
                protected override void Down(MigrationBuilder migrationBuilder)
                {
                    migrationBuilder.DropTable(
                        name: "__MigrationHistory");
        
                    migrationBuilder.DropTable(
                        name: "AspNetUserClaims");
        
                    migrationBuilder.DropTable(
                        name: "AspNetUserLogins");
        
                    migrationBuilder.DropTable(
                        name: "AspNetUserRoles");
        
                    migrationBuilder.DropTable(
                        name: "Dietary");
        
                    migrationBuilder.DropTable(
                        name: "Locations");
        
                    migrationBuilder.DropTable(
                        name: "TbTrainingForm");
        
                    migrationBuilder.DropTable(
                        name: "AspNetRoles");
        
                    migrationBuilder.DropTable(
                        name: "AspNetUsers");
                }
            }
        }
    
    // Controller
    public ActionResult SaveRegForm()
            {
                ViewBag.Message = "Details saved successfull";
                return View(db.TrainingRegs.ToList()); // Error is thrown here.
            }
    
       //GET:TrainingRegForm/Submit.
           public ActionResult SubmitRegDetails()
            {
                // Initialization.
                this.ViewBag.CountryList = this.GetCountryList();
                this.ViewBag.SaveRegForm = this.SaveRegForm();
    
                RegViewAndRoleViewModel model = new RegViewAndRoleViewModel();
    
                return View(model);
            }
    
    // Model
      public partial class TbTrainingForm
        {
            [Key]
            public Guid? Id { get; set; }
            public string Title { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Position { get; set; }
            public string Company { get; set; }
            
            public string StreetAddress { get; set; }
    
           
            public string StreetAddressLine { get; set; }
    
            
            public string City { get; set; }
    
            public string StateProvince { get; set; }
    
            
            public int ZipCode { get; set; }
    
            public string Country { get; set; }
    
           
            public string Email { get; set; }
    
            
            public int CellNumber { get; set; }
            public string DietaryRequirement { get; set; }
        }

using System.Data.Entity;
using eNtsaRegistrationTraining.Models;

namespace eNtsaRegistrationTraining.DAL
{
    public class eNtsaRegistration:DbContext
    {

        public eNtsaRegistration() : base("eNtsaRegistration")
        {
        }  public DbSet<TrainingRegForm> TrainingRegs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
 }

// View
@model eNtsaRegistrationTraining.Models.RegViewAndRoleViewModel

@using (Html.BeginForm("SaveRegForm", "Home", FormMethod.Post))
                    {


                        <div class="form-horizontal">

                            <hr />

                            <div class="form-group row">
                                <label for="Title" class="col-sm-2 col-form-label">Title</label>
                                <div class="col-sm-3 ">
                                    @Html.EditorFor(model => model.RegForm.Title, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.RegForm.Title, "", new { @class = "text-danger" })

                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="Name" class="col-sm-2 col-form-label">Name:</label>
                                <div class="col-sm-3 ">
                                    @Html.EditorFor(model => model.RegForm.FirstName, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Name" } })
                                    @Html.ValidationMessageFor(model => model.RegForm.FirstName, "", new { @class = "text-danger" })

                                </div>

}

生成的大部分 Entity Framework 相关 .cs 文件 类(映射视图)应与您的数据库和 table 定义。

当映射视图与您在数据库或(修改、迁移)DbContext 中的最新更改不匹配时,您会收到此错误消息。

您是否安装了 EF 电动工具扩展?您可以在 Visual Studio 扩展程序菜单中在线找到它。

https://marketplace.visualstudio.com/items?itemName=ErikEJ.EntityFramework6PowerToolsCommunityEdition

..安装的时候有一个右键可以为你的DbContext生成新的映射视图文件

https://www.c-sharpcorner.com/UploadFile/ff2f08/pre-generated-views-with-a-code-first-model-entity-framework/