4

Please, consider the following example:

public override int SaveChanges()
{
    foreach (var auditableEntity in ChangeTracker.Entries<ISomething>())
    {
        if (auditableEntity.State == EntityState.Added)
        {
            //Create a new instance of the same entity type. 
            //I don't know which one will be. They just have the same interface.
            var newEntity = ?; //???Reflection???

            //ISomething known properties.
            newEntity.propX = "1";
            newEntity.propY = "2";

            //Invoke Add method.
            ??.Add(newEntity); 
        }
    }

    return base.SaveChanges();
}

I need to dynamically create an instance of all my ISomething entities during SaveChanges() method in order to add a new particular entry on it.

Any help will be very welcome.

Regards

6
  • You'll at least need a concrete type to create. You can't just use ISomething, as the concrete type could be anything. Once you've got access to the type, use [Activator.CreateInstance](https://msdn.microsoft.com/en-us/library/wccyzw83(v=vs.110).aspx) and cast the result to ISomething. Commented Jul 19, 2017 at 6:16
  • @Rob all ISomething will be an entity but I don't know which one because many implements this same interface. Commented Jul 19, 2017 at 6:32
  • How will EF know which table to insert the row into? Commented Jul 19, 2017 at 6:32
  • @Rob for a better understanding please consider foreach as ChangeTracker.Entries().Where(p => p.State == EntityState.Added) instead. ChangeTracker brings the current entity in item.Entity.GetType().Name for example. Commented Jul 19, 2017 at 6:45
  • In that case, you'd use auditableEntity.Entity.GetType() and construct a new object via Activator.CreateInstance() (then cast that result back to ISomething). Commented Jul 19, 2017 at 6:46

1 Answer 1

3

You can use the non generic DbContext.Set Method (Type) to get the corresponding non generic DbSet. Then you can use Create method to create a new instance of the same type and Add method to add it.

Something like this:

var entityType = auditableEntity.Entity.GetType();
var dbSet = Set(entityType);
var newEntity = (ISomething)dbSet.Create();

//ISomething known properties.
newEntity.propX = "1";
newEntity.propY = "2";

dbSet.Add(newEntity); 
Sign up to request clarification or add additional context in comments.

4 Comments

This non generic method is extremely useful.
your sample apparently works perfect but SaveChanges() method is ignoring the new ones entities created.
There is nothing special in the way the sample works, hence no reason added entities to be ignored. Btw, they should appear immediately in the same ChangeTracker used in your sample, so I would add ToList to buffer the list of the entries I'm supposed to iterate. Anyway, if the entities are ignored, there must be some code causing that. You can create a new question and provide a reproducible example.
In fact, I was cloning new entities like newEntity = existentEntity before to add them and all additions were being considered the same. My fault

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.