实体框架无法投射错误

时间:2014-09-30 09:03:43

标签: c# entity-framework

在尝试使用实体框架保存数据时,我收到以下错误:

  

其他信息:无法将类型为“Domain.SubProcessRoot.SubProcess”的对象强制转换为类型Domain.BalancingRuleRoot.BalancingRule'。

问题似乎是由于为每个实体引入了基类。在基类中,主要字段声明为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);

        }
    }

0 个答案:

没有答案