如何首先设置一对一的关系实体框架代码

编程入门 行业动态 更新时间:2024-10-15 18:29:14
本文介绍了如何首先设置一对一的关系实体框架代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试跟随许多类似的问题,但我无法使其工作。我正在从一到多变成一对一。每次测量都会有一个ClientPicture。

无法确定类型FitFactor.Models.Clients.ClientPicture和FitFactor.Models.Clients之间关联的主体结束。测量'。必须使用关联流畅的API或数据注释来明确地配置此关联的主体。

public class ClientPicture { public int Id {get;组; } public string ClientPictureUrl {get;组; } public string ClientName {get;组; } public DateTime? ClientPictureDate {get;组; } public Guid MeasurementId {get;组; } [ForeignKey(MeasurementId)] public virtual Measurement Measurement {get;组; }

测量

public class Measurement { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] public Guid Id {get;组; } public DateTime? MeasurementDate {get;组; } public decimal?胸围测量{get;组; } public decimal?胸部测量{get;组; } public decimal?腰围测量{get;组; } public decimal?臀部测量组; } public int ClientPictureId {get;组; } [ForeignKey(ClientPictureId)] public virtual ClientPicture ClientPicture {get;组; } }

解决方案

- 一个关系一端必须是主体,另一端必须是依赖。首先应该插入校长,而不用依赖就可以存在。我们认为你有两个选择:

$ b

$ b

  • 您在两个实体中使用相同的Id,依赖关系的Id被声明为一对一关系的FK

    //主体 public class ClientPicture { public int Id {get;组; } // ... 公共虚拟测量测量{get;组; } //依赖的 public class Measurement { [Key,ForeignKey(ClientPicture)] public int ClientPictureId {get;组; } public virtual ClientPicture ClientPicture {get;组; }

  • 您的实体使用不同的ID,但您只能工作导航属性:

    public class ClientPicture { public int Id {get;组; } // ... 公共虚拟测量测量{get;组; public class Measurement { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] public Guid Id {get ;组; // ... [必需] public virtual ClientPicture ClientPicture {get;组; } }

    EF允许您将FK映射到一对一 - 一个关系是当FK被宣布为PK(像第一个变体)时。检查此链接更多信息

I am trying to follow many of the similar questions on here but I cannot get it working. I am changing from a one to many to a one to one. Every Measurement will have one ClientPicture.

Unable to determine the principal end of an association between the types 'FitFactor.Models.Clients.ClientPicture' and 'FitFactor.Models.Clients.Measurement'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

public class ClientPicture { public int Id { get; set; } public string ClientPictureUrl { get; set; } public string ClientName { get; set; } public DateTime? ClientPictureDate { get; set; } public Guid MeasurementId { get; set; } [ForeignKey("MeasurementId")] public virtual Measurement Measurement { get; set; } }

Measurement

public class Measurement { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] public Guid Id { get; set; } public DateTime? MeasurementDate { get; set; } public decimal? BustMeasurement { get; set; } public decimal? ChestMeasurement { get; set; } public decimal? WaistMeasurement { get; set; } public decimal? HipsMeasurement { get; set; } public int ClientPictureId { get; set; } [ForeignKey("ClientPictureId")] public virtual ClientPicture ClientPicture { get; set; } }

解决方案

In an one-to-one relation one end must be principal and the other end must be the dependent. The principal should be inserted first and can exist without the dependent. Dependent end is the one which must be inserted after the principal because it has foreign key to the principal.

I think you have two options here:

  • You use the same Id in both entities and the Id of the dependend is declared as the FK of the one to one relationship

    //The principal public class ClientPicture { public int Id { get; set; } //... public virtual Measurement Measurement { get; set; } } //The dependent public class Measurement { [Key, ForeignKey("ClientPicture")] public int ClientPictureId { get; set; } public virtual ClientPicture ClientPicture { get; set; } }

  • You use different Ids in your entities but you can work only with the navigation properties:

    public class ClientPicture { public int Id { get; set; } //... public virtual Measurement Measurement { get; set; } } public class Measurement { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)] public Guid Id { get; set; } //... [Required] public virtual ClientPicture ClientPicture { get; set; } }

    The only way that EF lets you map the FK in an one-to-one relationship is when the FK is declared as a PK too (like the first variant). Check this link for more info

更多推荐

如何首先设置一对一的关系实体框架代码

本文发布于:2023-11-16 06:52:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1601990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   关系   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!