我在單獨的項目中有實體。其中一個實體需要參照Web
項目中的UserId
,UserProfile
模型,該項目已經參考了Entity
項目。任何人都可以幫助得到一個新的外鍵,我的實體(像Expense
實體類來獲得額外的領域Expender
這已經是UserProfile
主鍵是一個成員表?)ASP.NET MVC 4實體框架實體從成員資格用戶配置文件表中獲取外鍵字段
namespace ExpenseAssistant.Entities
{
/// <summary>
/// Expense
/// </summary>
public class ExpenseEntry
{
/// <summary>
/// Gets or sets Unique Id of the Expense
/// </summary>
public virtual int Id { get; set; }
/// <summary>
/// Gets or sets Description about the Expense
/// </summary>
public virtual string ExpenseDescription { get; set; }
/// <summary>
/// Gets or sets the Amount Spent
/// </summary>
public virtual float Amount { get; set; }
/// <summary>
/// Gets or sets the Expender which is the UserId from UserProfile Table
/// </summary>
[ForeignKey("UserProfile")]
public virtual int ExpenderId { get; set; }
}
}
成員表是在同一個數據庫中我有所有的實體表。
我update-database
控制檯試過之後加入該鍵ExpenderId
在ExpenseEntry
實體,我得到如下錯誤,
的ForeignKeyAttribute財產「ExpenderId」在類型 「ExpenseAssistant.Entities.ExpenseEntry」無效。在依賴類型 'ExpenseAssistant.Entities.ExpenseEntry'上沒有找到導航 屬性'UserProfile'。名稱值應該是 有效的導航屬性名稱。
在此先感謝。
我都以不同的方式解決了這個問題!從現有的表格中,使用模型圖,我創建了一個關係。在逆向工程之後,我從控制檯運行update-database命令。有效!不管怎樣,謝謝! –