dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.72k stars 3.17k forks source link

Add-Migration isn't compatible with Cross-DbContext #17570

Closed Yuant-tobing closed 2 years ago

Yuant-tobing commented 5 years ago

Let say, I want to add a new table to my existing database, so I create a new DbContext and new model for this purpose. The new model for the table that I will create have a relationship with the existing model.

TobaRegionalContext (Exists)

    public partial class TobaRegionalContext : DbContext
    {
        ...

        public virtual DbSet<Country> Countries { get; set; }
        ...

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("ProductVersion", "3.0.0-preview5.19227.1");

            modelBuilder.Entity<Country>(entity =>
            {
                entity.ToTable("Country");

                entity.HasKey(e => e.Iso);

                ...
            });

Migration data 2019xxxxxxxxxx_InitialRegionalCreated.cs (Exists)

using Microsoft.EntityFrameworkCore.Migrations;

namespace Api.Areas.Regional.Data
{
    public partial class InitialRegionalCreated : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Country",
                columns: table => new
                {
                    Iso = table.Column<string>(fixedLength: true, maxLength: 2, nullable: false),
                    Name = table.Column<string>(maxLength: 80, nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Country", x => x.Iso);
                });

            ...
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Country");

            ...
        }
    }
}

To create this new model, I don't have a problem, but when I tried to migrate the data for the new model using command:


Add-Migration -Context NewOneInitialCreated

I found that the generator tried to create entities from my new model and entities for related models in the migration file and also the all model entity that has a relation to the related model.

My new DbContext TobaVehicleContext

    public partial class TobaRegionalContext : DbContext
    {
        ...

        public virtual DbSet<Car> Cars { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("ProductVersion", "3.0.0-preview5.19227.1");

            modelBuilder.Entity<Car>(entity =>
            {
                entity.ToTable("Car");

                entity.HasKey(e => e.LicenseNumber);

                ...
            });

Result migration data from PMC Add-Migration command. 2019xxxxxxxxxx_InitialVehicleCreated

using Microsoft.EntityFrameworkCore.Migrations;

namespace Api.Areas.Vehicle.Data
{
    public partial class InitialVehicleCreated : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Car",
                columns: table => new
                {
                    LicenseNumber = table.Column<string>(maxLength: 12, nullable: false),
                    ...
                },
                ....
        }
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Country",
                columns: table => new
                {
                    Iso = table.Column<string>(maxLength: 2, nullable: false),
                    ...
                },
                ....
        }
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Province",
                columns: table => new
                {
                    Iso = table.Column<string>(maxLength: 3, nullable: false),
                    ...
                },
                ....
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Car");
            migrationBuilder.DropTable(
                name: "Country");
            migrationBuilder.DropTable(
                name: "Province");
        }
    }
}

I know that I will get an error if I run this "Update-Database" command. What I want to ask, is it possible to make database relations on multiple DbContext? Or maybe I forget something. thank you

My .csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UserSecretsId>.................</UserSecretsId>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="3.0.0-preview5.19227.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0-preview5.19227.9" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0-preview5-19264-04" />
  </ItemGroup>

</Project>
ajcvickers commented 5 years ago

Duplicate of #2725