1

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>
3
  • Are you sure your connection string is correct in the web.config? Commented Feb 21, 2014 at 18:14
  • I think you have to use Entity Framework - not sure if there is support for this in LINQ to SQL Commented Feb 21, 2014 at 18:14
  • @GazWinter I just checked the connection string, it looks like it checks out for the database, it also adds the Aurl context. I just added it to the question just in case. Commented Feb 21, 2014 at 18:17

1 Answer 1

5

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.

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

1 Comment

Thanks, I thought I was messing up something with EF. I think I'm gonna go with the EF approach instead.

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.