0

我有一个用 ASP.NET MVC4、EntityFramework Code First 和 Razor 编写的系统。其中一个模型具有以下语句:

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

如前所述,Flour有一个集合FlourAnalysis。该模型描述如下:

[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}

我做错了什么?

4

2 回答 2

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

请注意,在所有情况下,我都从您的代码中省略了其他属性,只是为了突出显示我添加的内容。

是一篇旧文章,解释了如何使用该属性。

使用预先存在的数据库以查看您是否已正确定义映射的一个有用提示是安装EF Power Tools并使用查看实体数据模型 DDL SQL选项来查看 EF认为您的数据库是什么样的。如果生成的 SQL 与您的实际数据库不匹配,则您知道需要修改模型注释或配置。

于 2013-10-22T14:44:40.257 回答
0

默认情况下,实体框架将假定数据库中所有表的名称都是复数形式,或者在代码优先的情况下,您希望它们在创建时是复数形式。

检查这个希望它会有所帮助

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

于 2013-10-19T09:07:53.493 回答