Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code-first migration creates script for unwanted tables #20979

Closed
steamprodz opened this issue May 18, 2020 · 1 comment
Closed

Code-first migration creates script for unwanted tables #20979

steamprodz opened this issue May 18, 2020 · 1 comment

Comments

@steamprodz
Copy link

steamprodz commented May 18, 2020

Hello!

I am using EF Core and tried to implement a code-first migration according to the docs.
This is my entity model:

public class Company
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    [Required]
    public string CompanyName { get; set; }
    public string CompanyDescription { get; set; }
    public string CompanyType { get; set; }

    public Guid UserId { get; set; }
    public UserIdentity User { get; set; }

    public long SubscriptionId { get; set; }
    public Subscription Subscription { get; set; }
}

DbContext:

public class CompanyDbContext : DbContext, ICompanyDbContext
{
    public DbSet<Company> Companies { get; set; }

    public CompanyDbContext(DbContextOptions<CompanyDbContext> options)
        : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        ConfigureCompanyContext(builder);
    }

    private void ConfigureCompanyContext(ModelBuilder builder)
    {
        builder.Entity<Company>().ToTable(TableConsts.Company);
    }
}

The migration files are created using command
dotnet ef migrations add DbInit -c CompanyDbContext -o "Migrations\Company" -p ..\Admin.EntityFramework.SqlServer\Admin.EntityFramework.SqlServer.csproj

I expect to get a migration script for creating Company table. However, I get also script for creating Subscription table, SubscriptionType table (reference/foreign key nested in Subscription class) and IdentityUser table. But I want to create only Company table and have others being referenced by foreign keys.

The migration script:

public partial class DbInit : Migration
{
	protected override void Up(MigrationBuilder migrationBuilder)
	{
		migrationBuilder.CreateTable(
			name: "SubscriptionType",
			columns: table => new
			{
				Id = table.Column<long>(nullable: false)
					.Annotation("SqlServer:Identity", "1, 1"),
				Name = table.Column<string>(nullable: false),
				UserCount = table.Column<int>(nullable: false),
				MaxPeriod = table.Column<DateTime>(type: "Date", nullable: false)
			},
			constraints: table =>
			{
				table.PrimaryKey("PK_SubscriptionType", x => x.Id);
			});

		migrationBuilder.CreateTable(
			name: "UserIdentity",
			columns: table => new
			{
				Id = table.Column<string>(nullable: false),
				UserName = table.Column<string>(nullable: true),
				// ..
			},
			constraints: table =>
			{
				table.PrimaryKey("PK_UserIdentity", x => x.Id);
			});

		migrationBuilder.CreateTable(
			name: "Subscription",
			columns: table => new
			{
				Id = table.Column<long>(nullable: false)
					.Annotation("SqlServer:Identity", "1, 1"),
				Name = table.Column<string>(nullable: false),
				CompanyId = table.Column<long>(nullable: false),
				UserId = table.Column<long>(nullable: false),
				SubscriptionTypeId = table.Column<long>(nullable: true),
				ExpirationDate = table.Column<DateTime>(type: "Date", nullable: false)
			},
			constraints: table =>
			{
				table.PrimaryKey("PK_Subscription", x => x.Id);
				table.ForeignKey(
					name: "FK_Subscription_SubscriptionType_SubscriptionTypeId",
					column: x => x.SubscriptionTypeId,
					principalTable: "SubscriptionType",
					principalColumn: "Id",
					onDelete: ReferentialAction.Restrict);
			});

		migrationBuilder.CreateTable(
			name: "Company",
			columns: table => new
			{
				Id = table.Column<Guid>(nullable: false),
				CompanyName = table.Column<string>(nullable: false),
				CompanyDescription = table.Column<string>(nullable: true),
				CompanyType = table.Column<string>(nullable: true),
				UserId = table.Column<Guid>(nullable: false),
				UserId1 = table.Column<string>(nullable: true),
				SubscriptionId = table.Column<long>(nullable: false)
			},
			constraints: table =>
			{
				table.PrimaryKey("PK_Company", x => x.Id);
				table.ForeignKey(
					name: "FK_Company_Subscription_SubscriptionId",
					column: x => x.SubscriptionId,
					principalTable: "Subscription",
					principalColumn: "Id",
					onDelete: ReferentialAction.Cascade);
				table.ForeignKey(
					name: "FK_Company_UserIdentity_UserId1",
					column: x => x.UserId1,
					principalTable: "UserIdentity",
					principalColumn: "Id",
					onDelete: ReferentialAction.Restrict);
			});

		migrationBuilder.CreateIndex(
			name: "IX_Company_SubscriptionId",
			table: "Company",
			column: "SubscriptionId");

		migrationBuilder.CreateIndex(
			name: "IX_Company_UserId1",
			table: "Company",
			column: "UserId1");

		migrationBuilder.CreateIndex(
			name: "IX_Subscription_SubscriptionTypeId",
			table: "Subscription",
			column: "SubscriptionTypeId");
	}

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

		migrationBuilder.DropTable(
			name: "Subscription");

		migrationBuilder.DropTable(
			name: "UserIdentity");

		migrationBuilder.DropTable(
			name: "SubscriptionType");
	}
}

Is it a correct behavior? I guess it shouldn't work this way.

@ajcvickers
Copy link
Member

@steamprodz EF Core will create migrations for all entity types in your EF model. Being able to exclude certain types from the migration is tracked by #2725. Until that issue is implemented the way to handle this is to delete from the migration the parts associated with types that you want to exclude.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants