Reputation: 351
I have a database table named Bookmarks and I changed the name of the primary key in the table. The primary was initially id but I changed it to BookmarkId and I can't seem to run queries after updating the PK name.I am not able to insert data into the table.
Below is the snippet of my error:
Server Error in '/' Application. Invalid column name 'Id'. Invalid column name 'Id'. 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 'Id'. Invalid column name 'Id'. Source Error:
Line 40: {
Line 41: db.Bookmarks.Add(bookmark);
Line 42: db.SaveChanges();
Line 43: return RedirectToAction("Index");
Line 44: }
The error occurs in Line 42, and it seems that the data cannot be inserted into the database.
This is how I defined the database:
CREATE TABLE [dbo].[Bookmarks] (
[BookmarkId] INT IDENTITY (1, 1) NOT NULL,
[carparkId] INT NULL,
[date] DATETIME NULL,
[username] NVARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([BookmarkId] ASC),
FOREIGN KEY ([carparkId]) REFERENCES [dbo].[Carparks] ([id])
);
The PK is set to auto incremented.
This is how I implement the code in the contro
public ActionResult Create([Bind(Include = "carparkId,date,username")] Bookmark bookmark)
{
if (ModelState.IsValid)
{
db.Bookmarks.Add(bookmark);
db.SaveChanges();
return RedirectToAction("Index");
}
Anybody know how to resolve this error ?
Upvotes: 0
Views: 911
Reputation: 1928
if using EDMX
then delete your table from edmx diagram
then Add again your table to edmx
Save, Build and done :)
Upvotes: 1
Reputation: 506
If you are using Entity Framework, then you need to update your .edmx file. Open your .edmx file, right click and update model from database (if you took the database first approach).
Upvotes: 0