EF Core Add-Migration generating extra column with ColumnName1

.net-core ef-code-first ef-fluent-api entity-framework-core sql-server

Question

I have the following entities when I generate migration it creates two columns with name RestrictedCategoryId and RestrictedCategoryId1(FK). How to solve this issue to generate only one column with FK?

Note: I need OrderId in each entity.

`C#

public class Order
{
    public Guid Id { get; set; }
    public DateTime OrderDate { get; set; }

    private List<Category> _categories;
    public List<Category> Categories => _categories;
}
public class Category
{
    public Guid Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }

    public Guid OrderId { get; set; }
    public Order Order { get; set; }

    private List<RestrictionCategory> _restrictedCategories;
    public List<RestrictionCategory> RestrictedCategories => _restrictedCategories;
}
public class RestrictionCategory
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid OrderId { get; set; }
    public Order Order { get; set; }
    public Guid CategoryId { get; set; }
    public Category Category { get; set; }        
    public Guid RestrictedCategoryId { get; set; }
    public Category RestrictedCategory { get; set; }
}
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.HasKey(o => o.Id);
        builder.Property(o => o.Id).IsRequired();
    }
}
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
    public void Configure(EntityTypeBuilder<Category> builder)
    {
        builder.HasKey(c => new { c.Id, c.OrderId });
        builder.Property(o => o.Id).IsRequired();
        builder.Property(o => o.OrderId).IsRequired();

        builder.HasMany(c => c.RestrictedCategories).WithOne(cr => cr.Category)
            .HasForeignKey(cr => new { cr.CategoryId, cr.OrderId 
}).OnDelete(DeleteBehavior.NoAction);
    }
}
public class RestrictionCategoryConfiguration : IEntityTypeConfiguration<RestrictionCategory>
{
    public void Configure(EntityTypeBuilder<RestrictionCategory> builder)
    {
        builder.HasKey(c => new { c.Id, c.OrderId });
        builder.Property(o => o.Id).IsRequired();
        builder.Property(o => o.OrderId).IsRequired();

        builder.HasIndex(cr => new { cr.RestrictedCategoryId, cr.OrderId });
    }
}

` The entities resembles to actual ones.

1
1
4/2/2020 10:03:30 AM

Accepted Answer

Actually you get two additional columns:

RestrictedCategoryId = table.Column<Guid>(nullable: false),
RestrictedCategoryId1 = table.Column<Guid>(nullable: true), // <--
RestrictedCategoryOrderId = table.Column<Guid>(nullable: true) // <--

Apparently EF Core Foreign Key Conventions doesn't play well with composite keys, so you have to explicitly configure the relationship - similar to what you did for the other relationship, just since your model has no corresponding collection navigation property you have to use HasMany with generic type argument and no parameters, e.g. inside CategoryConfiguration:

builder.HasMany<RestrictionCategory>()
    .WithOne(cr => cr.RestrictedCategory)
    .HasForeignKey(cr => new { cr.RestrictedCategoryId, cr.OrderId})
    .OnDelete(DeleteBehavior.NoAction);
2
4/2/2020 11:43:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow