首先将表添加到现有数据库代码MVC 5 EF6

时间:2015-07-24 12:41:21

标签: c# asp.net-mvc database entity-framework visual-studio-2013

我正在创建一个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; }

    }
}

但是当我进入&#34; Add-Migrations Patient&#34;创建以下迁移类时没有患者详细信息

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

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

    public override void Down()
    {
    }
}
}

我知道这是一个非常基本的问题,但作为一个初学者,我不确定我哪里出错了。 任何建议将不胜感激 感谢

1 个答案:

答案 0 :(得分:1)

看起来好像这是因为你的DbSet<Patients>访问者在DbContext(PatientDbContext)中,它嵌套在ApplicationDbContext类中。

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; }
}
相关问题