【问题标题】: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当我尝试映射实体(实体框架)的属性时,我收到错误类型 '__' must be an non-nullable value type
【发布时间】:2015-08-25 00:18:39
【问题描述】:

我的映射类如下所示:

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

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

类型“System.Collections.Generic.List”必须是不可为空的值类型,才能将其用作泛型类型或方法中的参数“T”[带有 ModelConfiguration 和TStructuralType]。

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

  • Identity 是 EFPerson 类型(EFPerson 是我做的一个类)
  • Lo​​cation 是 EFOffice 类型(EFOffice 是我制作的另一个类)
  • ReportingTo 是一个 EFEmployee 类型(EFEmployee 是我创建的另一个类)
  • 管理是 List 类型

显然所有这些类型都是可以为空的,出于某种原因它不喜欢。对于可空类型,我应该使用不同于 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 cmets。

【问题讨论】:

  • 你能粘贴你的 EEmployee 类吗?
  • 您好,很抱歉回复晚了。我添加了 EFEmployee 类

标签: c# entity-framework generics mapping nullable


【解决方案1】:

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

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

    HasMany(e => e.Managing).WithRequired(p => p.Manager)
    
  2. 我不认为List&lt;string&gt; 是可映射的属性,如果您希望EF 忽略它,可以使用:

    Ignore(t => t.JobTitle)
    

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

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

【讨论】:

  • 哦,有趣,感谢您的回答!我想对于 list 类型,我会尝试忽略它,看看会发生什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2017-05-28
  • 2015-01-22
  • 2018-02-16
  • 1970-01-01
  • 2017-08-15
相关资源
最近更新 更多