2

I am doing the tutorial about movies of Microsoft to learn ASP.NET MVC4. I want some more information if that is possible.

I am at the page http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model .

It says to add this class at the movie class

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
}

And then to create a new connection string

  <add name="MovieDBContext" 
       connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" 
       providerName="System.Data.SqlClient" 
  /> 

And this is to get the info for the movies.

My question however, is the following. If we have two tables at the database, one for the movies and one for the actors for example. And we wanted to get the data of both of these tables, we should create two different models right? But we would use only one database(connection string). Then, how we will be able to say that the actor model, will take data from the actor table, and the movie model to take data from the movie table, but the two to have the same connection string?

EDIT:

Suppose I already have the two database tables before I create my project. Then how I make my models to point to them?

Jim Blum
  • 2,656
  • 5
  • 28
  • 37

3 Answers3

4

When the Database is created, metaData is added and the EntityFramework knows which table to reference for each model. The Connection String is identical because there's two tables in one database, and connection strings are the Databse-Level connection information, not the translation from table data to model.

Essentially, the basics of code first are One Model = One Table. You can do things like foreign key relationships so if your Actor Model references Movies, the underlying data structures will be created to match as well.

EDIT: If your confusion is around the way to create the DBContext, you could create a single DBContext Class with both DBSet types in it and only use one connection string, e.g:

public class ApplicationDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
    public DBSet<Actor> Actors { get; set; }
}
OBR_EXO
  • 600
  • 4
  • 19
  • Thanks a lot! :) However, if a table authors, or movies already exists, and we do not want a new one to be created when we create a new model, what do we do? I mean how do we make the authors model to take data from the existing authors table? – Jim Blum Jan 22 '14 at 00:57
  • 1
    I think you're talking about Database-First Design, which isn't what the Movie Tutorial is showing you, you could look at this URL for more information on Database-First Design using Entity Framework: http://msdn.microsoft.com/en-us/data/jj206878.aspx – OBR_EXO Jan 22 '14 at 01:01
  • Thanks a lot. I ran that, but it created a model edmx. How do I use that? In addition I get a warning every time I run my application that it is possible now to harm my computer. Why is that? – Jim Blum Jan 22 '14 at 01:40
  • 1
    What is the warning that Visual Studio is giving you? Here is another example probably a bit simpler for you to use: http://bwolfson.com/article/24/building-a-mvc-4-app-with-database-first-and-entity-framework-5-1 – OBR_EXO Jan 22 '14 at 01:54
  • Thanks a lot! However, at the step 4, I cannot select the name of my model, as it is not shown! Do you know why is that and how I can fix this? – Jim Blum Jan 22 '14 at 02:04
  • 1
    From memory once you define a model in Entity Framework you have to "Build" the solution before you can reference it by adding a controller, have you built the solution before attempting step 4? – OBR_EXO Jan 22 '14 at 02:15
  • Haha... Wow! actually, no. I built it just now and it works. Out of 5 answers, you just got my best answer flag, + an upvote :) – Jim Blum Jan 22 '14 at 03:26
1

Connection string will not point to table, it points to database. The model you create will match up with the proper table. If you use EF code first approach it will even create separate tables to match your models.

Update: Simplest example I could find for Database first approach. http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx

  • Thanks a lot llya. However, if a table authors, or movies already exists, and we do not want a new one to be created when we create a new model, what do we do? I mean how do we make the authors model to take data from the existing authors table? – Jim Blum Jan 22 '14 at 00:56
1

Looks like a good tutorial. If only there was one more table to complete the picture.

When you get to the next section, you will see the author uses LINQ to access the data. If you are adding a related table, you will be able to add an .Include("Actors") statement to get the additional data.

SolidFish notes http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4 - which is the tutorial i used to get started and contains multiple tables.

The connectionstring is for the database, so you will only ever use one.

Once you start getting multiple models, you should then start using viewmodels that contain the model data and setting your view from that.

i.e. a rough example:

    public class MoviesViewModel 
    {   
    // Properties
    public Movie Movie { get; set; }
    public IEnumerable<Actors> Actors { get; set; }

    }
Community
  • 1
  • 1
JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
  • Thanks a lot! :) However, if a table authors, or movies already exists, and we do not want a new one to be created when we create a new model, what do we do? I mean how do we make the authors model to take data from the existing authors table? – Jim Blum Jan 22 '14 at 01:47