Is there any way to mark an entity as read-only and not specify any key for it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a couple of things that you can do to enforce read-only in Code First. The first is to use
AsNoTracking()when you query.This tells Code First to not track changes to these entities, so when you call
SaveChanges()no changes made to these objects will be persisted.The seccond thing you can do is set the state to
Unchangedbefore callingSaveChanges().This tells Code First to ignore any changes that have been made to that entity.
As far as not having a key, all entities must have a key. This may not necessarily map to a primary key in the database, but it “must uniquely identify an entity type instance within an entity set”.