0

I've developed a code first application that I've been having a lot of problems with. I have a class defined that mapped out the table 'PatientChart' it looks like it:

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; }
}

I was having a lot of difficulty mapping a ForeignKey column (PatientInfo_Id). For some reason EntityFramework kept on mapping it to strange things like PatientInfo_Id1, Id2 ect. So I stupidly deleted the entire table from Sql Management Studio.

For the life of me I cannot get update-database to re-generate the table PatientChart. I've also cleared out that __MigrationHistory table! Poor decisions through and through.

Should I just delete the entire database and start over again?

Here is my 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);



        }
    }

I've tried: Reset Entity-Framework Migrations Entity Framework Code First - two Foreign Keys from same table plus a million other links with no luck. I'm hoping someone clever here can help me.

That Chart class is supposed to be tied 1:1 to 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; }
    }

I always use Database first for most large applications and this time I went Code First. I feel like Code First is a pain and riddled with problems but perhaps it's just my lack of depth of knowledge.

You can see the table is missing here [You can see the table is missing here[1]

jon.r
  • 896
  • 8
  • 16
  • You aren't alone... So many code-first, and especially issues with code first & EF Core. I never saw how code first was ever practical beyond whipping up a very basic and disposable prototype. Too much of it seems like trial & error or worse, ends up in far from optimal schema definitions. That rumbling you hear is every deceased DBA rolling in their grave. :) – Steve Py Jun 21 '17 at 05:13
  • On topic, a couple of things stand out. You have several mappings defined for ASPNetuser for related items. I'd probably suggest moving these to an actual EntityTypeConfiguration instance which can hold the PK key mapping and relations for child collections. I'd also recommend using a meaningless key (I.e. sequence Int) for the PK on this table and related FKs and set up the current "Id" as a uniquely constrained indexed value. – Steve Py Jun 21 '17 at 05:19

0 Answers0