【问题标题】:1 to 1 required relationship with database generated identity与数据库生成身份的 1 对 1 必需关系
【发布时间】:2014-10-14 15:39:16
【问题描述】:

我见过很多实现一对一关系的示例,但我的做法失败了,因为要求有些不同(Guid 与数据库生成的选项、外键属性等)。

我有 2 个类(Bundesland、Programmkonfiguration)具有 1:1 的关系(从业务角度来说,两端都是必需的)但不能合并到一个表中

对德州的要求:

  • Guid Id 作为键但没有 DatabaseGenerated 属性
  • 导航属性程序配置

Bundesland.cs:

public class Bundesland
{
    [Key]
    public Guid Id { get; set; }

    public virtual Programmkonfiguration Programmkonfiguration { get; set; }
}

对德州的要求

  • Guid ID 作为从数据库生成的密钥
  • ForeignKey 属性 Bundesland_Id(接口需要带 _)
  • Navigation Property Bundesland

程序配置.cs:

public class Programmkonfiguration
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public Guid Bundesland_Id { get; set; }

    public virtual Bundesland Bundesland { get; set; }
} 

数据库架构应如下所示

  • 表德国 (Id)
  • 表程序配置(Id,Bundesland_Id)

为什么我到现在都失败了:

  • EF 无法自行识别关系
  • 如果我使用属性(ForeignKey,Required)或 fluent API 并且模式构建器没有失败,则在 context.SaveChanges() 之后,永远不会设置外键属性 Programmkonfiguration.Bundesland_Id

如果您想帮助我,您可能需要以下其他课程:https://gist.github.com/anonymous/9cb554cd864e3dbee1ac

我将 .NET 4.5(.1) 与 EF5 一起使用,但我在 EF6 上也失败了

提前致谢:)

【问题讨论】:

    标签: c# entity-framework ef-code-first


    【解决方案1】:

    您可以为此使用流畅的配置:

    public class Bundesland
    {
        [Key]
        [ForeignKey("Programmkonfiguration")]
        public Guid Id { get; set; }
    
        public virtual Programmkonfiguration Programmkonfiguration { get; set; }
    }
    
    public class BundesLandConfiguration: EntityTypeConfiguration<Bundesland>
    {
        public BundesLandConfiguration()
        {            
            HasProperty(p=>p.Id)
            HasRequired(p=>p.Programmkonfiguration).WithRequiredPrincipal(p=>p.Bundesland);
        }
    }
    
    public class Programmkonfiguration
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public Guid Id { get; set; }
    
        public Guid Bundesland_Id { get; set; }
    
        public virtual Bundesland Bundesland { get; set; }
    }
    
    public class ProgrammkonfigurationConfiguration: EntityTypeConfiguration<Programmkonfiguration>
    {
        public ProgrammkonfigurationConfiguration()
        {
            HasKey(p=>p.Id);
            HasProperty(p=>p.Id)
            HasProperty(p=>p.Bundesland_Id)
        }
    }
    

    不要忘记将此配置添加到 db 上下文中的 EntityModelConfigurations 中。

    更新:因为属性命名违反约定,你应该添加 [ForeignKey] 属性,就像我添加到 Bundesland 类的属性 Id 一样。

    【讨论】:

    • 感谢代码,但它都不起作用(无法确定类型 'OneToOneRelation.Model.Programmkonfiguration' 和 'OneToOneRelation.Model.Bundesland' 之间关联的主体端。此关联必须使用关系流式 API 或数据注释显式配置。)也不满足我的要求(Programmkonfiguration.Bundesland_Id)我已经尝试了几十种不同的配置,但找不到合适的配置,所以一切都像预期的那样工作.
    • 我更新了我的答案。因为属性命名是违反约定的,所以你应该添加 [ForeignKey] 属性,就像我添加到 Bundesland 类的属性 Id 一样。
    • 您应该将配置类添加到您的 dbcontext。你这样做了吗?
    • 当使用您的示例时,异常 ReferentialConstraint 中的依赖属性被映射到存储生成的列。列:“Id”。 被抛出。 我确实了解使用实体框架映射关系的概念。就我的要求而言,这似乎是一种不常见的情况。我尝试了各种方法(ForeignKey、流式 API 与属性等),因此我正在寻找满足我第一篇文章要求的可行解决方案。
    猜你喜欢
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多