Updating an MVC application Model along with data-source
Firstly, new to Asp.Net MVC and ADO.NET entity data model.
While practicing an example, I created a table but forgot to set "Identity specification" to true for PK.
Created a model for MVC Application using entity data Model and worked fine.
Later on I've have set that "Identity specification" to true for that table in the DB.
When I try to insert a record an exception is raised and record does not get inserted.
{"Cannot insert explicit value for identity column in table 'Contacts' when IDENTITY_INSERT is set to OFF."}
Here is the digner created class in the model, which should have changed as per schema changes in DB
public static Contact CreateContact(int id, string firstName, string lastName, string phone, string email)
{
Contact contact = new Contact();
contact.Id = id;
//
return contact;
开发者_如何学编程 }
There is no need for "Id" variable in the above Method Signature as but it is still auto generating that.
How can we make our model to refresh itself or manually, if the database schema is updated.
NOTE: using C#, ASP.NET MVC
Thanks
Configure your DB schema correctly, then right-click your model and choose "Update Model from Database." This will correct the SSDL in your EDMX, which tells the EF that the id
is store generated.
It will not, however, remove the id
argument from the CreateContact
method. The EF's code generator puts all non-nullable properties in the signature to this method. But once you have updated the SSDL, you should no longer get the exception when you save; if you want to use this method (you don't have to), you can just pass a 0.
精彩评论