当我尝试映射实体(实体框架)的属性时,我得到错误类型'__'必须是不可为空的值类型

时间:2015-06-09 23:29:37

标签: c# entity-framework generics mapping nullable

我的映射类如下所示:

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

注释掉的Property映射是产生以下错误的映射:

  

类型'System.Collections.Generic.List&lt; string&gt;'必须是一个不可为空的值类型,以便在泛型类型或方法中使用它作为参数'T'[具有ModelConfiguration和TStructuralType的长期不重要的命名空间链]。

那不完全准确。上面的错误消息专门用于JobTitle和Certifications的映射,因为这两个属性是List类型。除了相应的类型之外,其他错误是相同的。

  • 身份是EFPerson类型(EFPerson是我制作的课程)
  • 位置是EFOffice类型(EFOffice是我制作的另一个类)
  • ReportingTo是EFEmployee类型(EFEmployee是我制作的另一个类)
  • 管理是一个列表&lt; EFEmployee&gt;输入

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

编辑:这是EFEmployee类:

/// <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评论表示抱歉。

1 个答案:

答案 0 :(得分:2)

实体框架通常根据您的提供程序将类映射到数据库表和实体类的属性到数据库列类型。 (例如,您可以看到CLR类型到SQL Server here的映射,但我不确定这是否是EF SQL Server提供程序使用的。)您的错误告诉您EF无法为Property的属性配置maping,因为它们不是值类型(或复杂类型)。

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

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

    Ignore(t => t.JobTitle)
    

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

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

    参见