【问题标题】:Code-First relationship -getting error about Multiplicity代码优先关系 - 得到关于多重性的错误
【发布时间】: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


【解决方案1】:

在一对一关系中,外键也是主键。

您需要更改ActivityResult 以删除Id 属性并且只有ActivityId。它将同时具有[Key][ForeignKey]

public class ActivityResult //: Entity
{

    [Key]
    [ForeignKey("Activity")]
    [Required]
    public int ActivityId { get; set; }
    public int Score1 { get; set; }
    public int Score2 { get; set; }

    public virtual Activity Activity { get; set; }
}

以下是 Entity Framework 将如何使用迁移创建表。您会注意到 ActivityResults 没有将 ActivityId 定义为 Identity。这是因为它不能自动生成。为了一对一,它必须在活动中具有匹配的行。它是主键,所以它是唯一的。如果您同时拥有 ActivityId 和 Id 并且 Id 是主键,那么您确实具有一对多的关系。

CreateTable(
    "dbo.Activities",
    c => new
    {
        Id = c.Int(nullable: false, identity: true),
        ActivityDate = c.DateTime(),
    })
    .PrimaryKey(t => t.Id);

CreateTable(
    "dbo.ActivityResults",
    c => new
    {
        ActivityId = c.Int(nullable: false),
        Score1 = c.Int(nullable: false),
        Score2 = c.Int(nullable: false),
    })
    .PrimaryKey(t => t.ActivityId)
    .ForeignKey("dbo.Activities", t => t.ActivityId)
    .Index(t => t.ActivityId);

【讨论】:

  • 谢谢,提问后我不在电脑前,请给我一点时间检查一下。这会有所帮助+1
【解决方案2】:

@Dismissile 是正确的,但恕我直言,正确的代码是:

public class Activity {
        [Key]
        public int Id { get; set; }
        public DateTime? ActivityDate { get; set; }
        public virtual ActivityResult ActivityResult { get; set; }
    }


    //---------------

    public class ActivityResult {

        [Key]
        public int Id { get; set; }
        public int Score1 { get; set; }
        public int Score2 { get; set; }

        //public int ActivityId { get; set; }
        [ForeignKey("Id")]
        [Required]
        public virtual Activity Activity { get; set; }
    }

给出下表:

CREATE TABLE [dbo].[Activities](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ActivityDate] [datetime] NULL,
 CONSTRAINT [PK_dbo.Activities] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)

CREATE TABLE [dbo].[ActivityResults](
    [Id] [int] NOT NULL,
    [Score1] [int] NOT NULL,
    [Score2] [int] NOT NULL,
 CONSTRAINT [PK_dbo.ActivityResults] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)

【讨论】:

  • 谢谢,提问后我不在电脑前,请给我一点时间检查一下。这会有所帮助+1
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
相关资源
最近更新 更多