EF Core 6 - How to fix inconsistency between the snapshot and migrations?

Cenk 956 Reputation points
2022-08-07T09:41:21.04+00:00

Hello,

There is an inconsistency when editing relationships between entities. I also deleted a migration, How can I fix this inconsistency? I expect your support.

Error: Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'VendorId'.
Invalid column name 'VendorId'.

Here is the snapshot:

using System;  
using IMS.Plugins.EFCore;  
using Microsoft.EntityFrameworkCore;  
using Microsoft.EntityFrameworkCore.Infrastructure;  
using Microsoft.EntityFrameworkCore.Metadata;  
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;  
  
#nullable disable  
  
namespace IMS.Plugins.EFCore.Migrations  
{  
    [DbContext(typeof(IMSContext))]  
    partial class IMSContextModelSnapshot : ModelSnapshot  
    {  
        protected override void BuildModel(ModelBuilder modelBuilder)  
        {  
#pragma warning disable 612, 618  
            modelBuilder  
                .HasAnnotation("ProductVersion", "6.0.7")  
                .HasAnnotation("Relational:MaxIdentifierLength", 128);  
  
            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);  
  
            modelBuilder.Entity("IMS.CoreBusiness.Customer", b =>  
                {  
                    b.Property<int>("CustomerId")  
                        .ValueGeneratedOnAdd()  
                        .HasColumnType("int");  
  
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("CustomerId"), 1L, 1);  
  
                    b.Property<string>("Address")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("AssistantResponsibleName")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("DeliveryAddress")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("Email")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("MainResponsibleName")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("Name")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("PhoneNumber")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("TaxAdministration")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<int>("TaxNumber")  
                        .HasColumnType("int");  
  
                    b.HasKey("CustomerId");  
  
                    b.ToTable("Customers");  
                });  
  
            modelBuilder.Entity("IMS.CoreBusiness.Order", b =>  
                {  
                    b.Property<int>("Id")  
                        .ValueGeneratedOnAdd()  
                        .HasColumnType("int");  
  
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);  
  
                    b.Property<string>("CustomerName")  
                        .IsRequired()  
                        .HasMaxLength(250)  
                        .HasColumnType("nvarchar(250)");  
  
                    b.Property<string>("DoneBy")  
                        .IsRequired()  
                        .HasMaxLength(50)  
                        .HasColumnType("nvarchar(50)");  
  
                    b.Property<DateTime>("OrderDateTime")  
                        .HasColumnType("datetime2");  
  
                    b.Property<string>("Status")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.HasKey("Id");  
  
                    b.ToTable("Orders");  
                });  
  
            modelBuilder.Entity("IMS.CoreBusiness.OrderDetail", b =>  
                {  
                    b.Property<int>("Id")  
                        .ValueGeneratedOnAdd()  
                        .HasColumnType("int");  
  
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);  
  
                    b.Property<int>("BuyQuantity")  
                        .HasColumnType("int");  
  
                    b.Property<double>("CostRatio")  
                        .HasColumnType("float");  
  
                    b.Property<string>("Description")  
                        .IsRequired()  
                        .HasMaxLength(400)  
                        .HasColumnType("nvarchar(400)");  
  
                    b.Property<int>("OrderId")  
                        .HasColumnType("int");  
  
                    b.Property<string>("ProductCode")  
                        .IsRequired()  
                        .HasMaxLength(100)  
                        .HasColumnType("nvarchar(100)");  
  
                    b.Property<string>("ProductName")  
                        .IsRequired()  
                        .HasMaxLength(250)  
                        .HasColumnType("nvarchar(250)");  
  
                    b.Property<int>("SellQuantity")  
                        .HasColumnType("int");  
  
                    b.Property<string>("ShippingNumber")  
                        .IsRequired()  
                        .HasMaxLength(150)  
                        .HasColumnType("nvarchar(150)");  
  
                    b.Property<string>("Status")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<double>("TotalBuyPrice")  
                        .HasColumnType("float");  
  
                    b.Property<double>("TotalSellPrice")  
                        .HasColumnType("float");  
  
                    b.Property<string>("TrackingNumber")  
                        .IsRequired()  
                        .HasMaxLength(150)  
                        .HasColumnType("nvarchar(150)");  
  
                    b.Property<double>("UnitCost")  
                        .HasColumnType("float");  
  
                    b.Property<int>("VendorId")  
                        .HasColumnType("int");  
  
                    b.HasKey("Id");  
  
                    b.HasIndex("OrderId");  
  
                    b.HasIndex("VendorId");  
  
                    b.ToTable("OrdersDetail");  
                });  
  
            modelBuilder.Entity("IMS.CoreBusiness.Vendor", b =>  
                {  
                    b.Property<int>("Id")  
                        .ValueGeneratedOnAdd()  
                        .HasColumnType("int");  
  
                    SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);  
  
                    b.Property<string>("Address")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("AssistantResponsibleName")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("Email")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("MainResponsibleName")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("Name")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.Property<string>("PhoneNumber")  
                        .IsRequired()  
                        .HasColumnType("nvarchar(max)");  
  
                    b.HasKey("Id");  
  
                    b.ToTable("Vendors");  
                });  
  
              
            modelBuilder.Entity("IMS.CoreBusiness.OrderDetail", b =>  
                {  
                    b.HasOne("IMS.CoreBusiness.Order", "Order")  
                        .WithMany("OrderDetails")  
                        .HasForeignKey("OrderId")  
                        .OnDelete(DeleteBehavior.Cascade)  
                        .IsRequired();  
  
                    b.HasOne("IMS.CoreBusiness.Vendor", "Vendor")  
                        .WithMany("OrderDetails")  
                        .HasForeignKey("VendorId")  
                        .OnDelete(DeleteBehavior.Cascade)  
                        .IsRequired();  
  
                    b.Navigation("Order");  
  
                    b.Navigation("Vendor");  
                });  
  
              
  
            modelBuilder.Entity("IMS.CoreBusiness.Order", b =>  
                {  
                    b.Navigation("OrderDetails");  
                });  
  
             
  
            modelBuilder.Entity("IMS.CoreBusiness.Vendor", b =>  
                {  
                    b.Navigation("OrderDetails");  
                });  
#pragma warning restore 612, 618  
        }  
    }  
}  

228848-tables.png

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,191 Reputation points
    2022-08-07T10:27:43.963+00:00

    I also deleted a migration, How can I fix this inconsistency?

    Physically deleting a migration file is a problem because the migration file has a Back method which rolls back that migration. Put the migration file back if you can. If you take the time read the openly published documentation you are supposed to rollback migrations using the CLI update-database command.

    update-database <rollback the database to this migration>  
    remove-migration <the name of the migration>  
    

    This community has no idea which migration you deleted in a series of possible migrations in your project. As I understand the snapshot file is supposed to revert if a migration file is deleted but I think that only works if you deleted the last migration. If you cannot put the file back then you probably need to delete the database, all migration files, and start over. Keep in mind, the database also contains migration history. Once the files and the database are deleted, create a new migration, update the database, and that should sync the schema with your models unless you have other issues with the design.

    In the future, and as recommended in your many other posts, read the documentation rather than guessing how a code first migrations work.

    Managing Migrations


1 additional answer

Sort by: Most helpful
  1. Wellington Faustino 1 Reputation point
    2022-08-08T01:45:42.837+00:00

    Apparently it's a local problem in your development environment, so solve it by creating the column manually. Because, as mentioned here, we have no idea of the history of changes in your environment (physical database or migrations)

    Hope this helps

    0 comments No comments