2013-02-19 45 views
-3

我有「用戶」和「出價」模型。實體框架和模型與一個類別的字段

Andcourse「Users」和「Bids」表在DateBase中。 ("Bid" columns in postgres table

我可以映射一個「用戶」字段是這樣的:

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; } 
} 
+0

不知道你要求什麼男人!你想要它做什麼? – jackncoke 2013-02-19 20:10:38

+0

爲什麼這是不正確的? – cadrell0 2013-02-19 20:34:23

+0

試着更清楚你的要求,並希望你的選票旁邊的負號會消失。如果你問我認爲你在問什麼,你應該可以在你的上下文的OnModelCreating方法中用流利的API來指定它。 – lintmouse 2013-02-19 22:06:10

回答

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; } 
+0

我在回答的同時找到了解決方案。是的,那正是我需要的。謝謝。 – BadEnglish 2013-02-19 22:19:43