Search code examples
asp.net-mvc-4entity-framework-4code-firstsimplemembershipef-database-first

How to make changes in Database First code to make it working with Code First


i am having trouble in changing code from database first to code first.I was implementing this blog post http://techbrij.com/facebook-wall-posts-comments-knockout-aspnet-webapi. First trouble is with database generated from code first.It is creating two User Profile tables in database.First is singular and another is Plurized.

Singular one (UserProfile table)is created while registering(Simple membership used). My Post class is something like this----

   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; }

}

} and my Post Comment class is something like this----

   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; }
}

and my UserProfile class is like this----

    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; }
}

and My DbContext is something like this---

    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();
    }
}

and my connection string are like this----

    <add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=DBWallPostByTechBrij;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />

what changes should i made so that only one UserProfile table is created in database and WallEntitiesDbContext should be able to retreive information from that table in database. Right now, If i remove------

  DbSet<UserProfile> UserProfiles {get; set;}

then i start getting error in my wallpostController. If anyone could help me in getting this working with code First then it would be great help.One more thing,I have manually entered some sample data into database so, after login it should show posts and comments from database but it's not showing and if i tried to post something, it throws exception System.Data.Infrastructure.DbUpdateException at this line----

   public override int SaveChanges()
    {
       return base.SaveChanges();
    }

in WallEntitiesDbContext class. Plzzz anyone have a look it.What changes should i make to make it working.


Solution

  • I think there is no need for two dbContext as u are implementing from the article.

    So, use only one dbcontext like this----

         public class UsersContext : DbContext
         {
         public UsersContext()
            : base("DefaultConnection")
        {
        }
    
        public DbSet<UserProfile> UserProfile { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<PostComment> PostComments { get; set; }
    

    In DbFisrt approach as in article, u may use it but in code first approach, definitly two dbcontext will create two userProfile table. Nothing more is needed to be changed.