Asp.net mvc 4 如何更改数据库中的第一个代码以使其首先使用代码

Asp.net mvc 4 如何更改数据库中的第一个代码以使其首先使用代码,asp.net-mvc-4,entity-framework-4,code-first,simplemembership,ef-database-first,Asp.net Mvc 4,Entity Framework 4,Code First,Simplemembership,Ef Database First,我在将代码从数据库优先更改为代码优先时遇到问题。我正在实现这篇博文。 第一个问题是由代码优先生成的数据库。它在数据库中创建两个用户配置文件表。第一个是单数表,另一个是复数表 注册(使用简单成员身份)时创建单数表(UserProfile表)。 我的博士后课程是这样的-- public class Post { public Post() { this.PostComments = new HashSet<PostComment>(); }

我在将代码从数据库优先更改为代码优先时遇到问题。我正在实现这篇博文。 第一个问题是由代码优先生成的数据库。它在数据库中创建两个用户配置文件表。第一个是单数表,另一个是复数表

注册(使用简单成员身份)时创建单数表(UserProfile表)。 我的博士后课程是这样的--

   public class Post
{
    public Post()
    {
        this.PostComments = new HashSet<PostComment>();
    }
    [Key]
    public int PostId { get; set; }
    public string Message { get; set; }
    public int PostedBy { get; set; }
    public System.DateTime PostedDate { get; set; }
    public int UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual UserProfile UserProfile { get; set; }
    public virtual ICollection<PostComment> PostComments { get; set; }

}
   public class PostComment
{
    [Key]
    public int CommentId { get; set; }

    public int PostId { get; set; }
    public string Message { get; set; }
    public int CommentedBy { get; set; }
    public System.DateTime CommentedDate { get; set; }

    public virtual Post Post { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}
    public class UserProfile
{
    public UserProfile()
    {
        this.PostComments = new HashSet<PostComment>();
        this.Posts = new HashSet<Post>();
    }
    [Key]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string AvatarExt { get; set; }

    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}
    public class WallEntitiesDbContext : DbContext
{
    public WallEntitiesDbContext():base("WallEntitiesDbContext")
    {

    }
    public DbSet<PostComment> PostComments { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
    public override int SaveChanges()
    {
       return base.SaveChanges();
    }
}
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=DBWallPostByTechBrij;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
我的UserProfile类是这样的--

   public class Post
{
    public Post()
    {
        this.PostComments = new HashSet<PostComment>();
    }
    [Key]
    public int PostId { get; set; }
    public string Message { get; set; }
    public int PostedBy { get; set; }
    public System.DateTime PostedDate { get; set; }
    public int UserId { get; set; }
    [ForeignKey("UserId")]
    public virtual UserProfile UserProfile { get; set; }
    public virtual ICollection<PostComment> PostComments { get; set; }

}
   public class PostComment
{
    [Key]
    public int CommentId { get; set; }

    public int PostId { get; set; }
    public string Message { get; set; }
    public int CommentedBy { get; set; }
    public System.DateTime CommentedDate { get; set; }

    public virtual Post Post { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}
    public class UserProfile
{
    public UserProfile()
    {
        this.PostComments = new HashSet<PostComment>();
        this.Posts = new HashSet<Post>();
    }
    [Key]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string AvatarExt { get; set; }

    public virtual ICollection<PostComment> PostComments { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
}
    public class WallEntitiesDbContext : DbContext
{
    public WallEntitiesDbContext():base("WallEntitiesDbContext")
    {

    }
    public DbSet<PostComment> PostComments { get; set; }
    public DbSet<Post> Posts { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
    public override int SaveChanges()
    {
       return base.SaveChanges();
    }
}
    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=DBWallPostByTechBrij;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
在WallentiesDbContext类中。
请大家看一看。我应该做些什么更改才能使它正常工作。

我认为没有必要使用两个dbContext,因为您是从本文中实现的

所以,像这样只使用一个dbcontext----

public类用户上下文:DbContext
{
公共用户上下文()
:base(“默认连接”)
{
}
公共数据库集用户配置文件{get;set;}
公共DbSet Posts{get;set;}
公共DbSet后命令{get;set;}
在本文中的DbFirst方法中,u可以使用它,但在代码优先方法中,定义为两个dbcontext将创建两个userProfile表。
不需要再做任何更改。

我已经在前面解决了这个问题,顺便说一句,这对将来的stackoverflow用户可能会有帮助