2

I want to create database EF migrations via the developer command prompt for VS2015. When I try to use this command line:

dotnet ef migrations add v1

I get this error:

The property 'partCategoriess' cannot be added to the entity type 'PartCategoryPart' because a navigation property with the same name already exists on entity type 'PartCategoryPart'.

Is anything wrong with the DbContext? I am trying to create a many-to-many table between categoryParts and parts.

public class ShoppingDbContext : IdentityDbContext<User>
{
    public ShoppingDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
    }

    public DbSet<PartCategory> PartCategories { get; set; }
    public DbSet<Part> Parts { get; set; }

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

        modelBuilder.Entity<PartCategoryPart>()
            .HasKey(t => new { t.partCategoriess, t.Part });

        modelBuilder.Entity<PartCategoryPart>()
            .HasOne(pt => pt.partCategoriess)
            .WithMany(p => p.PartCategoryPart)
            .HasForeignKey(pt => pt.PartCategoryId);

        modelBuilder.Entity<PartCategoryPart>()
            .HasOne(pt => pt.Part)
            .WithMany(t => t.PartCategoryPart)
            .HasForeignKey(pt => pt.PartId);
    }
}

public class PartCategoryPart
{
    public int Id { get; set; }

    public int PartCategoryId { get; set; }
    public PartCategory partCategoriess { get; set; }

    public int PartId { get; set; }
    public Part Part { get; set; }        
}

public class PartCategory
{
    public int PartCategoryId { get; set; }
    public string Category { get; set; }
    public List<ProductPartCategory> ProductPartCategories { get; set; }

    public List<PartCategoryPart> PartCategoryPart { get; set; }
}

public class Part 
{
    public int PartId { get; set; }
    public string Code { get; set; }       
    public string Name { get; set; }
    public double? Price { get; set; }
    public List<PartCategoryPart> PartCategoryPart { get; set; }
}

2 Answers 2

1

The problem here is how you are defining the primary key for the PartCategoryPart intermediate entity. You are using the navigation properties to define the PK and you have to use the FKs like this:

modelBuilder.Entity().HasKey(t => new { t.PartCategoryId, t.PartId});

Sign up to request clarification or add additional context in comments.

Comments

0

Referring to my own assignment, here's how you properly create an entity. You did not define a key..

modelBuilder.Entity<Price>()
            .HasKey(input => input.PriceId)
            .HasName("PrimaryKey_Price_PriceId");

        // Provide the properties of the PriceId column
        modelBuilder.Entity<Price>()
            .Property(input => input.PriceId)
            .HasColumnName("PriceId")
            .HasColumnType("int")
            .UseSqlServerIdentityColumn()
            .ValueGeneratedOnAdd()
            .IsRequired();

        //modelBuilder.Entity<Price>()
        //    .Property(input => input.MetricId)
        //    .HasColumnName("MetricId")
        //    .HasColumnType("int")
        //    .IsRequired();

        modelBuilder.Entity<Price>()
            .Property(input => input.Value)
            .HasColumnName("Value")
            .HasColumnType("DECIMAL(19,4)")
            .IsRequired();

        modelBuilder.Entity<Price>()
            .Property(input => input.RRP)
            .HasColumnName("RRP")
            .HasColumnType("DECIMAL(19,4)")
            .IsRequired(false);

        modelBuilder.Entity<Price>()
            .Property(input => input.CreatedAt)
            .HasDefaultValueSql("GetDate()");

        modelBuilder.Entity<Price>()
            .Property(input => input.DeletedAt)
            .IsRequired(false);

        // Two sets of Many to One relationship between User and ApplicationUser  entity (Start)
        modelBuilder.Entity<Price>()
         .HasOne(userClass => userClass.CreatedBy)
         .WithMany()
         .HasForeignKey(userClass => userClass.CreatedById)
         .OnDelete(DeleteBehavior.Restrict)
         .IsRequired();

        modelBuilder.Entity<Price>()
            .HasOne(userClass => userClass.DeletedBy)
            .WithMany()
            .HasForeignKey(userClass => userClass.DeletedById)
            .OnDelete(DeleteBehavior.Restrict);

Notice that after identifying which is the key you still need to declare its properties before you declare any relationships.

    modelBuilder.Entity<Price>()
            .HasKey(input => input.PriceId)
            .HasName("PrimaryKey_Price_PriceId");

        // Provide the properties of the PriceId column
        modelBuilder.Entity<Price>()
            .Property(input => input.PriceId)
            .HasColumnName("PriceId")
            .HasColumnType("int")
            .UseSqlServerIdentityColumn()
            .ValueGeneratedOnAdd()
            .IsRequired();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.