Unity + EntityFramework + PRISM -“解决依赖失败”
本文关键字:依赖 失败 解决 EntityFramework PRISM Unity | 更新日期: 2023-09-27 18:01:28
我新的Unity/EF世界,我得到一个错误,当我执行我的测试新项目。
我有一个Prism首先引导:
class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return new Shell();
}
protected override void InitializeShell()
{
base.InitializeShell();
Application.Current.MainWindow = (Window)this.Shell;
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
this.Container
.RegisterType<IDataContextAsync, SistemaContext>(new ContainerControlledLifetimeManager())
.RegisterType<IUnitOfWorkAsync, UnitOfWork>(new ContainerControlledLifetimeManager())
.RegisterType<IRepositoryAsync<Cliente>, Repository<Cliente>>()
.RegisterType<IClienteService, ClienteService>();
}
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
moduleCatalog.AddModule(typeof(CadastroModule.CadastroModule));
}
}
和其他项目"CadastroModule"与CadastroModule:
public class CadastroModule : IModule
{
private readonly IRegionManager regionManager;
public CadastroModule(IRegionManager regionManager)
{
this.regionManager = regionManager;
}
public void Initialize()
{
this.regionManager.RegisterViewWithRegion("MainRegion", typeof(Views.CadastroCliente));
}
}
那是我的EF。
public class SistemaContext : Sistema.Common.Repository.DataContext
{
static SistemaContext()
{//I dont know what to do here!
//Database.SetInitializer<SistemaContext>(new MigrateDatabaseToLatestVersion<SistemaContext, Sistema.DataAccess.Migrations.Configuration>());
}
public SistemaContext()
: base("Name=ContactsDb")
{
}
public DbSet<Cliente> Cliente { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Sistema.DataAccess.Mapping.ClienteConfig());
modelBuilder.Configurations.Add(new Sistema.DataAccess.Mapping.EnderecoConfig());
}
}
我需要"MigrateDatabaseToLatestVersion"用于自动迁移。
视图:public partial class CadastroCliente : UserControl
{
private readonly IClienteService _clienteService;
private readonly IUnitOfWork _unitOfWork;
public CadastroCliente()
{
InitializeComponent();
}
public CadastroCliente(IClienteService clienteService, IUnitOfWork unitOfWork)
{
_clienteService = clienteService;
_unitOfWork = unitOfWork;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dgv.DataContext = _clienteService.ClienteOrdenadoNome();
}
}
UPDATE 1如果我取出IUnitOfWork它运行!:
public partial class CadastroCliente : UserControl
{
private readonly IClienteService _clienteService;
private readonly IUnitOfWork _unitOfWork;
public CadastroCliente()
{
InitializeComponent();
}
public CadastroCliente(IClienteService clienteService)
{
_clienteService = clienteService;
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
dgv.DataContext = _clienteService.ClienteOrdenadoNome();
}
}
我使用位于这里的工作存储库的通用单元:Link
错误如下:
异常发生时,容器为:解决CadastroModule.Views.CadastroCliente(没有)解析构造函数cadastromodule . views . cadastroclient (systemma . service .)的参数"unitOfWork"。system . common . repository . uow . iunitofwork解决Sistema.Common.Repository.UoW.IUnitOfWork(没有)
我检测到UnitOfWork何时获取上下文
公共类UnitOfWork: UnitOfWorkAsyncpublic UnitOfWork(IDataContextAsync dataContext) {_dataContext = dataContext;}
dataContext返回一个内部异常异常:
在创建模型时不能使用上下文。如果上下文在OnModelCreating方法中使用,或者多个线程同时访问同一个上下文实例,则可能引发此异常。注意DbContext和相关类的实例成员不能保证是线程安全的
我做错了什么?
问题在许多天后解决…在我使用的RepositoryFramework内部,EntityClasses需要从一个名为Entity的类继承:
public abstract class Entity : IObjectState
{
[NotMapped]
public ObjectState ObjectState { get; set; }
}
子类"Complex"(实体框架映射complextype配置<<em>*>)没有被继承。错误不在于UNIT/PRISM,而在于我使用的存储库本身。为了解决这个问题,我创建了一个新项目,不使用模块化PRISM,以便更简单地找到错误发生的地方。
所以…我的问题解决了,把":Entity"在我的子类:
public class Endereco : Entity //HERE
{
public string Rua { get; set; }
public int Numero { get; set; }
}