How to use internal dbcontext? I wonder how I can hide the DbContext object so that other libraries of my project do not have direct access.
I placed my DbContext as internal in the library and, apparently, should work, however, when the application starts, the following message appears:
The target context "Context" is not constructive. Add a default constructor or specify an IDbContextFactory implementation
Can someone help me?
My data layer implementation:
[DbConfigurationType(typeof (ConfigContext))]
internal class Context : DbContext
{
internal Context()
: base(ConfigDataBase.GetSqlServerString(ConfigZnfce.Instance.SqlServerInstance))
{
}
}
public class ConfigContext: DbConfiguration
{
public ConfigContext()
{
SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.LocalDbConnectionFactory("v11.0"));
SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetDatabaseInitializer(new CreateDatabaseIfNotExists<Context>());
SetDatabaseInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
}
}
I want all other libraries to have to go through a unit of work and storage in order to perform any database operation
[SOLVED]
I left both the internal context and the constructor context and public, as in the code below:
[DbConfigurationType(typeof (ConfigContext))]
internal class Context : DbContext
{
public Context()
: base(ConfigDataBase.GetSqlServerString(ConfigZnfce.Instance.SqlServerInstance))
{
}
}