[英]Adding a table to an existing Database code first MVC 5 EF6
我正在创建VS 2013 MVC5 Web应用程序。 到目前为止,我已经通过Migration自定义了默认的AspNetUser表。 我现在正在尝试向现有数据库添加新表。 我创建了一个耐心的课程:
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; }
}
}
和一个患者配置类:
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");
}
}
}
在身份模型中,我添加了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; }
}
}
但是,当我输入“ 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()
{
}
}
}
我知道这是一个非常基本的问题,但是作为初学者,我不确定我要去哪里。 任何建议将不胜感激谢谢
似乎这是因为您的DbSet<Patients>
访问器位于嵌套在ApplicationDbContext类内部的DbContext(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.