列名称无效' User_User_Id'。列名称无效' Group_Group_Id'。 EF6例外

时间:2016-09-02 16:53:37

标签: asp.net-mvc entity-framework entity-framework-6 asp.net-core-mvc

我在运行类库4.6.1的net461框架上安装了ASP.NET Core 1.0 RTM项目,类库中有运行EF 6.1的数据访问存储库。

我收到以下错误: 列名称无效' User_User_Id'。 列名称无效' Group_Group_Id'。

调用以下存储库代码时:

public ReviewWorkflow GetWorkflowWithActors(int reviewWorkflowId)
    {
        return DbContext.ReviewWorkflows.Include(a => a.ReviewWorkflowActors).Where( r => r.ReviewWorkflow_Id == reviewWorkflowId).FirstOrDefault();
    }

实体

public partial class ReviewWorkflow
{
    public ReviewWorkflow()
    {
        this.ReviewWorkflowActors = new List<ReviewWorkflowActor>();
        this.ReviewWorkflowsProjects = new List<ReviewWorkflowsProject>();
    }

    public int ReviewWorkflow_Id { get; set; }
    public string Name { get; set; }
    public string CreatedBy { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public string UpdateBy { get; set; }
    public Nullable<System.DateTime> UpdatedDate { get; set; }
    public byte[] RowVersion { get; set; }
    public int Owner_UserId { get; set; }
    public User User { get; set; }
    public ICollection<ReviewWorkflowActor> ReviewWorkflowActors { get; set; }
    public ICollection<ReviewWorkflowsProject> ReviewWorkflowsProjects { get; set; }

    public ReviewWorkflowView TransformToReviewWorkflowView()
    {
        return new ReviewWorkflowView()
        {
            Name = this.Name,
            ReviewWorkflowActors = this.ReviewWorkflowActors.Select(r => r.TransformToReviewWorkflowActor()).ToList()
        };
    }
}


 public partial class ReviewWorkflowActor
{
    public int ReviewWorkflowActor_Id { get; set; }
    public int ReviewWorkflow_Id { get; set; }
    public byte SequenceNo { get; set; }
    public Nullable<int> Group_Id { get; set; }
    public Nullable<int> User_Id { get; set; }
    public string CreatedBy { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public string UpdateBy { get; set; }
    public Nullable<System.DateTime> UpdatedDate { get; set; }
    public byte[] RowVersion { get; set; }
    public  ReviewWorkflow ReviewWorkflow { get; set; }

    public ReviewWorkflowActorView TransformToReviewWorkflowActor()
    {
        return new ReviewWorkflowActorView() {
            Group_Id = this.Group_Id,
            SequenceNo =  this.SequenceNo,
            User_Id = this.User_Id
        };
    }
}

地图:

public class ReviewWorkflowMap : EntityTypeConfiguration<ReviewWorkflow>
{
    public ReviewWorkflowMap()
    {
        // Primary Key
        this.HasKey(t => t.ReviewWorkflow_Id);

        // Properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(1000);

        this.Property(t => t.CreatedBy)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.UpdateBy)
            .HasMaxLength(50);

        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        // Table & Column Mappings
        this.ToTable("ReviewWorkflows", "Weekly");
        this.Property(t => t.ReviewWorkflow_Id).HasColumnName("ReviewWorkflow_Id");
        this.Property(t => t.Name).HasColumnName("Name");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.CreatedDate).HasColumnName("CreatedDate");
        this.Property(t => t.UpdateBy).HasColumnName("UpdateBy");
        this.Property(t => t.UpdatedDate).HasColumnName("UpdatedDate");
        this.Property(t => t.RowVersion).HasColumnName("RowVersion");
        this.Property(t => t.Owner_UserId).HasColumnName("Owner_UserId");

        // Relationships
        this.HasRequired(t => t.User)
            .WithMany(t => t.ReviewWorkflows)
            .HasForeignKey(d => d.Owner_UserId);

    }
}


public class ReviewWorkflowActorMap : EntityTypeConfiguration<ReviewWorkflowActor>
{
    public ReviewWorkflowActorMap()
    {
        // Primary Key
        this.HasKey(t => t.ReviewWorkflowActor_Id);

        // Properties
        this.Property(t => t.CreatedBy)
            .IsRequired()
            .HasMaxLength(50);

        this.Property(t => t.UpdateBy)
            .HasMaxLength(50);

        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion();

        // Table & Column Mappings
        this.ToTable("ReviewWorkflowActors", "Weekly");
        this.Property(t => t.ReviewWorkflowActor_Id).HasColumnName("ReviewWorkflowActor_Id");
        this.Property(t => t.ReviewWorkflow_Id).HasColumnName("ReviewWorkflow_Id");
        this.Property(t => t.SequenceNo).HasColumnName("SequenceNo");
        this.Property(t => t.Group_Id).HasColumnName("Group_Id");
        this.Property(t => t.User_Id).HasColumnName("User_Id");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.CreatedDate).HasColumnName("CreatedDate");
        this.Property(t => t.UpdateBy).HasColumnName("UpdateBy");
        this.Property(t => t.UpdatedDate).HasColumnName("UpdatedDate");
        this.Property(t => t.RowVersion).HasColumnName("RowVersion");

