首页 > 解决方案 > 如何在实体框架中定义非常规(?)一对多关系

问题描述

我需要使用一个有两个表的数据库,Proprietaires并且Certification.

Proprietaires有这些列:

Certification有这些列:

所以我这样定义我的Proprietaire实体:

public class Proprietaire
{
    public int Id { get; set; }
    public int IdCertification { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Certification> Certifications { get; private set; } = new List<Certification>();
}

Certification实体:

public class Certification
{
    public int Id { get; set; }
    public int ProprietaireId { get; set; }
    public string Name { get; set; }

    public virtual Proprietaire Proprietaire { get; set; }
}

我的问题是我不知道将关系定义为具有以下行为:

当我访问Proprietaire我的实体的属性时Certification,我需要获取Proprietairewhere Proprietaire.Id == Certification.ProprietaireId。这部分有效。

但是当我访问Certifications我的实体的(列表)属性时Proprietaire,我需要获得所有的认证

where Certification.ProprietaireId == Proprietaire.IdCertification

我什至不知道EF是否可能。但是数据库的结构不是很好,但我需要处理它......

有解决办法吗?

谢谢

标签: c#entity-frameworkrelationship

解决方案


推荐阅读