当我尝试映射实体 (Entity Framework) 的属性时,我收到错误消息类型“__”必须是不可空值类型

When I try to map the properties for an entity (Entity Framework), I get the error the type '__' must be a non-nullable value type

我的映射 class 如下所示:

public class EmployeeMap : EntityTypeConfiguration<EFEmployee>
{
    public EmployeeMap()
    {
        HasKey(t => t.Id);
        Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(t => t.HireDate).IsRequired();
        //Property(t => t.JobTitle).IsRequired();
        Property(t => t.Salary).IsRequired();
        //Property(t => t.Certifications).IsRequired();
        Property(t => t.VacationTime).IsRequired();
        Property(t => t.SickTime).IsRequired();
        //Property(t => t.Identity).IsRequired();
        //Property(t => t.Location).IsRequired();
        //Property(t => t.ReportingTo).IsRequired();
        //Property(t => t.Managing).IsRequired();
        ToTable("EFEmployees");
        HasRequired(t => t.EFOffice).WithMany(u => u.EFEmployees);
    }
}

被注释掉的 属性 映射会产生以下错误:

The type 'System.Collections.Generic.List<string>' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method [long unimportant chain of namespaces with ModelConfiguration and TStructuralType].

嗯,这并不完全准确。上面的错误消息专门针对 JobTitle 和 Certifications 的映射,因为它们是类型为 List 的两个属性。其他错误除了对应的类型不同外都是一样的。

显然所有这些类型都可以为空,但出于某种原因它不喜欢。对于可空类型,我应该使用与 IsRequired() 不同的方法吗?或者我应该向这些可空类型添加一些东西以强制它们不可为空吗?我对如何映射这些属性有点困惑。

编辑:这是 EFEmployee class:

/// <summary>
/// The Class model for the Employee Entity
/// </summary>
public class EFEmployee : EFBusinessEntity
{
    /// <summary>
    /// The hire date of the employee
    /// </summary>
    public DateTime HireDate { get; set; }

    /// <summary>
    /// The list of jobtitles for the employee
    /// </summary>
    public List<string> JobTitle { get; set; }

    /// <summary>
    /// The Employee's salary (note: not attached to jobtitle necessarily)
    /// </summary>
    public int Salary { get; set; }

    /// <summary>
    /// List of Certifications for the employee
    /// </summary>
    public List<string> Certifications { get; set; }

    /// <summary>
    /// Employee's stored up vacation time
    /// </summary>
    public int VacationTime { get; set; }

    /// <summary>
    /// Employee's stored up sick time
    /// </summary>
    public int SickTime { get; set; }

    /// <summary>
    /// The personal information about the employee, stored as a person Entity
    /// </summary>
    public EFPerson Identity { get; set; }

    /// <summary>
    /// The office the Employee works at, stored as an office Entity
    /// </summary>
    public EFOffice Location { get; set; }

    /// <summary>
    /// The Person that the Employee reports to, stored as another Employee Entity
    /// </summary>
    public EFEmployee ReportingTo { get; set; }

    /// <summary>
    /// The list of Employees that this Employee manages, a list of Entities
    /// </summary>
    public List<EFEmployee> Managing { get; set; }

    /// <summary>
    /// Constructor for an Entity. Only requires the properties that cannot be null.
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="TenantId"></param>
    /// <param name="hire"></param>
    /// <param name="titles"></param>
    /// <param name="salary"></param>
    /// <param name="vacationTime"></param>
    /// <param name="sickTime"></param>
    /// <param name="identity"></param>
    /// <param name="location"></param>
    public EFEmployee(Guid Id, Guid TenantId, DateTime hire, List<string> titles, int salary,
        int vacationTime, int sickTime, EFPerson identity, EFOffice location)
    {

        this.HireDate = hire;
        this.Identity = identity;
        this.JobTitle = titles;
        this.Location = location;
        this.Salary = salary;
        this.SickTime = sickTime;
        this.VacationTime = vacationTime;
    }


    public EFOffice EFOffice { get; set; }
}

对所有 XML 评论表示抱歉。

Entity Framework 通常将 classes 映射到数据库表,将实体的属性 classes 映射到数据库列类型,具体取决于您的提供商。 (例如,您可以看到 CLR 类型到 SQL 服务器 here 的映射,但我不确定这是否是 EF SQL 服务器提供程序使用的。)您的错误告诉您EF 无法使用 Property 为这些属性配置映射,因为它们不是值类型(或复杂类型)。

  1. 对于具有您自己的 class 类型的属性(并且打算映射到其他表),您需要使用 configure relationships 的函数。例如:

    HasMany(e => e.Managing).WithRequired(p => p.Manager)
    
  2. 我不认为 List<string> 是可映射的 属性,如果你想让 EF 忽略它,你可以使用:

    Ignore(t => t.JobTitle)
    

    或者您需要创建一个新的 class 来保存 JobTitle 并为其配置关系。

通常,您甚至不需要映射每个 属性 和关系,因为 EF 可以从您的 class 定义本身推断映射。

参见