尝试创建多对多关系时迁移失败

问题描述

我正在尝试使用代码优先迁移连接两个表。我认为 EF 会创建多对多关系表本身,但我收到错误“构建失败”。在构建整个项目时,一切正常。这只是迁移。

以下是我的模型 -

任务:

[Key]
public int Id { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? EndedAt { get; set; }
[Column(TypeName = "text")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "text")]
public string Title { get; set; }
[Required]
[Column(TypeName = "text")]
public string Description { get; set; }
public virtual TaskGroups TaskGroup { get; set; }
public string Status { get; set; }
[Column(TypeName = "text")]
public string WantedUser { get; set; }

任务组:

[Required]
public int Id { get; set; }
[Required]
public string GroupName { get; set; }
public virtual Tasks Tasks { get; set; }

起初我尝试过 ICollection 但我遇到了同样的错误。 我的项目是 .Net Core 3。

有什么想法吗?

编辑

任务

        [Key]
        public int Id { get; set; }
        public DateTime? CreatedAt { get; set; }
        public DateTime? EndedAt { get; set; }
        [Column(TypeName = "text")]
        public string CreatedBy { get; set; }
        [Required]
        [Column(TypeName = "text")]
        public string Title { get; set; }
        [Required]
        [Column(TypeName = "text")]
        public string Description { get; set; }
        public string Status { get; set; }
        [Column(TypeName = "text")]
        public string WantedUser { get; set; }
        public IList<TaskGroupTask> TaskGroupTask { get; set; }

任务组

        [Key]
        public int Id { get; set; }
        [Required]
        public string GroupName { get; set; }
        public IList<TaskGroupTask> { get; set; }

TaskGroupTask

        public int TaskId { get; set; }
        public int TaskGroupId { get; set; }
        public Tasks Tasks { get; set; }
        public TaskGroups TaskGroups { get; set; }

数据库上下文

protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<TaskGroupTask>(e =>
           {
               e.HasKey(p => new { p.TaskId,p.TaskGroupId });
               e.HasOne(p => p.Tasks).WithMany(t => 
               t.TaskGroupTask).HasForeignKey(p => p.TaskId);
               e.HasOne(p => p.TaskGroups).WithMany(tg => 
               tg.TaskGroupTask).HasForeignKey(p => p.TaskGroupId);
           });
        }

 public DbSet<TaskGroupTask> TaskGroupTask { get; set; }

解决方法

因为我有一些时间,所以我决定将代码的所有图片放在一个地方。我认为如何为数据库表创建代码优先的多对多关系的示例非常有用。此代码在 Visual Studio 中进行了测试,并创建了一个新数据库,没有任何警告:

public class Task

    {
        public Task()
    {
        TaskTaskGroups = new HashSet<TaskTaskGroup>();
    }
    [Key]
    public int Id { get; set; }
    public DateTime? CreatedAt { get; set; }
    public DateTime? EndedAt { get; set; }
    [Column(TypeName = "text")]
    public string CreatedBy { get; set; }
    [Required]
    [Column(TypeName = "text")]
    public string Title { get; set; }
    [Required]
    [Column(TypeName = "text")]
    public string Description { get; set; }
    public string Status { get; set; }
    [Column(TypeName = "text")]
    public string WantedUser { get; set; }
    [InverseProperty(nameof(TaskTaskGroup.Task))]
    public virtual ICollection<TaskTaskGroup> TaskTaskGroups { get; set; }
}


public class TaskGroup
{
    public TaskGroup()
    {
        TaskTaskGroups = new HashSet<TaskTaskGroup>();
    }
    [Required]
    public int Id { get; set; }
    [Required]
    public string GroupName { get; set; }
    [InverseProperty(nameof(TaskTaskGroup.TaskGroup))]
    public virtual ICollection<TaskTaskGroup> TaskTaskGroups { get; set; }
}

public class TaskTaskGroup
{
    [Key]
    public int Id { get; set; }

    public int TaskId { get; set; }

    [ForeignKey(nameof(TaskId))]
    [InverseProperty(nameof(TaskTaskGroup.Task.TaskTaskGroups))]
    public virtual Task Task { get; set; }

    public int TaskGroupId { get; set; }

    [ForeignKey(nameof(TaskGroupId))]
    [InverseProperty(nameof(TaskTaskGroup.Task.TaskTaskGroups))]
    public virtual TaskGroup TaskGroup { get; set; }
}


public class TaskDbContext : DbContext
    {
        public TaskDbContext()
        {
        }

        public TaskDbContext(DbContextOptions<TaskDbContext> options)
            : base(options)
        {
        }
        public DbSet<Task> Tasks { get; set; }
        public DbSet<TaskGroup> TaskGroups { get; set; }
        public DbSet<TaskTaskGroup> TaskTaskGroups { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            optionsBuilder.UseSqlServer(@"Server=localhost;Database=Task;Trusted_Connection=True;");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TaskTaskGroup>(entity =>
            {
                entity.HasOne(d => d.Task)
                    .WithMany(p => p.TaskTaskGroups)
                    .HasForeignKey(d => d.TaskId)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("FK_TaskTaskGroup_Task");

                entity.HasOne(d => d.TaskGroup)
                    .WithMany(p => p.TaskTaskGroups)
                    .HasForeignKey(d => d.TaskGroupId)
                    .HasConstraintName("FK_TaskTaskGroup_TaskCroup");
            });

        }

    }
,

您需要创建一个连接实体,例如 MyJoiningEntityTaskGroupTask,其唯一目的是在 TaskTaskGroup 之间创建链接。以下模型应该给你的想法 -

public class Task
{
    public int Id { get; set; }
    public string Description { get; set; }

    public IList<JoiningEntity> JoiningEntities { get; set; }
}

public class TaskGroup
{
    public int Id { get; set; }
    public string GroupName { get; set; }

    public IList<JoiningEntity> JoiningEntities { get; set; }
}

// this is the Joining Entity that you need to create
public class JoiningEntity
{
    public int TaskId { get; set; }
    public int TaskGroupId { get; set; }

    public Task Task { get; set; }
    public TaskGroup TaskGroup { get; set; }
}

然后您可以在 OnModelCreating 类的 DbContext 方法中配置关系,例如 -

modelBuilder.Entity<JoiningEntity>(e =>
{
    e.HasKey(p => new { p.TaskId,p.TaskGroupId });
    e.HasOne(p => p.Task).WithMany(t => t.JoiningEntities).HasForeignKey(p => p.TaskId);
    e.HasOne(p => p.TaskGroup).WithMany(tg => tg.JoiningEntities).HasForeignKey(p => p.TaskGroupId);
});

这将基于 JoiningEntityTaskId 属性在 TaskGroupId 表上定义复合主键。由于此表的唯一目的是链接另外两个表,因此它实际上并不需要自己的 Id 字段作为主键。

注意:此方法适用于低于 5.0 的 EF 版本。从 EF 5.0 开始,您可以create a many-to-many relationship in a more transparent way

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...