coto2
coto2

Reputation: 179

Adding a table to an existing Database code first MVC 5 EF6

I am creating a VS 2013 MVC5 Web application. So far I have customised the default AspNetUser table by Migration. I am now trying to add a new table to the existing database. I have created a patient class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace TestModel.Models
{
public class Patient
{
    public int Id { get; set; }

    public string HCN { get; set; }

    public string GP { get; set; }

    public string MedHis { get; set; }

    public string Medication { get; set; }

    public string CurrentPrescription { get; set; }

    public string PresentRX { get; set; }
 }
}

And a Patient Configuration class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration;

namespace TestModel.Models
{
public class PatientConfig : EntityTypeConfiguration<Patient>
{
    public PatientConfig()
    {
        ToTable("Patient");

        Property(x => x.Id).HasColumnName("IntId");
        HasKey(x => x.Id);

        Property(x => x.HCN).HasColumnName("strHCN");

        Property(x => x.GP).HasColumnName("strGP");

        Property(x => x.MedHis).HasColumnName("strMedHis");

        Property(x => x.Medication).HasColumnName("strMedication");

        Property(x => x.CurrentPrescription).HasColumnName("strCurrentPrescription");

        Property(x => x.PresentRX).HasColumnName("strPresentRX");

    }
 }
}

In the Identity Model I have added the PatientDbContext class

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public class PatientDbContext : DbContext
    {
        public DbSet<Patient> Patients { get; set; }

    }
}

But when I enter "Add-Migrations Patient" The following migrations class is created without the Patient details

namespace TestModel.Migrations
{
using System;
using System.Data.Entity.Migrations;

public partial class Patient3 : DbMigration
{
    public override void Up()
    {
    }

    public override void Down()
    {
    }
}
}

I understand this is a very basic problem but as a beginner I am unsure where I am going wrong. Any advice would be greatly appreciated Thanks

Upvotes: 1

Views: 861

Answers (1)

Matt
Matt

Reputation: 1678

It looks as if this is because your DbSet<Patients> accessor is inside a DbContext (PatientDbContext) which is nested inside the ApplicationDbContext class.

Put the DbSet<Patients> accessor in the main ApplicationDbContext and remove the PatientDbContext.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<Patient> Patients { get; set; }
}

Upvotes: 1

Related Questions