In ASP.NET MVC, how can I utilize EF to generate a database and repository automatically?

asp.net asp.net-mvc c# database entity-framework

Question

I've begun learning ASP.NET MVC, and at this point in my studies, I want to make a simple blog site. I've chosen to utilize ORM Entity Framework and ASP.NET MVC. Perhaps you have any helpful links on this subject? I made an effort to begin by writing Model code first. I have the classes Post, User (who may be an administrator), and Comments.

I kindly want assistance in creating the relationships between the database models. Right now, my code looks like this:

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 the classes I use to build database tables, however I'm not sure how to generate many-to-one relations. Is it proper to create a list of comments for a post or should I simply enter the comment ID? I've just seen a few classes and have never used databases extensively. Can someone give me any advice on how to create a repository or fix my Model code? I really appreciate it.

1
8
3/15/2012 10:34:40 PM

Accepted Answer

There are several excellent tutorials available that show you how to achieve this. For instance, this one:

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

In response to a few of your inquiries, the moniker CommentId is accurate; any EF class that you wish to save in the database must have either an Id field or a MyClassId field (where "MyClass" is the name of your class). The latter, in my experience, makes things simpler, particularly when performing joins.

You don't need to describe the connections yourself; EF will find out the right relationship for you unless you have any ties that it can't determine automatically. Nothing in your code that EF can't take care of automatically, in my opinion.

One thing you must do is create theList<Post> and List<Comment> fields virtual so that EF may provide relationships supported by databases.

Good fortune.

4
3/15/2012 10:24:17 PM

Popular Answer

The Using Code First with Entity Framework 4.1 to Create an MVC 3 App lesson was fun. includes a video that I found to be really simple to understand.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow