Mark Berube
Mark Berube

Reputation: 204

MVC 5 unable to create controller with Linq to SQL class

I'm trying to create a controller to use a Linq to SQL class in VS2013. I created a SQL database and added a table called Alias with a fields and a primary key. I then proceeded to add a Linq to SQL class into my Models folder called Alias. I added the Alias table to the OR designer, saved the class and rebuilt the solution.

I clicked my Controllers folder and hit add controller MVC 5 with views using EF. This is where things got messed up, the data context for my Alias Linq to SQL class is missing. What am I doing wrong here? My only option with the Alias model class is to create a new data context class. If it's something to do with this template, what would be a default looking controller template for this Alias class?

Picture of the add controller dialog: enter image description here

web.config connection string:

 <connectionStrings>
    <add name="URLDBConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\URLDB.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="AurlContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=AurlContext-20140221123357; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|AurlContext-20140221123357.mdf"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Upvotes: 1

Views: 1388

Answers (1)

Dismissile
Dismissile

Reputation: 33071

Entity Framework (EF) is not the same thing as LINQ to SQL. You are trying to use scaffolding designed for Entity Framework, but the only thing you have is a LINQ to SQL context. This will not work.

If you are using the LINQ to SQL designer, you should be able to transition to Database First styled Entity Framework without too many headaches. To add a Database First context to your project follow these steps:

  1. Right click your project and click Add New Item
  2. Go to the Data section
  3. Choose ADO.NET Entity Data Model
  4. Give it a name like you did with your .dbml, but EF uses .edmx
  5. Choose generate from Database
  6. Create a new connection
  7. Choose the tables you want to include

That said, I highly recommend you go with an EF "Code First" approach.

Upvotes: 5

Related Questions