[英]How to Inject EF DbContext in Wpf using caliburn micro
I try to build WPF application to interact with database through Repositories above Entity framework, and I use caliburn micro as MVVM framework我尝试构建 WPF 应用程序通过 Entity 框架之上的 Repositories 与数据库交互,我使用caliburn micro作为 MVVM 框架
the problem is when I try to inject Repertories in ViewModels Through Simple Container it does not instantiate My DbContext问题是当我尝试通过简单容器在 ViewModels 中注入 Repertories 时,它不会实例化 My DbContext
public class UserRepo : IUserRepo
{
private AppDb _ctx;
public UserRepo(AppDb ctx)
{
_ctx = ctx;
}
}
public class AppDb : DbContext
{
public AppDb(DbContextOptions options) : base(options)
{
}
public DbSet<User> Users { get; set; }
}
class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
private AppDb _db;
public Bootstrapper()
{
Initialize();
var options = new DbContextOptionsBuilder<AppDb>()
.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=XRaySystem;Integrated Security=True;")
.Options;
_db = new AppDb(options);
}
protected override void Configure()
{
_container.Instance(_container);
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>();
//register the DataContext
// i don't know how to add it
_container.RegisterInstance(typeof(AppDb), null, _db); // <<<<<<<<<< how to add this correctly
//Register Reporisotries
_container
.PerRequest<IUserRepo, UserRepo>();
//Register ViewModels
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<DashBoardViewModel>();
//base.OnStartup(sender, e);
}
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
class DoctorViewModel : Screen
{
private readonly IUserRepo _userRepo;
public DoctorViewModel(IUserRepo userRepo)
{
_userRepo = userRepo;
}
}
UserRepo
is instantiated but with null AppDb
UserRepo
已实例化,但使用 null AppDb
How to configure Simple Container to Add AppDb
to UserRepo
?如何配置 Simple Container 以将
AppDb
添加到UserRepo
?
I have reproduced the same problem in on GitHub我在 GitHub 上重现了同样的问题
After some debugging, I found that Configure
Method run first then the constructor call !!经过一番调试,我发现先运行
Configure
方法,然后调用构造函数! So the instantiation is happened after the Configuration already done with null所以实例化是在 null 完成配置之后发生的
I solve it by adding the instantiation of _db in Configure
method itself我通过在
Configure
方法本身中添加 _db 的实例化来解决它
class Bootstrapper : BootstrapperBase
{
private SimpleContainer _container = new SimpleContainer();
private AppDb _db;
public Bootstrapper()
{
Initialize();
}
protected override void Configure()
{
var options = new DbContextOptionsBuilder<AppDb>()
.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=XRaySystem;Integrated Security=True;")
.Options;
_db = new AppDb(options); //<<<< solve the problem
_container.Instance(_container);
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>();
//register the DataContext
_container.Instance(_db);
// _container.RegisterInstance(typeof(AppDb), null, _db);
//Register Reporisotries
_container
.PerRequest<IUserRepo, UserRepo>();
//Register ViewModels
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<DashBoardViewModel>();
//base.OnStartup(sender, e);
}
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.