我正在尝试使用EF5中的查找表实现Enums。我先使用模特。我有一个名为“Appraisal”的主要实体,它有一个列“Appraisal_Type_ID”。这是数据库中的INT。我还有一个名为“Appraisal_Type”的实体,它还包含“Appraisal_Type_ID”以及描述。两个实体之间存在关联,我在主Appraisal表和查找表上将Int切换为枚举。以下是生成的代码中两个实体的外观:
public partial class Appraisal
{
public Appraisal()
{
this.Goals = new HashSet<Goal>();
this.NewGoals = new HashSet<NewGoal>();
this.CompetencyResults = new HashSet<CompetencyResult>();
this.Attachments = new HashSet<Attachment>();
this.Notes = new HashSet<Note>();
this.NewCompetency_Results = new HashSet<NewCompetency_Results>();
this.AppraisalActivityLogEntries = new HashSet<AppraisalActivityLogEntry>();
}
public int Appraisal_ID { get; set; }
public string Penn_ID { get; set; }
public string Review_Year { get; set; }
public string Supervisor_Penn_ID { get; set; }
public AppraisalType Appraisal_Type_ID { get; set; }
public int Appraisal_Status_ID { get; set; }
public Nullable<System.DateTime> Last_Update_Date { get; set; }
public string Last_Update_User { get; set; }
public string Future_Development { get; set; }
public string Rating_Comments { get; set; }
public Nullable<System.DateTime> Supervisor_Sign_Date { get; set; }
public Nullable<System.DateTime> Employee_Sign_Date { get; set; }
public string Employee_Comments { get; set; }
public Nullable<System.DateTime> Next_ReviewDate { get; set; }
public string Home_Dept_Org { get; set; }
public string Home_School_Ctr { get; set; }
public Nullable<int> ContentType_ID { get; set; }
public string Name { get; set; }
public string Rating_Code { get; set; }
public Nullable<bool> Final_Step_Reached { get; set; }
public string Supervisor_Name { get; set; }
public string Supervisor_Job_Class { get; set; }
public string Supervisor_Job_Title { get; set; }
public string Current_Editor { get; set; }
public string Current_Editor_Name { get; set; }
public virtual ICollection<Goal> Goals { get; set; }
public virtual ICollection<NewGoal> NewGoals { get; set; }
public virtual ICollection<CompetencyResult> CompetencyResults { get; set; }
public virtual ICollection<Attachment> Attachments { get; set; }
public virtual ICollection<Note> Notes { get; set; }
public virtual AppraisalUser AppraisalUser { get; set; }
public virtual ICollection<NewCompetency_Results> NewCompetency_Results { get; set; }
public virtual ContentType ContentType { get; set; }
public virtual ICollection<AppraisalActivityLogEntry> AppraisalActivityLogEntries { get; set; }
public virtual Appraisal_Status Status { get; set; }
public virtual Rating Rating { get; set; }
public virtual Appraisal_Type AppraisalType { get; set; }
}
public partial class Appraisal_Type
{
public AppraisalType Appraisal_Type_ID { get; set; }
public string Appraisal_Type_Desc { get; set; }
}
public enum AppraisalType : int
{
Appraisal = 2,
SelfAppraisal = 1
}
我可以轻松地从数据库中获取Appraisal,并且它可以与enum一起使用,我可以轻松地从数据库中获取Appraisal_Type数据,并且它可以与枚举一起使用。
但是,目标是使用.Include来获取带有评估的Appraisal_Type。这会引发错误。
public Appraisal GetAppraisal(int appraisalID)
{
using (AppraisalEntities context = new AppraisalEntities(AppraisalEntitiesConnectionString))
{
context.Configuration.LazyLoadingEnabled = false;
context.Configuration.ProxyCreationEnabled = true;
Appraisal data = context.Appraisals
.Include("Goals")
.Include("NewGoals")
.Include("CompetencyResults")
.Include("CompetencyResults.Competency")
.Include("NewCompetency_Results")
.Include("NewCompetency_Results.NewCompetency")
.Include("Attachments")
.Include("Rating")
.Include("Notes")
.Include("AppraisalUser")
.Include("Status")
.Include("AppraisalType")
.Include("ContentType")
.First(c => c.Appraisal_ID == appraisalID);
return data;
}
}
当我运行此代码时,出现以下错误:
* 关键字段'Appraisal_Type_ID'的类型应为'PennHR.HRManager.BusinessServices.Appraisals.AppraisalType',但提供的值实际上是'System.Int32'类型。*
看起来EF正在尝试验证密钥,因为我在调用堆栈中看到了这一点:
System.Data.EntityKey.ValidateTypeOfKeyValue(MetadataWorkspace workspace, EdmMember keyMember, Object keyValue, Boolean isArgumentException, String argumentName) +11452250
System.Data.EntityKey.ValidateEntityKey(MetadataWorkspace workspace, EntitySet entitySet, Boolean isArgumentException, String argumentName) +158
System.Data.Objects.ObjectStateManager.CheckKeyMatchesEntity(IEntityWrapper wrappedEntity, EntityKey entityKey, EntitySet entitySetForType, Boolean forAttach) +84
System.Data.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded) +269
System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) +262
lambda_method(Closure , Shaper ) +2651
System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +10995785
System.Data.Common.Internal.Materialization.RowNestedResultEnumerator.MoveNext() +277
System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.TryReadToNextElement() +31
System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.MoveNext() +78
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +247
System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate) +490
PennHR.HRManager.BusinessServices.Appraisals.AppraisalManager.GetAppraisal(String pennID, String reviewYear, AppraisalType type) in d:\Users\Blickley\Documents\Visual Studio 2012\Projects\HRManager\Trunk\HRManagerSolution\HRManager.BusinessServices\Appraisals\AppraisalManager.cs:359
PennHR.HRManager.Portal.Appraisals.AppraisalsDefault.btnViewSelfAppraisal_Click(Object sender, EventArgs e) in d:\Users\Blickley\Documents\Visual Studio 2012\Projects\HRManager\Trunk\HRManagerSolution\HRManager.Portal\Appraisals\AppraisalsDefault.aspx.cs:228
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804
如果我删除AppraisalType上的.Include,一切都很好。此代码也可以正常工作,因此它似乎是导致问题的包含:
public Appraisal_Type GetAppraisalTypes()
{
AppraisalEntities context = new AppraisalEntities(AppraisalEntitiesConnectionString);
Appraisal_Type data = context.Appraisal_Types
.FirstOrDefault(c => c.Appraisal_Type_ID == AppraisalType.Appraisal);
return data;
}
我似乎无法在错误上找到太多,有没有人能想到解决这个问题的方法?我希望能够将枚举与我的查找表一起使用。如果我删除枚举并将其切换回Int,一切正常。