首页 > 解决方案 > Entity Framework 6 code-first 不创建所有列

问题描述

我首先使用 EF6 和代码创建一个简单的表。除CreateDate列外,所有字段均已创建。这是为什么?

public class InspectionPoint
{
    public DateTime CreateDate { get; }
    public string Detail { get; set; }
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
    public string Question { get; set; }
    public DateTime UpdateDate { get; set; }
}

UpdateDate字段正在按预期创建,但不是CreateDate. 这是为什么?

标签: c#entity-frameworkentity-framework-6

解决方案


这是因为该字段是只读的,因为它只有一个吸气剂:

public class InspectionPoint
{
    // only has "get"ter - therefore it's readonly 
    public DateTime CreateDate { get; }      

    // Every other field has both "get" and "set" and can be set to new values
    public string Detail { get; set; }
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }
    public string Question { get; set; }
    public DateTime UpdateDate { get; set; }
}

推荐阅读