我是Unity / EF世界的新手,我在执行测试新项目时遇到错误。
我首先有一个Prism bootstrap:
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();
}
}
更新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.CadastroCliente的参数\“unitOfWork \”(Sistema.Service.IClienteService clienteService,Sistema.Common.Repository.UoW.IUnitOfWork unitOfWork) 解析Sistema.Common.Repository.UoW.IUnitOfWork,(无)
我在UnitOfWork处理上下文时检测到
public class UnitOfWork:UnitOfWorkAsync public UnitOfWork(IDataContextAsync dataContext){_ dataContext = dataContext; }
dataContext返回内部异常异常:
创建模型时无法使用上下文。 如果在OnModelCreating方法中使用上下文,或者同时由多个线程访问相同的上下文实例,则可能抛出此异常。 请注意,DbContext和相关类的实例成员不保证是线程安全的
我做错了什么?
答案 0 :(得分:0)
多天后问题解决了...... 在我使用的RepositoryFramework的内部,EntityClasses需要从名为Entity的类继承:
public abstract class Entity : IObjectState
{
[NotMapped]
public ObjectState ObjectState { get; set; }
}
未继承子类“Complex”(实体框架Mapping ComplexTypeConfiguration&lt; * &gt;)类。 错误不是UNIT / PRISM,是我正在使用的存储库本身。 为了解决这个问题,我创建了一个不使用Modularity PRISM的新项目来更简单地找到错误发生的位置。
所以......通过在我的Child类上放置“:Entity”解决了我的问题:
public class Endereco : Entity //HERE
{
public string Rua { get; set; }
public int Numero { get; set; }
}