我正在创建一个 VS 2013 MVC5 Web 应用程序。到目前为止,我已经通过迁移自定义了默认的 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; }
}
}
还有一个 Patient Configuration 类:
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”时,创建了以下没有患者详细信息的迁移类
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; }
}
关于c# - 将表添加到现有数据库代码首先 MVC 5 EF6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610811/