        // Relationships
        this.HasRequired(t => t.ReviewWorkflow)
            .WithMany(t => t.ReviewWorkflowActors)
            .HasForeignKey(d => d.ReviewWorkflow_Id);

    }
}

其他:

[DbConfigurationType(typeof(EFDbConfiguration))]
public class EWRSContext : DbContext
{
    public EWRSContext()
        : base(_connectionString)
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
    public  DbSet<Murad> Muradies { get; set; }
    public DbSet<PositionHierarchy> PositionHierarchy { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<OrganizationHierarchy> OrganizationHierarchy { get; set; }
    public DbSet<Delegation> Delegations { get; set; }
    public DbSet<GroupPermission> GroupPermissions { get; set; }
    public DbSet<Group> Groups { get; set; }
    public DbSet<GroupUser> GroupUsers { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<Configuration> Configurations { get; set; }
    public DbSet<Notification> Notifications { get; set; }
    public DbSet<NotificationsUser> NotificationsUsers { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<ProjectStatus> ProjectStatuses { get; set; }
    public DbSet<ReviewWorkflowInstance> ReviewWorkflowInstances { get; set; }
    public DbSet<ReviewWorkflow> ReviewWorkflows { get; set; }
    public DbSet<ReviewWorkflowsProject> ReviewWorkflowsProjects { get; set; }
    public DbSet<Subject> Subjects { get; set; }
    public DbSet<SubjectStatus> SubjectStatuses { get; set; }
    public DbSet<TeamModel> TeamModels { get; set; }
    public DbSet<TeamModelSubject> TeamModelSubjects { get; set; }
    public DbSet<Template> Templates { get; set; }
    public DbSet<WeeklyInput> WeeklyInputs { get; set; }
    public DbSet<WeeklyInputHistory> WeeklyInputHistories { get; set; }
    public DbSet<BusinessUnitesView> BusinessUnitesViews { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        //Murad :: Fluent API configuration goes here
        modelBuilder.Configurations.Add(new PositionHierarchyMap());
        modelBuilder.Configurations.Add(new UserMap());
        modelBuilder.Configurations.Add(new OrganizationHierarchyMap());
        modelBuilder.Configurations.Add(new DelegationMap());
        modelBuilder.Configurations.Add(new GroupPermissionMap());
        modelBuilder.Configurations.Add(new GroupMap());
        modelBuilder.Configurations.Add(new GroupUserMap());
        modelBuilder.Configurations.Add(new PermissionMap());
        modelBuilder.Configurations.Add(new ConfigurationMap());
        modelBuilder.Configurations.Add(new NotificationMap());
        modelBuilder.Configurations.Add(new NotificationsUserMap());
        modelBuilder.Configurations.Add(new ProjectMap());
        modelBuilder.Configurations.Add(new ProjectStatusMap());
        modelBuilder.Configurations.Add(new ReviewWorkflowInstanceMap());
        modelBuilder.Configurations.Add(new ReviewWorkflowMap());
        modelBuilder.Configurations.Add(new ReviewWorkflowsProjectMap());
        modelBuilder.Configurations.Add(new SubjectMap());
        modelBuilder.Configurations.Add(new SubjectStatusMap());
        modelBuilder.Configurations.Add(new TeamModelMap());
        modelBuilder.Configurations.Add(new TeamModelSubjectMap());
        modelBuilder.Configurations.Add(new TemplateMap());
        modelBuilder.Configurations.Add(new WeeklyInputMap());
        modelBuilder.Configurations.Add(new WeeklyInputHistoryMap());
        modelBuilder.Configurations.Add(new BusinessUnitesViewMap());
        modelBuilder.Configurations.Add(new MuradConfigurationMap());
        modelBuilder.Configurations.Add(new ReviewWorkflowActorMap());

    }

    //Add Db Custom validation Errors
    protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
    {
        //validate Project Data Insertion
        if (entityEntry.Entity is Project && (entityEntry.State == EntityState.Added || entityEntry.State == EntityState.Modified))
        {
            var dbErrors = new ProjectValidator().Validate(entityEntry);
            if (dbErrors != null)
                return dbErrors;
        }


        return base.ValidateEntity(entityEntry, items);

    }


}

1 个答案:

答案 0 :(得分:1)

您尝试做的事情似乎与与

相关联的RelationShip有关

public User User { get; set; }

ReviewWorkflow

中的

并在ReviewWorkflowActor

public Nullable<int> Group_Id { get; set; } public Nullable<int> User_Id { get; set; }&lt; ---主要问题是它试图通过惯例进行映射

这2个nullables是您目前的问题,因为它不知道Group_Id的来源,也不知道User_Id它不知道如何将这2列映射到表格中的列,因为它们不会t存在于您显示的2个表中。

EF不知道如何解决它们因此你的错误,这种关系可能与外键问题有关,它也可能与组/用户表有关。