I have an entity loaded from one DBContext with some changes made to it. If I then create another DBContext how would I load the same entity into it from the database? This needs to be generic across different entities, so I can't just lookup where the ID's are the same - some entities could have different key properties.
The reason behind the question is that I'm wanting to use the entity loaded into the second context to validate some changed properties in the first one, comparing previous values to new ones. So the second context is purely read only during the validation of the first entity.
Edit
I tried to keep this simple I think a bit more detail is needed.
Say I have this entity:
public partial class SomeEntity : IValidatableObject
{
public int Id { get; set; }
public int StatusId { get; set; }
//... Other properties
public virtual ICollection<SomeOtherEntity> relatedEntity { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext){
// validation code
}
}
What I'm trying to do is when the entity is validated the StatusId
is compared against the StatusId
currently in the database and a validation error could occur - for example say StatusId
can't be changed from 1 to 5. As
As Pawel mentioned this can be done by using the OriginalValues however I also need to be able to validate the StatusId
based on the values in the relatedEntity
. For example say the StatusId
can't be changed from 1 to 2 if certain values exist in the relatedEntity
.
So in order to do this validation I need a duplicate of the SomeEntity
object in its unmodifed form from the database so it can be compared against the object I'm trying to modify.
It's a bit messy but the idea I've come up with is:
Make
SomeEntity
a member of a new, empty, interfaceIMyValidationInterface
.public interface IMyValidationInterface { } public partial class SomeEntity : IValidatableObject, IMyValidationInterface { public int Id { get; set; } // .... }
Override the
ValidateEntity
method onMyDBContext
so that an object with the original values is passed into the validation method on the entity.public class MyDBContext : DbContext { protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { if (entityEntry.Entity is IMyValidationInterface) { var _validationContext = new MyDbContext(); /// TODO: some code here to load a duplicated version of /// the entity from the database var originalEntity; // unchanged entity is here // unmodified entity is passed as an item to the entities // Validate method items.Add("OriginalEntity", originalEntity); } return base.ValidateEntity(entityEntry, items); } } public partial class SomeEntity : IValidatableObject, IMyValidationInterface { // ..... public IEnumerable<ValidationResult> Validate(ValidationContext validationContext){ // validation code if (validationContext.Items.ContainsKey("OriginalEntity")){ var originalEntity = (SomeEntity)validationContext.Items["OriginalEntity"]; // do validation here and yield return any validation errors } } }
The bit I'm stuck on is the TODO part in the above snippet.
I'm I going at this the wrong way?