主键与实体框架 Code First 的一对一关系

发布于 2024-10-22 09:55:53 字数 869 浏览 0 评论 0原文

目前,我在尝试使用 Code First 创建一对一关系时遇到以下错误: System.Data.Edm.EdmAssociationEnd::多重性在关系“C001_Holding_Teste_C001_Holding”中的角色“C001_Holding_Teste_C001_Holding_Source”中无效。由于 Dependent Role 引用了关键属性,因此 Dependent Role 的重数上限必须为 1。 我的实体定义如下:

[Table("C001_Holding", Schema = "Cad")]
public partial class C001_Holding
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int C001_Id { get; set; }

    [MaxLength(16)]
    public string C001_Codigo { get; set; }

    [MaxLength(100)]
    public string C001_Descricao { get; set; }
}

public class C001_Holding_Test
{
    [Key]
    public int C001_Id { get; set; }
    [MaxLength(100)]
    public string C001_TestInfo { get; set; }

    [ForeignKey("C001_Id")]
    public virtual C001_Holding C001_Holding { get; set; }
}

我不想使用 Fluent 来创建这些关系,有谁知道为什么会发生这种情况?

谢了。

I'm currently getting the following error when trying to create an one to one relationship using Code First:
System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'C001_Holding_Teste_C001_Holding_Source' in relationship 'C001_Holding_Teste_C001_Holding'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1.
My entity definitions are the following:

[Table("C001_Holding", Schema = "Cad")]
public partial class C001_Holding
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int C001_Id { get; set; }

    [MaxLength(16)]
    public string C001_Codigo { get; set; }

    [MaxLength(100)]
    public string C001_Descricao { get; set; }
}

public class C001_Holding_Test
{
    [Key]
    public int C001_Id { get; set; }
    [MaxLength(100)]
    public string C001_TestInfo { get; set; }

    [ForeignKey("C001_Id")]
    public virtual C001_Holding C001_Holding { get; set; }
}

I didn't want to use Fluent to create these relationships, does anyone knows why this is happening?

Tks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-10-29 09:55:53

可以将 ForeignKey 属性放置在导航属性上,然后指定您想要作为外键的属性名称(这就是您所做的)。或者,您可以将其放置在外键属性上,然后指定表示关系的导航属性的名称。这看起来像:

public class C001_Holding_Test
{
    [Key]
    [ForeignKey("C001_Holding")]
    public int C001_Id { get; set; }

    [MaxLength(100)]
    public string C001_TestInfo { get; set; }

    public virtual C001_Holding C001_Holding { get; set; }
}

由于某种原因,第二个选项有效,而第一个选项抛出错误。 (对我来说这感觉像是一个错误,因为两个选项应该代表相同的关系。或者实际上存在我看不到的语义差异......)

It is possible to place the ForeignKey attribute either on a navigation property and then specify the name of the property you want to have as the foreign key (that's what you did). Or you can place it on the foreign key property and then specify the name of the navigation property which represents the relationship. This would look like:

public class C001_Holding_Test
{
    [Key]
    [ForeignKey("C001_Holding")]
    public int C001_Id { get; set; }

    [MaxLength(100)]
    public string C001_TestInfo { get; set; }

    public virtual C001_Holding C001_Holding { get; set; }
}

For some reason this second option works while the first throws an error. (It feels like a bug to me because both options should represent the same relationship. Or there is actually a semantic difference which I don't see...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文