'How does Entity Framework map to tables in SQL?

How does EF handle the mapping to the SQL tables? I understand the general idea, but I changed the name of my model in C#, but it's still pointing to the table when using the context object. I expected it to break, but I am guessing it is cached somewhere? Is that how it is handled deep inside EF somewhere?

More detail: This is persistent when the console app stops, and then restarts. The model has a different name, but EF still somehow goes to the table.



Solution 1:[1]

Well normally in an EF Core Project with EntityFrameworkCore you have a file called ApplicationDbContextModalSnapshot that describes the state of your entities and the SQL tables they map to http://prntscr.com/26lj4ow So in this file, which is updated with every migration, you can find code like this http://prntscr.com/26lj5lf

modelBuilder.Entity("Identity.Data.Domain.Locale", b =>
            {
                b.Property<Guid>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("uniqueidentifier");

                b.Property<string>("Code")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Description")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Name")
                    .HasColumnType("nvarchar(max)");

                b.HasKey("Id");

                b.ToTable("Locales");
            });

Here we are mapping an entity called Identity.Data.Domain.Locale to table Locales. If you change the entity model name from Locale to LanguageLocale and we update ApplicationDbContextModalSnapshot we get LanguageLocale entity mapped to Locales table http://prntscr.com/26lj9dz

        modelBuilder.Entity("Identity.Data.Domain.LanguageLocale", b =>
            {
                b.Property<Guid>("Id")
                    .ValueGeneratedOnAdd()
                    .HasColumnType("uniqueidentifier");

                b.Property<string>("Code")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Description")
                    .HasColumnType("nvarchar(max)");

                b.Property<string>("Name")
                    .HasColumnType("nvarchar(max)");

                b.HasKey("Id");

                b.ToTable("Locales");
            });

In ApplicationDbContext OnModelCreating you can override the default entity model to table mapping (which is normally [EntityModelName] to SQL tables [EntityModelNames] with code similar to this

  foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            var table = entityType.GetTableName();
            entityType.SetTableName("Identity_" + table);
        }

Here we are setting a mapping to each entity in our project to map to SQL table with a name Identity_[EntityName]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1