EF核心多对多关系身份插入错误

时间:2019-05-01 11:33:31

标签: c# entity-framework-core

我已按照本指南在我的API https://www.learnentityframeworkcore.com/configuration/many-to-many-relationship-configuration

中实现了多对多关系

当我尝试创建一个新的Project条目时,其中包含要保存到ProjectAreas链接表中的区域列表(从预定义列表中选择),我收到以下错误消息:无法为标识列插入显式值当IDENTITY_INSERT设置为OFF时,在表“区域”中。

public class Project
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long ProjectId { get; set; }
        public string ProjectName { get; set; }
        public string ProjectDescription { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime CreateDate { get; set; }
        public User CreatingUser { get; set; }
        public bool Closed { get; set; }
        public User ClosingUser { get; set; }
        public DateTime CloseDate { get; set; }
        public Customer Customer { get; set; }
        public ICollection<ProjectArea> ProjectAreas { get; set; }
        public ICollection<ProjectCategory> ProjectCategories { get; set; }
    }
public class Area
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long AreaId { get; set; }
        public string AreaName { get; set; }
        public string Description { get; set; }
        public bool Active { get; set; }
        public User CreatingUser { get; set; }
        public Customer Customer { get; set; }
        public ICollection<ProjectArea> ProjectAreas { get; set; }
    }
public class ProjectArea
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long ProjectAreaId { get; set; }
        [ForeignKey("Project")]
        public long ProjectId { get; set; }
        public Project Project { get; set; }
        [ForeignKey("Area")]
        public long AreaId { get; set; }
        public Area Area { get; set; }
    }
public class DatabaseContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProjectArea>().HasKey(projectArea => new { projectArea.ProjectId, projectArea.AreaId });
            modelBuilder.Entity<ProjectArea>().HasOne(projectArea => projectArea.Project).WithMany(project => project.ProjectAreas).HasForeignKey(projectArea => projectArea.ProjectId);
            modelBuilder.Entity<ProjectArea>().HasOne(projectArea => projectArea.Area).WithMany(area => area.ProjectAreas).HasForeignKey(projectArea => projectArea.AreaId);
}
public void Add(Project entity)
        {
            databaseContext.Project.Add(entity);
            databaseContext.SaveChanges();
        }

有人可以告诉我我在想什么吗?

1 个答案:

答案 0 :(得分:0)

将您的OnModelCreating更改为此:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
                modelBuilder.Entity<ProjectArea>()
                            .HasKey(projectArea => new 
                            {
                                 projectArea.ProjectId,
                                 projectArea.AreaId 
                            });
                modelBuilder.Entity<Project>()
                            .HasMany(c => c.ProjectArea)
                            .WithRequired()
                            .HasForeignKey(c => c.ProjectId);
                modelBuilder.Entity<Area>()
                            .HasMany(c => c.ProjectArea)
                            .WithRequired()
                            .HasForeignKey(c => c.AreaId);
}