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

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

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

相关文章:

c# - 将 FieldInfo 转换为 C# 中的列表

c# - ASP.NET Core - 当前上下文中不存在名称 'JsonRequestBehavior'

c# - 表情型成员奇怪的案例

c# - 将触发器值传递给 Wpf 应用程序

database - 在 Satchmo/Django 项目上使用 Git 的正确方法

c# - 测试TCP连接的Java等效方法

sql - 我可以在 ASP.net MVC 中使用纯 SQL 吗?

asp.net-mvc - 如果在 asp.net MVC 3 + EntityFramework 中列名为 "Model",如何保存数据

c# - MVC 4 中的等待屏幕

database - 如何使用awk查找列中的最大值