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

dotnet ef migrations add freezes #29826

Closed
klinki opened this issue Dec 11, 2022 · 1 comment · Fixed by #29849
Closed

dotnet ef migrations add freezes #29826

klinki opened this issue Dec 11, 2022 · 1 comment · Fixed by #29849
Labels
area-model-building 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

@klinki
Copy link

klinki commented Dec 11, 2022

Hello,

I have problems with migrations freezing. I tried using Local SQL Server and SQLite database, it was freezing with both, so it seems provider independent.

Here is very shortened part of my entities and mapping:

public abstract class Investment 
{
    public int Id { get; set; }
}

public class Bond : Investment 
{
   public virtual ICollection<Coupon> Coupons { get; set; }
} 

public class Stock : Investment 
{
    public virtual ICollection<Dividend> Dividends { get; set; }
}


public abstract class InvestmentYield
{
    public int Id { get; set; }
    public Investment Investment { get; set; }
    public int InvestmentId { get; set; }
}

And now mapping:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var investment = modelBuilder.Entity<Investment>();
        investment.UseTpcMappingStrategy();

        modelBuilder.Entity<Stock>();
        modelBuilder.Entity<Bond>();

        modelBuilder.Entity<InvestmentYield>().UseTpcMappingStrategy();

        // This is the part of code which causes migrations to freeze.
        // To prevent freezes, remove (or comment) code below:

        var dividend = modelBuilder.Entity<Dividend>();
        dividend.Property(e => e.InvestmentId)
            .HasColumnName("stock_id");

        dividend.HasOne<Stock>()
            .WithMany(s => s.Dividends)
            .HasForeignKey(e => e.InvestmentId);

        var coupon = modelBuilder.Entity<Coupon>();
        coupon.Property(e => e.InvestmentId)
            .HasColumnName("bond_id");

        coupon.HasOne<Bond>()
            .WithMany(b => b.Coupons)
            .HasForeignKey(e => e.InvestmentId);

   }

Here is repo with sample project for reproduction:
https://github.com/klinki/ef-core-issue

Include verbose output

Here is verbose output from dotnet ef migrations add MappingMigration. There is not much to see there, it freezes basically right away.

Using project 'C:\projects\own\ef-tpc\ef-tpc.csproj'.
Using startup project 'C:\projects\own\ef-tpc\ef-tpc.csproj'.
...

Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'ef-tpc'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'AppDbContext'.
Finding DbContext classes in the project...
Using context 'AppDbContext'.

Include provider and version information

EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer (same happens also for Microsoft.EntityFrameworkCore.Sqlite, maybe it is provider independent issue)
Target framework: .NET 7
Operating system: Windows 10
IDE: Visual Studio 2022 17.4

@ajcvickers
Copy link
Member

ajcvickers commented Dec 12, 2022

@AndriySvyryd Hangs in model building; minimal repro below. Regression from 6.0. Still fails on latest main.

public class Bond : Investment
{
    public virtual ICollection<Coupon> Coupons { get; set; }
}

public class Coupon : InvestmentYield
{
}

public class Dividend : InvestmentYield
{
}

public abstract class Investment
{
    public int Id { get; set; }
}

public abstract class InvestmentYield
{
    public int Id { get; set; }

    public Investment Investment { get; set; }
    public int InvestmentId { get; set; }
}

public class Stock : Investment
{
    public virtual ICollection<Dividend> Dividends { get; set; }
}

public class SomeDbContext : DbContext
{
    public DbSet<Investment> Investments { get; set; }
    public DbSet<Stock> Stocks { get; set; }
    public DbSet<Bond> Bonds { get; set; }
    public DbSet<InvestmentYield> Yields { get; set; }
    public DbSet<Dividend> Dividends { get; set; }
    public DbSet<Coupon> Coupons { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Dividend>().HasOne<Stock>()
            .WithMany(s => s.Dividends)
            .HasForeignKey(e => e.InvestmentId);

        // Model building hangs on this line:
        modelBuilder.Entity<Coupon>().HasOne<Bond>()
            .WithMany(b => b.Coupons)
            .HasForeignKey(e => e.InvestmentId);
    }
}

public class Program
{
    public static async Task Main()
    {
        using (var context = new SomeDbContext())
        {
            Console.WriteLine("Building model...");
            var model = context.Model;
            Console.WriteLine("Model built.");
        }
    }
}

@AndriySvyryd AndriySvyryd added this to the 7.0.x milestone Dec 14, 2022
@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 14, 2022
@AndriySvyryd AndriySvyryd removed their assignment Dec 14, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.x, 7.0.3 Dec 15, 2022
@AndriySvyryd AndriySvyryd reopened this Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building 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

Successfully merging a pull request may close this issue.

3 participants