0

Before you say that this is an answered question, I checked everything I found on this subject and nothing works for me.

I am watching a tutorial about entity framework and doing a project in .net MVC. I have created the following class:

namespace ShowReel.Models
{
public class ShowReelDB : DbContext
{
    public DbSet<Tvshow> Tvshows { get; set; }
    public DbSet<EpisodeGuide> Episode { get; set; }
}
}

I am supposed to open a connection to a local database, and on the tutorial as an option to a database, it gives this DBContext class.

I get only this:

enter image description here

If I enter it manually, then it creates a new empty database, but it is supposed to create a database that has the tables Tvshows and Episode.

Can anyone tell me what's the problem? Do I need to install anything else to the Visual Studio?

I apologize if this is a stupid question but I am fairly new to .net mvc, and I am trying to learn it right now.

Here is the connectionstring:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ShowReel-20170214120508.mdf;Initial Catalog=aspnet-ShowReel-20170214120508;Integrated Security=True" providerName="System.Data.SqlClient" />

And here is what I have for the entity framework in the web.config file:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="mssqllocaldb" />
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

EDIT: When running the app, I get the following error: enter image description here

The model from where I call the database is this one:

  // GET: TVShows
    ShowReelDB _db = new ShowReelDB();
    public ActionResult TVshow()
    {
        var model = _db.Tvshows.ToList();


        return View(model);
    }

And here are the elements of the Tvshow and Episode table:

public class EpisodeGuide
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Seasons { get; set; }
    public Array EpisodesPerSeason { get; set; }
    public DateTime Date { get; set; }
}

 public class Tvshow
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public double Rating { get; set; }
    public string Image { get; set; }
    public int Year { get; set; }

    public int EpisodeGuideID { get; set; }
}

The view is the following:

@foreach (var item in Model)

{

<div class="row">
    <div class="col-md-4">
        <img src="../Content/Images/@item.Image" style="height:200px; width:300px;" />
    </div>
    <div class="col-md-8">
        <h2> @Html.DisplayFor(modelItem => item.Name)</h2>
        <p>  @Html.DisplayFor(modelItem => item.Description)</p>
        <button class="btn btn-danger btn-lg news" id="@Html.DisplayFor(modalItem => item.Id)">Track</button>
    </div>

</div>
<hr />

}

5
  • What happens if you allow it to create a new empty database, then pass that database's connection string to your DbContext and run the app? Does it create the necessary tables for you? Commented Feb 21, 2017 at 14:39
  • Where is you DBCOntext class and the connectionstring . please share those . Also where did you add migrations of this is DB First Approach ? Commented Feb 21, 2017 at 14:46
  • I put the connectionstring, and the DBContext class was already given.. at least I think that's the class you mean.. Commented Feb 21, 2017 at 14:50
  • @Spirit_Scarlet you want to view your table in sql server? Commented Feb 21, 2017 at 14:59
  • what about you migrations ? Are they included and executed . Please try running update-database command Commented Feb 21, 2017 at 15:00

1 Answer 1

1

you have to create at least a constructor and pass the name of the connectionstring or the connectionstring to base-constructor-call:

public class ShowReelDB :DbContext
{
    public ShowReelDB ():base("DefaultConnection")
    {
    }

    public DbSet<Tvshow> Tvshows { get; set; }
    public DbSet<EpisodeGuide> Episode { get; set; }
}

then you could work either with migrations to have a code-first application. just google for code-first-migrations.

or, if the database already exists, just work with it as it is. If you have any problems add a Database.SetInitializer<ShowReelDB>(null); into the constructor.

Sign up to request clarification or add additional context in comments.

8 Comments

It worker. I successfully connected the database. Thank you. I have one more problem though. When I run the app, I get this error, Type 'ShowReel.Models.Array' in conceptual side cannot be mapped to type 'System.Array' on the object side. Both the types must be abstract or both must be concrete types. Do you know what's the problem?
@Spirit_Scarlet I think it means, you cannot map a type like int[] or something.. to check your problem, show me your models you're adding to the context
I updated the question with the models, tables and the view.
@Spirit_Scarlet Since your model has to be represented in a relational way, you can only use primitive types. Array is not a primitive type, you can't use it as property-type for a column: stackoverflow.com/questions/5877358/… (Create a new table EpisodePerSeason or something)
I changed it to Int and I still get the same error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.