3

我想通过开发人员命令提示符为VS2015创建数据库EF迁移。当我尝试使用以下命令行:尝试使用实体框架和迁移创建数据库时遇到问题

dotnet ef migrations add v1 

我得到这个错误:

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'.

是什么毛病DbContext?我正在尝试创建一个介于categoryPartsparts之间的多对多表。

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; } 
} 

回答

1

这里的问题是你如何定义PartCategoryPart中间实体的主键。您正在使用导航属性来定义的PK,你必须使用FKS是这样的:

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

0

参照我自己assignment,这里是你如何正确地创建一个实体。你没有定义一个键。

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); 

请注意,在确定哪个是关键字之后,在声明任何关系之前仍需要声明其属性。

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();