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

Foreign keys for tables marked as ExcludeFromMigrations regenerated in every migrations #23639

Closed
vitaliy-leschenko opened this issue Dec 9, 2020 · 2 comments
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@vitaliy-leschenko
Copy link

vitaliy-leschenko commented Dec 9, 2020

Foreign keys for tables marked as ExcludeFromMigrations regenerate in every migrations

Code sample

    public class ApplicationDbContext : DbContext, IApplicationDbContext
    {
        public DbSet<PhotoItem> PhotoItems { get; set; }
        public DbSet<LabImage> LabImages { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
        }
    }

    public class PhotoItem
    {
        public Guid PhotoItemId { get; set; }
        public Guid ImageId { get; set; }
        public virtual LabImage LabImage { get; set; }
    }

    public class LabImage
    {
        public Guid ImageId { get; set; }
    }

    public class LabImageConfiguration : IEntityTypeConfiguration<LabImage>
    {
        public void Configure(EntityTypeBuilder<LabImage> builder)
        {
            builder.ToTable("Image", "lab", t => t.ExcludeFromMigrations());
            builder.HasKey(t => t.ImageId);
        }
    }

    public class PhotoItemConfiguration : IEntityTypeConfiguration<PhotoItem>
    {
        public void Configure(EntityTypeBuilder<PhotoItem> builder)
        {
            builder.HasKey(t => t.PhotoItemId);
            builder.HasOne(t => t.LabImage).WithMany().HasForeignKey(t => t.ImageId).OnDelete(DeleteBehavior.NoAction);
        }
    }

Now try to create migration. First migration will be OK. But next migrations will always generate drop and create foreign key from PhotoItem to LabImage.

Include provider and version information

EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.8

@ajcvickers
Copy link
Member

/cc @AndriySvyryd @bricelam

@AndriySvyryd
Copy link
Member

@ajcvickers This is not a regression

@AndriySvyryd AndriySvyryd added this to the 5.0.2 milestone Dec 10, 2020
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 10, 2020
AndriySvyryd added a commit that referenced this issue Dec 10, 2020
AndriySvyryd added a commit that referenced this issue Dec 10, 2020
AndriySvyryd added a commit that referenced this issue Dec 10, 2020
AndriySvyryd added a commit to dotnet-maestro-bot/EntityFrameworkCore that referenced this issue Dec 10, 2020
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants