-3

我有“用户”和“投标”模型。

当然还有 DateBase 中的“Users”和“Bids”表。(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; }
}
4

1 回答 1

0

我怀疑你必须告诉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; }
于 2013-02-19T21:49:24.180 回答