【问题标题】:Trouble when trying to create a database with Entity Framework and migrations尝试使用实体框架和迁移创建数据库时出现问题
【发布时间】:2017-02-04 06:27:21
【问题描述】:

我想通过 VS2015 的开发人员命令提示符创建数据库 EF 迁移。当我尝试使用这个命令行时:

dotnet ef migrations add v1

我收到此错误:

无法将属性“partCategories”添加到实体类型 'PartCategoryPart' 因为有同名的导航属性 实体类型“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; }
}

【问题讨论】:

    标签: c# database asp.net-core-mvc database-migration entity-framework-core


    【解决方案1】:

    参考我自己的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();
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-10
        • 1970-01-01
        • 2014-07-08
        • 1970-01-01
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多