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

Error on entity.HasKey line in specific model #35450

Closed
marcelaopimenta opened this issue Jan 10, 2025 · 2 comments
Closed

Error on entity.HasKey line in specific model #35450

marcelaopimenta opened this issue Jan 10, 2025 · 2 comments

Comments

@marcelaopimenta
Copy link

Hello Folks!

I generated the database models via Entity Framework scaffolding.

dotnet ef dbcontext scaffold "Server=.;Database=<DataBase>;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

I have a key in a table that is between a GUID field and a LONG field (Bigint in Sql Server). It generates the classes.

Image

But when it passes through this point in the generated DbContext File the code it crashes,

Image

modelBuilder.Entity<Order>(entity =>
{
    entity.HasKey(e => new { e.Id, e.OrderId }).HasName("PK_Orders_1");

    entity.Property(e => e.Id).HasDefaultValueSql("(newid())");
    entity.Property(e => e.OrderId).ValueGeneratedOnAdd();
    entity.Property(e => e.Address)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.City)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Complement)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Country)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.CountryPrefix)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Created).HasColumnType("datetime");
    entity.Property(e => e.Currency)
        .HasMaxLength(3)
        .IsUnicode(false);
    entity.Property(e => e.DocumentId)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Modified).HasColumnType("datetime");
    entity.Property(e => e.Neighborhood)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Number)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.ProductGroup)
        .HasMaxLength(50)
        .IsUnicode(false);
    entity.Property(e => e.ReturnProcess).HasColumnType("text");
    entity.Property(e => e.State)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.Status)
        .HasMaxLength(50)
        .IsUnicode(false);
    entity.Property(e => e.Type)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.VatId)
        .HasMaxLength(255)
        .IsUnicode(false);
    entity.Property(e => e.ZipCode)
        .HasMaxLength(255)
        .IsUnicode(false);

    entity.HasOne(d => d.Customer).WithMany(p => p.Orders)
        .HasForeignKey(d => d.CustomerId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_Orders_Customers");

    entity.HasOne(d => d.PaymentGateway).WithMany(p => p.Orders)
        .HasForeignKey(d => d.PaymentGatewayId)
        .OnDelete(DeleteBehavior.ClientSetNull)
        .HasConstraintName("FK_Orders_PaymentGateways");
});

it doesn't return an error or anything, it just consumes memory until the machine crashes. I imagine it's because of mixing two different types, but I couldn't solve it.

Image

Can you help?

Thanks!

EF Core version: 9.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 9.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.12.13

@AndriySvyryd
Copy link
Member

@marcelaopimenta This certainly looks like a bug. Can you share a small runnable project that shows the issue?

@marcelaopimenta
Copy link
Author

@marcelaopimenta This certainly looks like a bug. Can you share a small runnable project that shows the issue?

Hi Andriy,

I found the cause of the error in the post below on StackOverflow.

https://stackoverflow.com/questions/76668197/ef-core-ends-in-infinite-loop

The problem was that in many-to-many relationships it generated the class with the same name as the property in several cases. So getting into a infinity loop.

I solved it by adding the --data-annotations parameter to the scaffolding command.

Thanks!

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