【发布时间】:2014-06-19 15:11:19
【问题描述】:
这应该是一个简单的问题, 我不明白为什么会这样:
错误:
当尝试通过 Power 工具生成这些模型时,我收到了这个错误, 当我尝试在另一个项目中添加关于这些的迁移时,我也遇到了类似的错误。
Activity_ActivityResult_Target: : Multiplicity is not valid in Role 'Activity_ActivityResult_Target' in relationship 'Activity_ActivityResult'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
这里我们有 2 个简单的类,其中有一个 relationship 用于 code-fisrt 策略。
Desired: Activity 1 ----> ActivityResult 0-1
完整代码:
public class Activity
{
[Key]
public int Id { get; set; }
public DateTime? ActivityDate{ get; set;}
// Tested with and without these commented part, I didn't think it should be
//[ForeignKey("ActivityResult")]
//public int? ActivityResultId { get; set; }
public virtual ActivityResult ActivityResult { get; set; }
}
//---------------
public class ActivityResult //: Entity
{
[Key]
public int Id { get; set; }
public int Score1 { get; set; }
public int Score2 { get; set; }
[ForeignKey("Activity")]
[Required]
public int ActivityId { get; set; }
public virtual Activity Activity { get; set; }
}
// ---------------
public class Context : DbContext
{
public DbSet<Activity> Activities { get; set; }
public DbSet<ActivityResult> ActivityResults { get; set; }
}
为什么?解决办法是什么?
【问题讨论】:
-
两个答案都是正确的,我希望我能打两个,我更喜欢@tschmit007 版本,因为我可以在那里拥有 ID,并且可以为具有该属性的所有模型提供抽象基础,谢谢两者。
标签: entity-framework ef-code-first entity-relationship foreign-key-relationship