2015-03-31 21 views
5

我測試了模型中的其他表並且它們正在工作。但是,我添加了兩個新表格,它們不在模型中。實體類型<T>不是當前上下文的模型的一部分

如何確保數據庫表是模型的一部分?

OnModelCreating不在此代碼中調用。 enter image description here

The entity type AppToLeadRequestLine is not part of the model for the current context. 

此異常將項目添加到表時被拋出。

public void Add<T>(T obj) where T : class 
{ 
    Set<T>().Add(obj); 
} 

我將這兩個表都添加到數據庫。

enter image description here

加入這兩個表來我生成它們在模型中使用Update Model from Database數據庫後,和兩個表出現在模型中。 enter image description here

當我查看錶映射時,它們似乎映射到正確的POCO。我對此並不十分確定。

enter image description here enter image description here

的類是這樣的: enter image description here enter image description here

+0

該錯誤可能意味着一些事情。你可以驗證你的其他桌子是否正常工作,只有一兩個不是? – Jonesopolis 2015-03-31 18:37:29

+0

我測試了其他表,他們正在工作。只有我添加的兩個新表有這個問題。 – 2015-03-31 19:41:46

+0

在類模型中,「LeadDeliveryTimes」如何成爲'TimeSpan',而在映射中顯示'bigint/Int64'? – 2015-03-31 20:18:27

回答

1

OnModelCreating不叫 - 正如你所指出 - 所以我可以肯定,您使用的模型首先方法(和而不是 Code-First)。在這種情況下,您負責的所有的映射。 Here is a good article on the topic

看來,RequestIdAppToLeadRequest類的外鍵 - 如果是這種情況,您必須自己在兩個類中聲明public virtual語句。

例如 - 在AppToLeadRequest類的底部,它應該包含:

public virtual ICollection<AppToLeadRequestLine> AppToLeadRequestLines { get; set; }

,並在AppToLeadRequestLine類的底部,它應該包含:

public virtual AppToLeadRequest AppToLeadRequest { get; set; }

我也會將RequestId更改爲AppToLeadRequestId,以遵循<class name>Id格式。

相關問題