I am new to Autofac and I try to set a public property in the class every time it is initialized. Here is the script. I have many classes that inherit from "Entity" that follow this basic format ...
public class Person : Entity
{
public virtual DataContext ServiceContext { get; set; }
public string FirstName {get; set;}
...
}
These classes are usually created using the LINQ query as follows:
var context = SomeContext(connection);
var people = context.Query<Person>().Where(item => item.FirstName == "Joe").ToList();
What I'm trying to do is pass the "context" object to the ServiceContext property of the Person class every time I create it. In this example, each Person in the list of people will have this property.
Ideally, I would pass the DataContext through the "Entity" constructor, but the problem is that I do not have access to the Entity or Linq provider, since they belong to a third party. So my question is using Autofac, how do I add a “context” to the “ServiceContext” property for each class that comes from “Entity”? It seems that the OnActivating event is close to what I need, but I cannot get it to work.
source
share