Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 487
BeginerDummy Avatar asked Mar 15 '12 22:03

BeginerDummy


People also ask

Does EF core create database?

1 Creating a Database with Code First in EF Core. The Code First approach enables you to define an entity model in code, create a database from the model, and then add data to the database. MySQL Connector/NET is compatible with multiple versions of Entity Framework Core.


1 Answers

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.

like image 125
Ethan Brown Avatar answered Oct 07 '22 23:10

Ethan Brown