How do I use EF to automatically create a database and repository in ASP.NET MVC?

11,603

Solution 1

There are plenty of good tutorials out there about how to do this. This one, for example:

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

To answer some of your questions, yes, the name CommentId is correct; every EF class that you want stored in the database must have either a field called Id or a field called MyClassId (where "MyClass" is the name of your class). I've found that the latter makes your life easier, especially when doing joins.

Unless you have some relationships that EF can't figure out automatically, you don't have to specify the relationships yourself: EF will automatically detect the correct relationship for you. I don't see anything in your code that EF can't handle automatically.

One thing you will have to do is make the List<Post> and List<Comment> fields virtual; that way EF can supply database-backed relationships.

Good luck.

Solution 2

I enjoyed the Building an MVC 3 App with Code First and Entity Framework 4.1 tutorial. Includes a video that I found very easy to follow.

Share:
11,603
BeginerDummy
Author by

BeginerDummy

Updated on June 05, 2022

Comments

  • BeginerDummy
    BeginerDummy almost 2 years

    I have started to learn ASP.NET MVC, and at this time of studying I wanna create simple blog site. I have decided to use ASP.NET MVC and ORM Entity Framework. Probably you have some useful links about this theme? I tried to start from creating Model code first. i have 3 classes Post, User(User can be admin), Comments.

    Please I need help to make the relations between the database models. I have code like this right now:

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public List<Comment> Comments { get; set; }
        public DateTime PublishDate { get; set; }
    }
    
    public class User
    {
        public readonly bool IsAdmin { get; set; }
        public string FirstName { get; set; }
        public string SecondName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public DateTime DateOfBirthday { get; set; }
        public string Country { get; set; }
        public string City { get; set; }
        public List<Post> Posts { get; set; }
        public List<Comment> Comments { get; set; }
    }
    
    public class Comment
    {
        public int CommentId { get; set; }
        public string UserName { get; set; }
        public string Content { get; set; }
        public DateTime PublishDate { get; set; }
    }
    

    These are my classes to create database tables, but I'm not sure how make relations like many-to-one. Is it correct to make List of Comments for Post or just write int CommentID?? I have never use database very deep, just saw a few lessons. Can somebody to advise how make repository or correct my Model code? Thank you very much!