Question

Im new on Unity/EF world and im getting a Error when i execute my test new project.

I have a Prism bootstrap first:

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));
    }
}

And other project "CadastroModule" with the 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));
        }
    }

Thats my EF Context:

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());
    }
}

I need the "MigrateDatabaseToLatestVersion" for automatic migrations.

The View:

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 IF i take out the IUnitOfWork it runs!:

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();
    }
}

Im using a Generic unit of Work repository located here: Link

And the Error is this:

At the time of the exception, the container was: Resolving CadastroModule.Views.CadastroCliente,(none) Resolving parameter \"unitOfWork\" of constructor CadastroModule.Views.CadastroCliente(Sistema.Service.IClienteService clienteService, Sistema.Common.Repository.UoW.IUnitOfWork unitOfWork) Resolving Sistema.Common.Repository.UoW.IUnitOfWork,(none)

I detected when the UnitOfWork is taking the context

public class UnitOfWork : UnitOfWorkAsync public UnitOfWork(IDataContextAsync dataContext) { _dataContext = dataContext; }

The dataContext return a internal exception exception:

The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe

What im doing wrong?

Was it helpful?

Solution

Problem resolved after many days... Internaly of the RepositoryFramework that im using, the EntityClasses need to inherit from a class named Entity:

public abstract class Entity : IObjectState
{
    [NotMapped]
    public ObjectState ObjectState { get; set; }
}

A child "Complex"(Entity framework Mapping ComplexTypeConfiguration<*>) class, was not inherited. The error was not the UNIT/PRISM, was the repository itself that im using. To resolve that i created a new project using no Modularity PRISM to be more simple to find where the error is comming.

So... my problem was resolved by putting the ": Entity" on my Child class:

public class Endereco : Entity //HERE
{
    public string Rua { get; set; }
    public int Numero { get; set; }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top