c# - Entity Framework 6 上的数据库迁移问题

标签 c# entity-framework

我有以下类(class)(为简洁起见缩短);

public class InsurancePolicy : AuditableEntity<int>
{
    [Column("id"), Key]
    public override int ID { get; set; }
    [Column("deviceid"), ForeignKey("Device")]
    public int DeviceId { get; set; }
    public virtual Device Device { get; set; }
}

public partial class Device : AuditableEntity<int>
{
    [Column("deviceid"), Key]
    public override int ID { get; set; }

    [ForeignKey("Policy")]
    public int PolicyId { get; set; }
    public virtual InsurancePolicy Policy { get; set; }

    [ForeignKey("Vehicle")]
    public int VehicleId { get; set; }
    public virtual Vehicle Vehicle { get; set; }
}

public partial class Vehicle : AuditableEntity<int>
{
    [Column("id"), Key]
    public override int ID { get; set; }

    [Column("deviceid"), ForeignKey("Device")]
    public int DeviceId { get; set; }
    public virtual Device Device { get; set; }

    public virtual List<InsurancePolicyVehicle> InsurancePolicyVehicles { get; set; }
}

现在,当我尝试运行我的 update-database 命令时,出现此错误:

Device_Policy_Target: : Multiplicity is not valid in Role 'Device_Policy_Target' in relationship 'Device_Policy'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

Device_Vehicle_Target: : Multiplicity is not valid in Role 'Device_Vehicle_Target' in relationship 'Device_Vehicle'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

谁能告诉我这里做错了什么?

最佳答案

Device_Policy_Target 关系为例,您有以下内容(缩减为该关系的相关属性):

public class InsurancePolicy : AuditableEntity<int>
{   
    [Column("id"), Key]
    public override int ID { get; set; }
    [Column("deviceid"), ForeignKey("Device")]
    public int DeviceId { get; set; }
    public virtual Device Device { get; set; }
}

public partial class Device : AuditableEntity<int>
{
    [Column("deviceid"), Key]
    public override int ID { get; set; }

    [ForeignKey("Policy")]
    public int PolicyId { get; set; }
    public virtual InsurancePolicy Policy { get; set; }

}

您在这里定义的是一对一关系。 EF 仅支持两个表共享主键的这种关系。在您的配置中,您的子表 - InsurancePolicy 有自己的 PK 和 Device 的外键。 EF 仅支持具有这些约束的一对多关系。

要将这些关系定义为一对一,请将您的设置切换为以下内容:

public class InsurancePolicy : AuditableEntity<int>
{
    [Column("deviceid"), Key, ForeignKey("Device")]
    public override int ID { get; set; }
    public virtual Device Device { get; set; }
}

public partial class Device : AuditableEntity<int>
{
    [Column("deviceid"), Key]
    public override int ID { get; set; }

    public virtual InsurancePolicy Policy { get; set; }    
}

部分原因是 EF 不支持唯一约束。 This article使用 DataAnnotations 对一对一关系进行了更全面的解释

更新:

要使用您拥有的表/键“伪造”一对一,然后考虑这样的事情:

public class InsurancePolicy : AuditableEntity<int>
{   
    [Column("id"), Key]
    public override int ID { get; set; }
    [Column("deviceid"), ForeignKey("Device")]
    public int DeviceId { get; set; }
    public virtual Device Device { get; set; }
}

public partial class Device : AuditableEntity<int>
{
    [Column("deviceid"), Key]
    public override int ID { get; set; }

    public virtual IEnumerable<InsurancePolicy> PoliciesAsList { get; set; }

    [NotMapped]
    public virtual InsurancePolicy Policy {
        get {
            return (PoliciesAsList != null) 
                ? PoliciesAsList.FirstOrDefault() 
                : null;                
        }
    }

}

在这种情况下,我任意选择了 Device 作为父级——您选择哪个并不重要。

您还应确保数据库对 InsurancePolicy 表上的 deviceid 具有唯一约束。

关于c# - Entity Framework 6 上的数据库迁移问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330195/

相关文章:

c# - VS2013 错误 "Exception has been thrown by the target of an invocation"

c# - 为什么使用 EF 序列化后得到无效的 JSON?

c# - 从 JSON 输出中删除不需要的空格

c# - 如何重复使用 string.Format N 次生成的字符串

c# - 是否可以控制 .NET Progress<T> 对象使用的 SynchronizationContext?

c# - 将多个来源合并到一个目的地

c# - .NET 模拟 Ctrl+Alt+Del 发送键

c# - Visual Studio 不同语言的文本搞砸了

sql - 有没有办法为 Azure SQL Server 设置默认的 Azure SQL 数据库定价层

c# - EF 6 Code First From Database - 调用现有的存储过程