【发布时间】: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 是我做的一个类)
- Location 是 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