Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hello,

I am using ASP MVC ENTITY FRAMEWORK CODE FIRST. I have a table called "Voiture" which contains 4 fields. I am trying now to add a new field UserID by writing this in my Class Voiture.cs :
C#
[Required]
public String UserId { get; set; }


but I got always this Error :
<pre lang="c#">Invalid column name 'UserId'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'UserId'.


What I have tried:

in order to show only the data related of the logged User, I wrote this in my Controller :
C#
return View(db.Voitures.Where(a => a.UserId == HttpContext.User.Identity.Name).ToList());
Posted
Updated 24-Aug-17 1:32am

You added it to the class, but you don't appear to have a matching column in your database. As a result, SQL cannot handle any requests you make to that column.
Add the column to the DB...
 
Share this answer
 
Go through this : Adding a New Field to the Movie Model and Table | Microsoft Docs[^]

You will get the idea about what is happening and why it is happening.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900