【发布时间】:2016-09-02 16:53:37
【问题描述】:
我在运行 Class Library 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);
}
}
【问题讨论】:
-
我相信您正在访问 ShadowProperty,并且由于您确实有 2 个属性为
Nullable<int>,它们与各自的类型相关,我相信 EFC 会说它们在桌面上的位置,因为它认为它们是假设到那里。您是否尝试将现有数据库中的 DbContext 创建工具与 EFC 结合使用?包管理器控制台命令有它们docs.efproject.net/en/latest/miscellaneous/cli/… -
记住不要延迟或显式加载,因此它也可能会丢失其他必要的东西,因此 Include / ThenInclude (可能是必要的)。
-
对不起,我是 EF6 的新手,我没有访问任何影子属性,我使用 VS 2015 的 ef 电动工具(修改后)生成我的上下文。
-
你在使用哪个因为你的标题说 EF Core 不是 EF6
-
你指的是什么标题,我的标题明确表示 ef6
标签: asp.net-mvc entity-framework entity-framework-6 asp.net-core-mvc