C# 实体框架无法转换错误
尝试使用实体框架保存数据时,我遇到以下错误: 其他信息:无法将“Domain.SubProcessRoot.SubProcess”类型的对象强制转换为“Domain.BalancingRuleRoot.BalancingRule”类型 这个问题似乎是由于为每个实体引入了基类。在基类中,主字段声明为C# 实体框架无法转换错误,c#,entity-framework,C#,Entity Framework,尝试使用实体框架保存数据时,我遇到以下错误: 其他信息:无法将“Domain.SubProcessRoot.SubProcess”类型的对象强制转换为“Domain.BalancingRuleRoot.BalancingRule”类型 这个问题似乎是由于为每个实体引入了基类。在基类中,主字段声明为Id。随着基类的引入,我更改了每个类的映射配置,以指向主键(Id基类的属性)。如果我从每个实体中删除基类,并在相应的类中声明主键,那么一切都会正常工作。请帮忙。映射配置和实体包括: public abs
Id
。随着基类的引入,我更改了每个类的映射配置,以指向主键(Id
基类的属性)。如果我从每个实体中删除基类,并在相应的类中声明主键,那么一切都会正常工作。请帮忙。映射配置和实体包括:
public abstract class EntityBase<TId>
{
public TId Id { get; set; }
protected abstract void Validate();
public override bool Equals(object entity)
{
return entity != null
&& entity is EntityBase<TId>
&& this == (EntityBase<TId>)entity;
}
public override int GetHashCode()
{
return this.Id.GetHashCode();
}
public static bool operator ==(EntityBase<TId> entity1,
EntityBase<TId> entity2)
{
if ((object)entity1 == null && (object)entity2 == null)
{
return true;
}
if ((object)entity1 == null || (object)entity2 == null)
{
return false;
}
if (entity1.Id.ToString() == entity2.Id.ToString())
{
return true;
}
return false;
}
public static bool operator !=(EntityBase<TId> entity1,
EntityBase<TId> entity2)
{
return (!(entity1 == entity2));
}
}
public class MainProcess:EntityBase<int>
{
public MainProcess()
{
this.MainProcessEmail = new List<MainProcessEmail>();
this.SubProcess = new List<SubProcess>();
this.UserAdminRight = new List<UserAdminRight>();
}
//public int iMainProcessId { get; set; }
public string MainProcessname { get; set; }
public string MainProcessDesc { get; set; }
public string OwningDept { get; set; }
public bool EmailAlertEnabled { get; set; }
public bool FirstChoiceTicketsEnabled { get; set; }
public int MaxFirstChoiceTicketsPerDay { get; set; }
public int FirstChoiceQueueId { get; set; }
public int ApplicationId { get; set; }
public bool ActiveInd { get; set; }
public virtual Application Application { get; set; }
public virtual FirstChoiceQueue FirstChoiceQueue { get; set; }
public virtual ICollection<MainProcessEmail> MainProcessEmail { get; set; }
public virtual ICollection<SubProcess> SubProcess { get; set; }
public virtual ICollection<UserAdminRight> UserAdminRight { get; set; }
protected override void Validate()
{
throw new NotImplementedException();
}
}
public class SubProcess:EntityBase<int>
{
public SubProcess()
{
this.SubProcessRunInstance = new List<SubProcessRunInstance>();
this.SubProcessAlertTypeRel = new List<SubProcessAlert>();
this.BalancingRules = new List<BalancingRule>();
this.R_SubProcessSetup = new List<SubProcessSetup>();
}
//public int SubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessDesc { get; set; }
public int SignOffTypeId { get; set; }
public bool FirstChoiceEnabled { get; set; }
public int MainProcesId { get; set; }
public bool ActiveInd { get; set; }
public virtual ICollection<SubProcessRunInstance> SubProcessRunInstance { get; set; }
public virtual MainProcess MainProcess { get; set; }
public virtual SignOffType SignOffType { get; set; }
public virtual ICollection<SubProcessAlert> SubProcessAlertTypeRel { get; set; }
public virtual ICollection<BalancingRule> BalancingRules { get; set; }
public virtual ICollection<SubProcessSetup> R_SubProcessSetup { get; set; }
protected override void Validate()
{
throw new NotImplementedException();
}
}
public partial class BalancingRule:EntityBase<int>
{
// public int BalancingRuleId { get; set; }
public int SubProcessId { get; set; }
public int BalanceTypeId { get; set; }
public int BalanceFactorTypeId { get; set; }
public int BalancingUnitTypeId { get; set; }
public int BalancingOperatorTypeId { get; set; }
public Nullable<decimal> MinBalanceFactorValue { get; set; }
public Nullable<decimal> MaxBalanceFactorValue { get; set; }
public string BalanceTypeName { get; set; }
public bool ActiveInd { get; set; }
public virtual BalanceFactorType BalanceFactorType { get; set; }
public virtual BalancingType BalancingType { get; set; }
public virtual BalancingOperatorType BalancingOperatorType { get; set; }
public virtual BalancingUnitType BalancingUnitType { get; set; }
public virtual SubProcess SubProcess { get; set; }
protected override void Validate()
{
throw new NotImplementedException();
}
}
public class MainProcessMap : EntityTypeConfiguration<MainProcess>
{
public MainProcessMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.MainProcessname)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.MainProcessDesc)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.OwningDept)
.IsRequired()
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("R_MainProcess");
this.Property(t => t.Id).HasColumnName("iMainProcessId");
this.Property(t => t.MainProcessname).HasColumnName("vcMainProcessname");
this.Property(t => t.MainProcessDesc).HasColumnName("vcMainProcessDesc");
this.Property(t => t.OwningDept).HasColumnName("vcOwningDept");
this.Property(t => t.EmailAlertEnabled).HasColumnName("bEmailAlertEnabled");
this.Property(t => t.FirstChoiceTicketsEnabled).HasColumnName("bFirstChoiceTicketsEnabled");
this.Property(t => t.MaxFirstChoiceTicketsPerDay).HasColumnName("iMaxFirstChoiceTicketsPerDay");
this.Property(t => t.FirstChoiceQueueId).HasColumnName("iFirstChoiceQueueIdFK");
this.Property(t => t.ApplicationId).HasColumnName("iApplicationIdFK");
this.Property(t => t.ActiveInd).HasColumnName("siActiveInd");
// Relationships
this.HasRequired(t => t.Application)
.WithMany(t => t.MainProcess)
.HasForeignKey(d => d.ApplicationId);
this.HasRequired(t => t.FirstChoiceQueue)
.WithMany(t => t.R_MainProcess)
.HasForeignKey(d => d.FirstChoiceQueueId);
}
public class SubProcessMap : EntityTypeConfiguration<SubProcess>
{
public SubProcessMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.SubProcessName)
.IsRequired()
.HasMaxLength(50);
this.Property(t => t.SubProcessDesc)
.IsRequired()
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("R_SubProcess");
this.Property(t => t.Id).HasColumnName("iSubProcessId");
this.Property(t => t.SubProcessName).HasColumnName("vcSubProcessName");
this.Property(t => t.SubProcessDesc).HasColumnName("vcSubProcessDesc");
this.Property(t => t.SignOffTypeId).HasColumnName("iSignOffTypeIdFK");
this.Property(t => t.FirstChoiceEnabled).HasColumnName("bFirstChoiceEnabled");
this.Property(t => t.MainProcesId).HasColumnName("iMainProcesIdFK");
this.Property(t => t.ActiveInd).HasColumnName("siActiveInd");
// Relationships
this.HasRequired(t => t.MainProcess)
.WithMany(t => t.SubProcess)
.HasForeignKey(d => d.MainProcesId);
this.HasRequired(t => t.SignOffType)
.WithMany(t => t.R_SubProcess)
.HasForeignKey(d => d.SignOffTypeId);
}
}
public class BalancingRuleMap : EntityTypeConfiguration<BalancingRule>
{
public BalancingRuleMap()
{
// Primary Key
this.HasKey(t => t.Id);
// Properties
this.Property(t => t.BalanceTypeName)
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("R_SubProcessBalanceFactorTypeRel");
this.Property(t => t.Id).HasColumnName("iSubProcessBalanceFactorTypeRelId");
this.Property(t => t.SubProcessId).HasColumnName("iSubProcessIdFK");
this.Property(t => t.BalanceTypeId).HasColumnName("iBalanceTypeIdFK");
this.Property(t => t.BalanceFactorTypeId).HasColumnName("iBalanceFactorTypeIdFK");
this.Property(t => t.BalancingUnitTypeId).HasColumnName("iBalancingUnitTypeIdFK");
this.Property(t => t.BalancingOperatorTypeId).HasColumnName("iBalancingOperatorTypeIdFK");
this.Property(t => t.MinBalanceFactorValue).HasColumnName("dMinBalanceFactorValue");
this.Property(t => t.MaxBalanceFactorValue).HasColumnName("dMaxBalanceFactorValue");
this.Property(t => t.BalanceTypeName).HasColumnName("vcBalanceTypeName");
this.Property(t => t.ActiveInd).HasColumnName("siActiveInd");
// Relationships
this.HasRequired(t => t.BalanceFactorType)
.WithMany(t => t.R_SubProcessBalanceFactorTypeRel)
.HasForeignKey(d => d.BalanceFactorTypeId);
this.HasRequired(t => t.BalancingType)
.WithMany(t => t.R_SubProcessBalanceFactorTypeRel)
.HasForeignKey(d => d.BalanceTypeId);
this.HasRequired(t => t.BalancingOperatorType)
.WithMany(t => t.R_SubProcessBalanceFactorTypeRel)
.HasForeignKey(d => d.BalancingOperatorTypeId);
this.HasRequired(t => t.BalancingUnitType)
.WithMany(t => t.BalancingRules)
.HasForeignKey(d => d.BalancingUnitTypeId);
this.HasRequired(t => t.SubProcess)
.WithMany(t => t.BalancingRules)
.HasForeignKey(d => d.SubProcessId);
}
}
公共抽象类EntityBase
{
公共TId Id{get;set;}
受保护的抽象void Validate();
公共覆盖布尔等于(对象实体)
{
返回实体!=null
&&实体是EntityBase
&&此==(EntityBase)实体;
}
公共覆盖int GetHashCode()
{
返回此.Id.GetHashCode();
}
公共静态布尔运算符==(EntityBase entity1,
EntityBase entity2)
{
如果((对象)entity1==null&&(对象)entity2==null)
{
返回true;
}
如果((对象)entity1==null | |(对象)entity2==null)
{
返回false;
}
if(entity1.Id.ToString()==entity2.Id.ToString())
{
返回true;
}
返回false;
}
公共静态布尔运算符!=(EntityBase entity1,
EntityBase entity2)
{
返回(!(entity1==entity2));
}
}
公共类主进程:EntityBase
{
公共进程()
{
this.MainProcessEmail=新列表();
this.SubProcess=新列表();
this.UserAdminRight=新列表();
}
//public int-iMainProcessId{get;set;}
公共字符串MainProcessname{get;set;}
公共字符串mainprocesdesc{get;set;}
公共字符串OwningDept{get;set;}
公共bool EmailAlertEnabled{get;set;}
public bool FirstChoiceTicketsEnabled{get;set;}
public int MaxFirstChoiceTicketsPerDay{get;set;}
public int FirstChoiceQueueId{get;set;}
public int ApplicationId{get;set;}
公共bool ActiveInd{get;set;}
公共虚拟应用程序应用程序{get;set;}
公共虚拟FirstChoiceQueue FirstChoiceQueue{get;set;}
公共虚拟ICollection MainProcessEmail{get;set;}
公共虚拟ICollection子进程{get;set;}
公共虚拟ICollection UserAdminRight{get;set;}
受保护的覆盖无效验证()
{
抛出新的NotImplementedException();
}
}
公共类子进程:EntityBase
{
公共子流程()
{
this.SubProcessRunInstance=新列表();
this.SubProcessAlertTypeRel=新列表();
this.BalancingRules=新列表();
this.R_SubProcessSetup=新列表();
}
//public int SubProcessId{get;set;}
公共字符串子进程名{get;set;}
公共字符串子进程desc{get;set;}
public int SignOffTypeId{get;set;}
public bool FirstChoiceEnabled{get;set;}
public int MainProcesId{get;set;}
公共bool ActiveInd{get;set;}
公共虚拟ICollection子流程RunInstance{get;set;}
公共虚拟主进程MainProcess{get;set;}
公共虚拟SignOffType SignOffType{get;set;}
公共虚拟ICollection子流程AlertTypeRel{get;set;}
公共虚拟ICollection平衡规则{get;set;}
公共虚拟ICollection R_子流程设置{get;set;}
受保护的覆盖无效验证()
{
抛出新的NotImplementedException();
}
}
公共部分类平衡规则:EntityBase
{
//public int BalancingRuleId{get;set;}
public int SubProcessId{get;set;}
public int BalanceTypeId{get;set;}
public int BalanceFactorTypeId{get;set;}
public int balancingunitypeid{get;set;}
public int BalancingOperatorTypeId{get;set;}
公共可为空的MinBalanceFactoryValue{get;set;}
公共可空MaxBalanceFactoryValue{get;set;}
公共字符串BalanceTypeName{get;set;}
公共bool ActiveInd{get;set;}
公共虚拟平衡因子类型平衡因子类型{get;set;}
公共虚拟平衡类型平衡类型{get;set;}
公共虚拟BalancingOperatorType BalancingOperatorType{get;set;}
公共虚拟balancingunitype balancingunitype{get;set;}
公共虚拟子进程子进程{get;set;}
受保护的覆盖无效验证()
{
抛出新的NotImplementedException();
}
}
公共类MainProcessMap:EntityTypeConfiguration
{
公共MainProcessMap()
{
//主键
this.HasKey(t=>t.Id);
//性质
this.Property(t=>t.MainProcessname)
.IsRequired()
.HasMaxLength(50);
this.Property(t=>t.MainProcessDesc)
.IsRequired()
.HasMaxLength(50);
this.Property(t=>t.OwningDept)
.IsRequired()
.HasMaxLength(50);
//表和列映射
本.ToTable(“R_主流程”);
this.Property(t=>t.Id).HasColumnName(“iMainProcessId”);
this.Property(t=>t.MainProcessname).HasColumnName(“vcMainProcessname”);
this.Property(t=>t.mainprocesdesc).HasColumnName(“vcMainProcessDesc”);
this.Property(t=>t.OwningDept).HasColumnName(“vcOwningDept”);