具有一个类的字段的EntityFramework和Model
本文关键字:Model EntityFramework 字段 具有一 有一个 | 更新日期: 2023-09-27 18:00:20
我有"User"answers"Bid"两种型号。
当然还有数据库中的"用户"answers"投标"表。(postgres表中的"投标"列)
我可以像这样映射一个"用户"字段:
public int UserId { get; set; }
public virtual User User { get; set; }
但是不知道如何用不同的名称映射三个"用户"字段。
public int CreatorId { get; set; }//id of user in db table
public int ManagerId { get; set; }//id of user in db table
public int ExecutorId { get; set; }//id of user in db table
public virtual User Creator { get; set; }
public virtual User Manager { get; set; }
public virtual User Executor { get; set; }
我该怎么办?
public class Bid
{
public int BidId { get; set; }
public string Title { get; set; }
public string Text { get; set; }
public DateTime DateOfCreating { get; set; }
public DateTime DateOfProcessing { get; set; }
public DateTime DateOfExecution { get; set; }
//here is something incorrect
public int CreatorId { get; set; }
public int ManagerId { get; set; }
public int ExecutorId { get; set; }
public virtual User Creator { get; set; }
public virtual User Manager { get; set; }
public virtual User Executor { get; set; }
public bool IsProcessed { get; set; }
public bool IsExecuted { get; set; }
public bool IsExecutionConfirmed { get; set; }
}
我怀疑您必须告诉Entity Framework
,这3个属性应该用作数据库中的外键。
由于它们具有非常规名称(EF不会自动将它们识别为外键),您可以通过设置它们的ForeignKey
属性并指定它们是外键值的导航属性来帮助它理解。
[ForeignKey("Creator")]
public int CreatorId { get; set; }
[ForeignKey("Manager")]
public int ManagerId { get; set; }
[ForeignKey("Executor")]
public int ExecutorId { get; set; }
public virtual User Creator { get; set; }
public virtual User Manager { get; set; }
public virtual User Executor { get; set; }