1

I'm working on an ASP.NET MVC application. I'm trying to use some SQL View objects (not SQL tables, not stored procedures) to fetch some data, but it isn't working at all. When I run the app to go to the index() method of on of the controllers to show the data, an error is thrown which says:

System.Data.Entity.Core.EntityCommandExecutionException was unhandled by user code HResult=-2146232004 Message=An error occurred while executing the command definition. See the inner exception for details. Source=EntityFramework

then later looking at the innerexception I've got this:

InnerException: System.Data.SqlClient.SqlException HResult=-2146232060 Message=Invalid column name 'HeardAboutID'. Source=.Net SqlClient Data Provider ErrorCode=-2146232060

This is wrong. I most certainly DO have a column named HeardAboutID in the SQL View. I've got a model class in my MVC project that has a column named HeardAboutID in it. So to say that HeardAboutID isn't there is patently false.

So this makes me wonder, does ASP.NET MVC 5 only work with SQL tables and stored procedures? Does it in fact NOT work with SQL Views?

1
  • 2
    ASP.Net MVC has nothing whatsoever to do with SQL Server. What is your code doing? Commented Jan 25, 2015 at 0:55

2 Answers 2

1

Since you said it wasn't working at all I'd like to share some things I've come across since starting with Entity Framework. Here are some things I learned about using Views as Models in EF.

  1. If you do not have a Primary Key it will try to make one for you. This may or may not be good or ruin the view of your data entirely. The two solutions I found was to either name the field you want as a PK with ID in it or use something like

    ISNULL(fieldname,999999) AS Primary_ID 
    

    Wrapped around the field name in the SQL view.

  2. If the a field name matches the table name it will change the field name. For Example your table is Named ORG, a field in the table is named ORG. EF's scaffolding will name the field ORG1.

  3. Remove the "Pluralize or singularize generated object names" check-mark when you create your entity object if you don't want it to Pluralize.

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

Comments

0

I have finally gotten it to work. I found a few problems. First, I hadn't changed the names of the foreign key columns in the ResumeSubmissions table. Second, and this was a lot harder to find, MVC pluralized the names of the SQL Views in ways I did not expect. Finally after a few hours of working at it with SQL Profiler I could see what it was looking for. Once I renamed the SQL Views to what MVC was looking for it was happy.

The answer to my question is yes MVC does work with SQL View objects.

Thank you everyone for your help.

1 Comment

This has nothing to do with MVC; you're actually talking about Entity Framework.

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.