简体   繁体   English

通过除键以外的其他属性的EF导航属性

[英]EF Navigational Property through Different Properties Other Than Key

I'm working on mapping a legacy application with classes and use EntityFramework against it. 我正在使用类映射旧版应用程序,并对它使用EntityFramework

One flaw I have found in this legacy database is that multiple tables refer to a specific table through 2 different fields. 我在此旧数据库中发现的一个缺陷是,多个表通过2个不同的字段引用了一个特定的表。

I'm not sure if this is possible and why I can't seem to find anything about it so I am here. 我不确定这是否可行,为什么我似乎找不到任何相关信息,所以我在这里。

Here is a visual sample: 这是一个视觉样本:

public class Term {
    [Key]
    public string Id { get; set; } // sample value: "12-34-56/78"
    public string CleanId { get; set; } // sample value: "12345678" (basically the Id without special characters)
    public DateTime Date { get; set; }
}

public class App {
    public int Id { get; set; }
    public string CleanTermId { get; set; } // foreign key is in Term class using the `CleanId` field
}

public class Question {
    public int Id { get; set; }
    public string TermId { get; set; } // foreign key is in Term class using the `Id` field
}

How can I properly add a navigational property from App and Question to the Term class using either DataAnnotations (preferred) to Fluent API ? 如何使用Fluent API DataAnnotations (首选)将AppQuestion的导航属性正确地添加到Term类? I do not require a navigational property from Term to App or Question but it's ok if your answer includes it. 我不需要从TermAppQuestion的导航属性,但是如果您的答案包括它也可以。

Let me know if this is not clear. 让我知道是否不清楚。

Joining on fields other than Primary Key was something that isnt supported in EF versions prior to EF Core, however with your mention of it being a legacy app I doubt you would want to overhaul it to be able to use EF Core. 在EF Core之前的EF版本中,除主键之外的其他字段上的连接都是不支持的,但是您提到它是一个遗留应用程序,我怀疑您是否需要对其进行全面改造才能使用EF Core。

There was a User Voice request for the feature to be added Here which the response is that they had no plans to add this functionality into EF6 - so Core would be the only way to really do this. 有一个用户语音请求要添加功能,这是因为他们没有计划将此功能添加到EF6中-因此Core是真正做到这一点的唯一方法。

In terms of your classes you would be able to link Question and Term as its based PK - FK, but the App to Term is basing both on non-PK fields, even with a Unique constraint on the DB, this is something not supported in EF prior to Core 就您的类而言,您可以将“问题和术语”作为其基础的PK-FK进行链接,但是“应用程序到术语”都是基于非PK字段,即使在数据库上具有唯一性约束,这也不支持。 EF优先于Core

Hi this is the correct Code: 嗨,这是正确的代码:

    public class Term
{
    [Key]
    public string Id { get; set; }
    public string CleanId { get; set; }
    public DateTime Date { get; set; }
}

public class App
{
    public int Id { get; set; }

    [ForeignKey("CleanTermId")]
    public Term MyTerm { get; set; }
    public string CleanTermId { get; set; } 

}

public class Question
{
    public int Id { get; set; }

    [ForeignKey("TermId")]
    public Term MyTerm { get; set; }
    public string TermId { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM