我有两个班级,其中Plan
的列表为PlanTask
,如下所示。但是,当它最初制作时,外键的属性没有从PlanTask
转到Plan
。但是现在我需要Plan
上的PlanTask
ID。我的问题在于,由于这两者之间存在多对多的关系,Entity Framework创建了一个外键列。但是由于我没有访问该列的属性,我无法访问该ID。所以我的问题是,我可以以某种方式访问自动生成的列,或者我可以编辑我的类,以便我不删除已存在的数据。
安排:
public class Plan
{
public int PlanId { get; set; }
[DisplayName("Navn")]
public string Name { get; set; }
public IList<PlanTask> Tasks { get; set; }
public bool IsActive { get; set; }
public ICollection<Department> Departments { get; set; }
public bool IsCompleted { get; set; }
public PlanStages Stage { get; set; }
public DateTime? ApprovedDate { get; set; }
[DisplayName("Ejer")]
public User Owner { get; set; }
[DisplayName("Ejer e-mail")]
public string OwnerMail { get; set; }
}
PlanTask:
public class PlanTask
{
public int PlanTaskId { get; set; }
public bool IsActive { get; set; }
[DisplayName("Beskrivelse/Årsag")]
[Required(ErrorMessage = "Indtast venligst en beskrivelse/årsag")]
public string Description { get; set; }
[DisplayName("Løsning")]
public string Solution { get; set; }
[DisplayName("Ansvarlig e-Mail")]
[Required(ErrorMessage = "Indtast venligst den ansvarliges mail")]
public string ManagerMail { get; set; }
[DisplayName("Ansvarlig navn")]
[Required(ErrorMessage = "Indtast venligst den ansvarliges navn")]
public string ManagerName { get; set; }
[DisplayName("Videresend til navn")]
public string ForwardName { get; set; }
[DisplayName("Videresend til e-Mail")]
public string ForwardMail { get; set; }
[DisplayName("Dato udført")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? DateCompleted { get; set; }
[DisplayName("Deadline for udførelse")]
public DateTime? Deadline { get; set; }
[DisplayName("Dato der er fulgt op")]
public DateTime? FollowupDate { get; set; }
[DisplayName("Påmindelses dato")]
public DateTime? ReminderDate { get; set; }
[DisplayName("Vedhæft fil")]
public string FileName { get; set; }
}
的DataContext:
public class DataContext : IdentityDbContext<User>
{
public DataContext() : base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("AspNetUsers");
modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
modelBuilder.Entity<User>()
.HasMany(x => x.Departments)
.WithMany(c => c.Users);
modelBuilder.Entity<Department>()
.HasMany(x => x.Users)
.WithMany(c => c.Departments);
base.OnModelCreating(modelBuilder);
}
public static DataContext Create()
{
return new DataContext();
}
public DbSet<Template> Templates { get; set; }
public DbSet<Section> Sections { get; set; }
public DbSet<Question> Questions { get; set; }
public DbSet<SubmittedSheet> SubmittedSheets { get; set; }
public DbSet<SubmittedQuestion> SubmittedQuestions { get; set; }
public DbSet<Department> Departments { get; set; }
public DbSet<Role> WorkRoles { get; set; }
public DbSet<Actionplan> Plans { get; set; }
public DbSet<ActionPlanTask> PlanTasks { get; set; }
//public DbSet<User> Users { get; set; }
}
答案 0 :(得分:1)
或者我可以编辑我的课程,以便我不会删除已经存在的数据。
是。只需添加
TransactionVerificationException.ContractConstraintRejection
并确保它与生成的列的名称完全匹配。您可以使用属性。
稍微小心地测试它,但是当你正确地执行它然后执行添加迁移时,新的迁移应该是空的。