代码之家  ›  专栏  ›  技术社区  ›  bugrasitemkar

基于关系变化的实体框架迁移

  •  1
  • bugrasitemkar  · 技术社区  · 6 年前

    我有两个模型

      public class Employee
        {
            public Employee()
            {
                Active = true;
            }
    
            [Key]
            public long Id { get; set; }
    
            public List<Service> Services { get; set; }
    
            public List<SubService> SubServices { get; set; }
    
            [NotMapped]
            public List<long> ServiceIds { get; set; }
    
            public bool IsSyncedToSP { get; set; }
    
            public Certificate Certificate { get; set; }
    
            [NotMapped]
            public List<long> SubServiceIds { get; set; }
    
            public List<long> GetServiceIds()
            {
                if (ServiceIds != null && ServiceIds.Count > 0)
                {
                    return ServiceIds;
                }
                else if (Services != null && Services.Count > 0)
                {
                    return Services.Select(s => s.Id).ToList();
                }
                return new List<long>();
            }
    

    public class Certificate
    {
        [Key]
        public long Id { get; set; }
    
        [Required]
        [UnsyncOnEdit(Unsync = true)]
        public string Title { get; set; }
    
        public bool IsSyncedToSP { get; set; }
    
        public List<Employee> Employees { get; set; }
    }
    

    当我试图添加 public List<Employee> Employees { get; set; } EF创建以下迁移

     public partial class empcert2 : DbMigration
        {
            public override void Up()
            {
                RenameTable(name: "dbo.ServiceClients", newName: "ClientServices");
                RenameTable(name: "dbo.EmployeeServices", newName: "ServiceEmployees");
                DropPrimaryKey("dbo.ClientServices");
                DropPrimaryKey("dbo.ServiceEmployees");
                AddPrimaryKey("dbo.ClientServices", new[] { "Client_Id", "Service_Id" });
                AddPrimaryKey("dbo.ServiceEmployees", new[] { "Service_Id", "Employee_Id" });
            }
    
            public override void Down()
            {
                DropPrimaryKey("dbo.ServiceEmployees");
                DropPrimaryKey("dbo.ClientServices");
                AddPrimaryKey("dbo.ServiceEmployees", new[] { "Employee_Id", "Service_Id" });
                AddPrimaryKey("dbo.ClientServices", new[] { "Service_Id", "Client_Id" });
                RenameTable(name: "dbo.ServiceEmployees", newName: "EmployeeServices");
                RenameTable(name: "dbo.ClientServices", newName: "ServiceClients");
            }
        }
    

    public List<Employee>员工{get;set;} 这行从证书模型,奇怪的迁移不创建。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ivan Stoev    6 年前

    问题是隐式连接表的多对多关系的EF映射是不确定的。由于所涉及的两个表在关系中的角色是相等的,所以认为“左”还是“右”完全取决于EF模型依赖顺序算法,该算法的唯一要求是确保在依赖表之前创建引用表。

    正确的 多对多关系的隐式连接表中的角色。因此,您不应该让EF选择它们,而总是显式地指定via HasMany WithMany (右)fluent API。

    要保留原始设计,请将以下内容添加到 OnModelCreating

    modelBuilder.Entity<Service>().HasMany(e => e.Clients).WithMany(e => e.Services);
    modelBuilder.Entity<Employee>().HasMany(e => e.Services).WithMany(e => e.Employees);