3

VS2015の開発者コマンドプロンプトからデータベースEF移行を作成したいとします。私は、このコマンドラインを使用しようとすると:Entity Frameworkと移行を使用してデータベースを作成しようとするときの問題

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(); 
関連する問題