0

我有一個系統用ASP.NET MVC4,EntityFramework Code First和Razor編寫。其中一個模型有以下語句:EntityFramework:模型配置與多個不規則名稱

public class Flour : IEntityBase 
{ 
    [Key] 
    public Guid FlourId { get; set; } 
    public Guid ProcessId { get; set; } 

    [Display(Name = "Timestamp", ResourceType = typeof(Resources.Language))] 
    [Timestamp] 
    public Byte[] Timestamp { get; set; } 

    [Display(Name = "FlourAnalyzes", ResourceType = typeof(Resources.Language))] 
    public virtual ICollection<FlourAnalysis> FlourAnalyzes { get; set; } 
    [Display(Name = "Process", ResourceType = typeof(Resources.Language))] 
    public virtual Process Process { get; set; } 

    [Display(Name = "LastModified", ResourceType = typeof(Resources.Language))] 
    public DateTime LastModified { get; set; } 
    [Display(Name = "CreatedOn", ResourceType = typeof(Resources.Language))] 
    public DateTime CreatedOn { get; set; } 
} 

如前所述,FlourFlourAnalysis的集合。下面的模型描述:

[Table(name: "FlourAnalyzes")] 
public class FlourAnalysis : IEntityBase 
{ 
    [Key] 
    public Guid FlourAnalysisId { get; set; } 
    public Guid FlourId { get; set; } 
    public Guid? MeshId { get; set; } 

    [Display(Name = "Timestamp", ResourceType = typeof(Resources.Language))] 
    [Timestamp] 
    public Byte[] Timestamp { get; set; } 

    [DataType(DataType.Date)] 
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm}", ApplyFormatInEditMode = true)] 
    [Display(Name = "StartTimestamp", ResourceType = typeof(Resources.Language))] 
    public DateTime? StartTimestamp { get; set; } 

    [Display(Name = "HumidityPercentage", ResourceType = typeof(Resources.Language))] 
    [Range(0, 100)] 
    public Double HumidityPercentage { get; set; } 

    [Display(Name = "StarchPercentage", ResourceType = typeof(Resources.Language))] 
    [Range(0, 100)] 
    public Double StarchPercentage { get; set; } 

    [DataType(DataType.MultilineText)] 
    [Display(Name = "Comments", ResourceType = typeof(Resources.Language))] 
    public String Comments { get; set; } 

    [Display(Name = "Flour", ResourceType = typeof(Resources.Language))] 
    public virtual Flour Flour { get; set; } 
    [Display(Name = "Mesh", ResourceType = typeof(Resources.Language))] 
    public virtual Mesh Mesh { get; set; } 

    [Display(Name = "LastModified", ResourceType = typeof(Resources.Language))] 
    public DateTime LastModified { get; set; } 
    [Display(Name = "CreatedOn", ResourceType = typeof(Resources.Language))] 
    public DateTime CreatedOn { get; set; } 

    public FlourAnalysis() { 
     this.HumidityPercentage = 0; 
     this.StarchPercentage = 0; 
    } 

產生遷移後,EF創建名稱爲FlourAnalyzes(我需要強制表名,否則將EF奇異創建表)的表。插入到它的一些數據後,EF不會帶來FlourAnalysis數據調用通過上下文Flour對象:

[Authorize] 
public ViewResult Details(System.Guid id) 
{ 
    var flour = context.Flours 
     .Include(f => f.FlourAnalyzes) 
     .Single(x => x.FlourId == id); 

    return View(flour); 
} 

編輯:

一些建議後,我改變了.Single()表達.Where()和生成的SQL指向應該不存在的列,Flour_ProcessId

