我想在模型加载 Entity Framework 时向模型中注入(inject)一些东西。
考虑以下用户模型。我希望 Entity Framework 在执行 context.Users.Where(somequery) 时将 SomeReference 注入(inject)到从数据库加载的每个用户中。
public class User {
public string Username {get; set;}
public User(SomeReference ref)
{
}
}
我在哪里可以注册?
最佳答案
您可以尝试在DbContext
构造函数中添加类似这样的内容
((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized +=
(sender, args) =>
{
var user = args.Entity as User;
if (user != null)
user.SomeReference = ref;
};
我想下一步是依靠某种工厂来检索变量 ref
关于c# - 使用 Entity Framework 向模型中注入(inject)一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36840810/