我首先在模型中使用代码,并尝试向评估类添加 2 个属性,这些属性将作为返回到我的提供程序类的外键。
... In Assessment Class ...
public class Assessment
{
public int AssessmentID { get; set; }
public int createdByProviderID { get; set; } // this will container a providerID
public int closedByProviderID { get; set; } // this will container a providerID
}
...
... Provider ...
public class Provider
{
public int ProviderID { get; set; }
}
...
我不知道如何执行此操作,因为它们不遵循 EF 寻找的标准命名约定。
最佳答案
您可以通过几种方式做到这一点;我个人使用映射类来将特定于数据库的详细信息保留在我的模型之外,但我无法从您的问题中判断您是否正在使用该方法。假设您只有模型类,您可以执行以下操作:
1 - 添加一个指向每个外键的提供者模型的虚拟属性(这将允许延迟加载)。
2 - 使用ForeignKey 属性装饰每个新的虚拟属性,指向实际外键的属性。
public int createdByProviderID { get; set; } // this will container a providerID
[ForeignKey("createdByProviderID")]
public virtual Provider createdByProvider{get; set;}
public int closedByProviderID { get; set; } // this will container a providerID
[ForeignKey("closedByProviderID")]
public virtual Provider closedByProvider{get; set;}
祝你好运。
关于asp.net-mvc - (代码第一)使用非标准命名约定的 Entity Framework 外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18899338/