{SELECT 
[Project1].[C1] AS [C1], 
[Project1].[ProcessId] AS [ProcessId], 
[Project1].[FlourId] AS [FlourId], 
[Project1].[Timestamp] AS [Timestamp], 
[Project1].[LastModified] AS [LastModified], 
[Project1].[CreatedOn] AS [CreatedOn], 
[Project1].[Mesh_MeshId] AS [Mesh_MeshId], 
[Project1].[C2] AS [C2], 
[Project1].[FlourAnalysisId] AS [FlourAnalysisId], 
[Project1].[FlourId1] AS [FlourId1], 
[Project1].[MeshId] AS [MeshId], 
[Project1].[Timestamp1] AS [Timestamp1], 
[Project1].[StartTimestamp] AS [StartTimestamp], 
[Project1].[HumidityPercentage] AS [HumidityPercentage], 
[Project1].[StarchPercentage] AS [StarchPercentage], 
[Project1].[Comments] AS [Comments], 
[Project1].[LastModified1] AS [LastModified1], 
[Project1].[CreatedOn1] AS [CreatedOn1], 
[Project1].[Flour_ProcessId] AS [Flour_ProcessId] 
FROM (SELECT 
    [Extent1].[ProcessId] AS [ProcessId], 
    [Extent1].[FlourId] AS [FlourId], 
    [Extent1].[Timestamp] AS [Timestamp], 
    [Extent1].[LastModified] AS [LastModified], 
    [Extent1].[CreatedOn] AS [CreatedOn], 
    [Extent1].[Mesh_MeshId] AS [Mesh_MeshId], 
    1 AS [C1], 
    [Extent2].[FlourAnalysisId] AS [FlourAnalysisId], 
    [Extent2].[FlourId] AS [FlourId1], 
    [Extent2].[MeshId] AS [MeshId], 
    [Extent2].[Timestamp] AS [Timestamp1], 
    [Extent2].[StartTimestamp] AS [StartTimestamp], 
    [Extent2].[HumidityPercentage] AS [HumidityPercentage], 
    [Extent2].[StarchPercentage] AS [StarchPercentage], 
    [Extent2].[Comments] AS [Comments], 
    [Extent2].[LastModified] AS [LastModified1], 
    [Extent2].[CreatedOn] AS [CreatedOn1], 
    [Extent2].[Flour_ProcessId] AS [Flour_ProcessId], 
    CASE WHEN ([Extent2].[FlourAnalysisId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2] 
    FROM [dbo].[Flours] AS [Extent1] 
    LEFT OUTER JOIN [dbo].[FlourAnalyzes] AS [Extent2] ON [Extent1].[ProcessId] = [Extent2].[Flour_ProcessId] 
WHERE [Extent1].[FlourId] = @p__linq__0 
) AS [Project1] 
ORDER BY [Project1].[ProcessId] ASC, [Project1].[C2] ASC} 

我在做什麼錯了?

+0

你能改說這個問題嗎?目前還不清楚發生了什麼問題。從我可以收集的內容中,您可能有興趣去除約定'modelBuilder.Conventions.Remove ();'? –

+0

當我調用'Details'方法時,在上下文調用中調用'Flours',相關的FlourAnalyzes不會被檢索。沒有必要刪除複數命名約定。 –

+0

你確定你的數據庫中有相關實體的ID嗎? – Pawel

回答

1

爲避免你得到關於Process_ProcessId你需要添加一個屬性錯誤:

[ForeignKey("ProcessId")] 
public virtual Process Process { get; set; } 

這是因爲約定用於生成外鍵將使用錯誤的列名稱。

你可能也需要像這樣在你的FlourAnalysis類:

[ForeignKey("FlourId")] 
public virtual Flour Flour { get; set; } 

[ForeignKey("MeshId")] 
public virtual Mesh Mesh { get; set; } 

需要注意的是,在所有的情況下,我忽略你的代碼中其他屬性只是爲了突出我已經添加了什麼。

Here是一個古老的文章,解釋瞭如何使用該屬性。

一個有用的技巧與預先存在的數據庫的工作,看看您是否已正確定義的映射是安裝EF Power Tools並使用查看實體數據模型DDL SQL選項,看看有什麼EF 認爲你的數據庫看起來像。在生成的SQL與實際數據庫不匹配的情況下,您需要修改模型註釋或配置。

0

默認情況下,實體框架會假定數據庫中所有表的名稱都是複數形式,或者在代碼優先的情況下,您希望在創建時將它們複數化。

檢查這個希望這將有助於

http://edspencer.me.uk/2012/03/13/entity-framework-plural-and-singular-table-names/

+0

正如我以前所說,我主要關心的是關於檢索的數據。表名的生成不是問題。但是我感謝你的回答。 –