关系约束中从属角色和主要角色中的属性数量必须相同?
The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical?
ConsumerNM 是一个 NM/bridge table/entity。
它与事件 table 有 1:N 关系。
当我执行插入操作时,出现问题标题异常。
我必须更改什么才能使其正常工作?
我是否必须创建 2 个外键,每个外键都指向另一个 ConsumerNM_Key?
public class ConsumerNM
{
public ConsumerNM()
{
Events = new HashSet<Event>();
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADCONSUMER { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_Consumer { get; set; }
}
ConsumerNM
或 Event
不正确。您有两个选择:
第一个选项:
public class ConsumerNM
{
//This is not a PK
//[Key]
//[Column(Order = 0)]
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
public int FK_LEADCONSUMER { get; set; }
}
Event
保持不变
第二个选项:
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADCONSUMER { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADMETA { get; set; }
}
ConsumerNM
保持不变
ConsumerNM 是一个 NM/bridge table/entity。
它与事件 table 有 1:N 关系。
当我执行插入操作时,出现问题标题异常。
我必须更改什么才能使其正常工作?
我是否必须创建 2 个外键,每个外键都指向另一个 ConsumerNM_Key?
public class ConsumerNM
{
public ConsumerNM()
{
Events = new HashSet<Event>();
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADCONSUMER { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_Consumer { get; set; }
}
ConsumerNM
或 Event
不正确。您有两个选择:
第一个选项:
public class ConsumerNM
{
//This is not a PK
//[Key]
//[Column(Order = 0)]
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
public int FK_LEADCONSUMER { get; set; }
}
Event
保持不变
第二个选项:
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADCONSUMER { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADMETA { get; set; }
}
ConsumerNM
保持不变