Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用实体框架和迁移创建数据库时出现问题_C#_Database_Asp.net Core Mvc_Database Migration_Entity Framework Core - Fatal编程技术网

C# 尝试使用实体框架和迁移创建数据库时出现问题

C# 尝试使用实体框架和迁移创建数据库时出现问题,c#,database,asp.net-core-mvc,database-migration,entity-framework-core,C#,Database,Asp.net Core Mvc,Database Migration,Entity Framework Core,我想通过VS2015的开发者命令提示符创建数据库EF迁移。当我尝试使用此命令行时: dotnet ef migrations add v1 我得到这个错误: 无法将属性“PartCategory”添加到实体类型 “PartCategoryPart”是因为导航属性具有相同的名称 实体类型“PartCategoryPart”上已存在 DbContext有什么问题吗?我正在尝试在categoryParts和parts之间创建一个多对多表 public class ShoppingDbContext

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

dotnet ef migrations add v1
我得到这个错误:

无法将属性“PartCategory”添加到实体类型 “PartCategoryPart”是因为导航属性具有相同的名称 实体类型“PartCategoryPart”上已存在

DbContext
有什么问题吗?我正在尝试在
categoryParts
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; }
}
公共类ShoppingDbContext:IdentityDbContext
{
public ShoppingDbContext(DbContextOptions选项):基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
基本配置(选项生成器);
}
公共DbSet PartCategories{get;set;}
公共DbSet部分{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasKey(t=>new{t.partCategoriess,t.Part});
modelBuilder.Entity()
.HasOne(pt=>pt.partCategoriess)
.WithMany(p=>p.PartCategoryPart)
.HasForeignKey(pt=>pt.PartCategoryId);
modelBuilder.Entity()
.HasOne(pt=>pt.Part)
.WithMany(t=>t.PartCategoryPart)
.HasForeignKey(pt=>pt.PartId);
}
}
公共类PartCategoryPart
{
公共int Id{get;set;}
公共int PartCategoryId{get;set;}
公共PartCategory PartCategory属性{get;set;}
公共int PartId{get;set;}
公共部分{get;set;}
}
公共类零件类别
{
公共int PartCategoryId{get;set;}
公共字符串类别{get;set;}
公共列表ProductPartCategories{get;set;}
公共列表PartCategoryPart{get;set;}
}
公共课部分
{
公共int PartId{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共双价{get;set;}
公共列表PartCategoryPart{get;set;}
}

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

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

关于我自己,这里是如何正确创建实体的。您没有定义密钥

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()
.HasKey(输入=>input.PriceId)
.HasName(“PrimaryKey_Price_PriceId”);
//提供PriceId列的属性
modelBuilder.Entity()
.Property(输入=>input.PriceId)
.HasColumnName(“PriceId”)
.HasColumnType(“int”)
.UseSqlServerIdentityColumn()
.ValueGeneratedOnAdd()
.IsRequired();
//modelBuilder.Entity()
//.Property(input=>input.MetricId)
//.HasColumnName(“MetricId”)
//.HasColumnType(“int”)
//.IsRequired();
modelBuilder.Entity()
.Property(输入=>input.Value)
.HasColumnName(“值”)
.HasColumnType(“十进制(19,4)”)
.IsRequired();
modelBuilder.Entity()
.Property(input=>input.RRP)
.名称(“RRP”)
.HasColumnType(“十进制(19,4)”)
.i被要求(虚假);
modelBuilder.Entity()
.Property(输入=>input.CreatedAt)
.HasDefaultValueSql(“GetDate()”);
modelBuilder.Entity()
.Property(输入=>input.DeletedAt)
.i被要求(虚假);
//用户和ApplicationUser实体之间的两组多对一关系(开始)
modelBuilder.Entity()
.HasOne(userClass=>userClass.CreatedBy)
.有很多
.HasForeignKey(userClass=>userClass.CreatedById)
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
modelBuilder.Entity()
.HasOne(userClass=>userClass.DeletedBy)
.有很多
.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();
modelBuilder.Entity()
.HasKey(输入=>input.PriceId)
.HasName(“PrimaryKey_Price_PriceId”);
//提供PriceId列的属性
modelBuilder.Entity()
.Property(输入=>input.PriceId)
.HasColumnName(“PriceId”)
.HasColumnType(“int”)
.UseSqlServerIdentityColumn()
.ValueGeneratedOnAdd()
.IsRequired();