Dependency Injection in Entity Classes with Code First

I am trying to figure out a way to handle dependency injection in the root code of an entity.

I came across an article that uses an ObjectStateManager, although I don't think it is available in the code in the first place, so I'm looking for something similar where I could inject an (injection property) object into a newly created / received object, or maybe , another way?

I use Autofac as a di container

+4
source share
1 answer

You can do di like this

public class YourContext : DbContext { protected ObjectContext ObjectContext { get { return ((IObjectContextAdapter)this).ObjectContext; } } public YourContext(string connectionString):base(connectionString) { ObjectContext.ObjectMaterialized += ObjectMaterialized; } void ObjectMaterialized(object sender, ObjectMaterializedEventArgs e) { // do property injection here using e.Entity } } 
+4
source

All Articles