我开发了一个代码优先的应用程序,我遇到了很多问题。我有一个已定义的类,用于绘制表格' PatientChart' 它看起来像:
public class PatientChart
{
[Key]
public int PatientChartId { get; set; }
[MaxLength(2048)]
public string Allergies { get; set; }
[MaxLength(2048)]
public string PrimaryAilments { get; set; }
[MaxLength(2048)]
public string Sensitivities { get; set; }
[MaxLength(2048)]
public string Medications { get; set; }
[MaxLength(512)]
public string ReferredBy { get; set; }
public string ChartNotes { get; set; }
[MaxLength(2048)]
public string MedicalHistory { get; set; }
[MaxLength(2048)]
public string SocialHistory { get; set; }
[MaxLength(128)]
public string PatientInfo_Id1 { get; set; }
public virtual AspNetUser PatientInfo { get; set; }
}
我在映射ForeignKey列(PatientInfo_Id)时遇到了很多困难。出于某种原因,EntityFramework继续将它映射到像PatientInfo_Id1,Id2等奇怪的东西。所以我愚蠢地从Sql Management Studio中删除了整个表。
对于我的生活,我无法获得update-database来重新生成表PatientChart。我也清除了__MigrationHistory表!做不好的决定。
我应该删除整个数据库并重新开始吗?
这是我的DbContext:
public partial class HMSModel : DbContext
{
public HMSModel()
: base("name=HMSModel")
{
}
public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUserRole> AspNetUserRoles { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
public virtual DbSet<ActiveSession> ActiveSessions { get; set; }
public virtual DbSet<PatientChart> PatientCharts { get; set; }
public virtual DbSet<PatientCert> PatientCert { get; set; }
public virtual DbSet<PatientImages> PatientImages { get; set; }
public virtual DbSet<Appointment> Appointment { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.AspNetUserClaims)
.WithRequired(e => e.AspNetUser)
.HasForeignKey(e => e.UserId);
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.AspNetUserLogins)
.WithRequired(e => e.AspNetUser)
.HasForeignKey(e => e.UserId);
modelBuilder.Entity<AspNetUser>()
.HasMany(e => e.AspNetUserRoles)
.WithRequired(e => e.AspNetUser)
.HasForeignKey(e => e.UserId);
}
}
我试过了: Reset Entity-Framework Migrations Entity Framework Code First - two Foreign Keys from same table 加上一百万个其他没有运气的链接。我希望有人聪明,可以帮助我。
该Chart类应该以1:1的比例绑定到AspNetUser
public partial class AspNetUser
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetUser()
{
AspNetUserClaims = new HashSet<AspNetUserClaim>();
AspNetUserLogins = new HashSet<AspNetUserLogin>();
AspNetUserRoles = new HashSet<AspNetUserRole>();
}
public string Id { get; set; }
[StringLength(256)]
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTime? LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
[StringLength(256)]
public string FirstName { get; set; }
[StringLength(256)]
public string LastName { get; set; }
public string FullName { get { return string.Format("{0} {1}", FirstName, LastName); } }
public string DOB { get; set; }
public string Address1 { get; set; }
[StringLength(256)]
public string City { get; set; }
[StringLength(2)]
public string State { get; set; }
[StringLength(25)]
public string Zip { get; set; }
public string Phone { get; set; }
[StringLength(25)]
public string Gender { get; set; }
public bool? ReceiveSMS { get; set; }
[Required]
[StringLength(256)]
public string UserName { get; set; }
public DateTime? LastLogin { get; set; }
public DateTime? RegisterDate { get; set; }
public bool? Active { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserClaim> AspNetUserClaims { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserLogin> AspNetUserLogins { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserRole> AspNetUserRoles { get; set; }
public virtual AspNetUserRole AspNetUserRole { get; set; }
public virtual PatientCert PatientCert { get; set; }
//public virtual PatientChart PatientChart { get; set; }
public virtual ICollection<PatientImages> PatientImages { get; set; }
// public virtual PatientChart PatientChart { get; set; }
public virtual ICollection<Appointment> Appointment { get; set; }
}
对于大多数大型应用程序,我总是首先使用数据库,这次我去了Code First。我觉得Code First是一种痛苦,充满了问题,但也许只是因为我缺乏知识深度。
您可以在此处看到该表格遗失 [