[英]Adding a table to an existing Database code first MVC 5 EF6
I am creating a VS 2013 MVC5 Web application. 我正在创建VS 2013 MVC5 Web应用程序。 So far I have customised the default AspNetUser table by Migration.
到目前为止,我已经通过Migration自定义了默认的AspNetUser表。 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 在身份模型中,我添加了PatientDbContext类
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 但是,当我输入“ Add-Migrations Patient”时,将创建以下迁移类,而没有“ Patient”详细信息
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
任何建议将不胜感激谢谢
It looks as if this is because your DbSet<Patients>
accessor is inside a DbContext (PatientDbContext) which is nested inside the ApplicationDbContext class. 似乎这是因为您的
DbSet<Patients>
访问器位于嵌套在ApplicationDbContext类内部的DbContext(PatientDbContext)中。
Put the DbSet<Patients>
accessor in the main ApplicationDbContext and remove the PatientDbContext. 将
DbSet<Patients>
访问器放在主ApplicationDbContext中,然后删除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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.