In my web project I am trying to use Entity Framework 6 model first approach. Model is generated from existing database in separate project. For model generation connection string is saved in app.config of data access project. In main web project (ASP.NET Core MVC) I am trying to inject context creation in Startup.cs class.
Below is a code for context partial class. Partial class is used because context is auto generated from template.
[DbConfigurationType(typeof(EntityFrameworkConfig))]
public partial class MyEntities
{
public MyEntities(String name) : base(name)
{
}
}
For using entity framework configuration from code instead app.config that cannot be used with asp.net core projects I have config class inherited from System.Data.Entity.DbConfiguration
public class EntityFrameworkConfig : DbConfiguration
{
public EntityFrameworkConfig()
{
this.SetDefaultConnectionFactory(new SqlConnectionFactory());
this.SetProviderServices(SqlProviderServices.ProviderInvariantName, SqlProviderServices.Instance);
}
}
In config.json in web project i have connection string:
{
"MailSettings": {
"ToAddress": "foo@bar.com"
},
"Connection": {
"ConnectionString": "data source=SERVER;initial catalog=DB;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework;"
}
}
In Startup.cs :
public void ConfigureServices(IServiceCollection services)
{
...
String connStr = _config["Connection:ConnectionString"];
services.AddScoped((_) => new MyEntities(connStr));
...
}
I am experiencing UnintentionalCodeFirstException thrown from OnModelCreating event of auto generated context class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
Is this proper way to use Entity framework 6 model first approach with asp.net Core MVC project and what is the reason for this exception ?