Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向现有数据库中添加表代码第一个MVC5EF6_C#_Asp.net Mvc_Database_Entity Framework_Visual Studio 2013 - Fatal编程技术网

C# 向现有数据库中添加表代码第一个MVC5EF6

C# 向现有数据库中添加表代码第一个MVC5EF6,c#,asp.net-mvc,database,entity-framework,visual-studio-2013,C#,Asp.net Mvc,Database,Entity Framework,Visual Studio 2013,我正在创建一个VS2013 MVC5 Web应用程序。到目前为止,我已经通过迁移定制了默认的AspNetUser表。我现在正试图向现有数据库添加一个新表。 我已经创建了一个patient类: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; using System.ComponentMo

我正在创建一个VS2013 MVC5 Web应用程序。到目前为止,我已经通过迁移定制了默认的AspNetUser表。我现在正试图向现有数据库添加一个新表。 我已经创建了一个patient类:

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");

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

这似乎是因为您的
DbSet
访问器位于嵌套在ApplicationDbContext类中的DbContext(PatientDbContext)中

DbSet
访问器放入主ApplicationDB上下文中,并删除PatientDB上下文

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<Patient> Patients { get; set; }
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
公共数据库集患者{get;set;}
}

为什么要在IdentityDbContext中嵌套常规DBContext?
namespace TestModel.Migrations
{
using System;
using System.Data.Entity.Migrations;

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

    public override void Down()
    {
    }
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public DbSet<Patient> Patients { get; set; }
}