Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello everyone have a good day i am making a website but i looked on the internet but i can't fix it in any way

What I have tried:

entity : 
public int Id { get; set; }       
public string BlogKategoriBaslik { get; set; }
public int DurumId { get; set; }
public  Durum Durum { get; set; }
 public void BlogKategoriEkle(BlogKategori entity, bool tracking = false)
            entity.BlogKategoriBaslik = entity.BlogKategoriBaslik;
           entity.DurumId = entity.DurumId;
error : 
SqlException: Cannot insert explicit value for identity column in table 'BlogKategoris' when IDENTITY_INSERT is set to OFF.
DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
Updated 9-May-23 2:33am

The error message is clear: you cannot INSERT values to an IDENTITY field of a database unless IDENTITY_INSERT is off - and that's deliberate, because the DB engine manages the ID values and your INSERTs could easily cause duplications which would trash the DB integrity.
You probably need to add the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute to the DurumId field to prevent it being included in INSERTs- but we have no access to your DB so we can't be sure.
Share this answer
The error means you're trying to set an Id value for the record. You cannot do that when the Id is generated by the database.

This is usually caused by a design flaw in your code. You're setting, or allowing to set, the Id value of the record you're trying to insert.

You should not be using the database entity classes in your view. What you should be doing is creating a separate set of classes to be used by the view that contains the EDITABLE data for the record.

The controller will get a record from the database to edit (if necessary), create an instance of the view model class and copy the data from the entity class to the view class and send that view class to the view.

When the data is submitted back to the controller, you'll be getting back the view class. The controller can then add that data to the database or go get the record that was being editing, making the changes to that entity record using the data in the view class that was returned, then save the changes to the entity record.
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