1
Vote

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled by user code

description

When I attempt to run a unit test using Effort in database-first mode, this is the exception I'm getting:
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException was unhandled by user code
  HResult=-2146233079
  Message=The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
  Source=Cssd.IT.PortalIntegration.DataAccess
  StackTrace:
       at Cssd.IT.PortalIntegration.DataAccess.HR.Dao.HRADDbContext.OnModelCreating(DbModelBuilder modelBuilder) in c:\DevProjects\IT_Portal\Source\PortalIntegration\Cssd.IT.PortalIntegration.DataAccess\HR.Dao\HRModel.Context.cs:line 50
       at System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder)
       at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
       at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
  InnerException: 
This is the line of code that throws the exception in the DbContext:
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
The above exception should not be removed in database-first mode, since it should never be called in that mode because all of the mappings are in the EDMX file. The OnModelCreating should only be called in code-first EF mode to build the mappings between the database and the source code. See https://blog.oneunicorn.com/2012/02/26/dont-use-code-first-by-mistake/

Does anyone have any suggestions? TIA.

comments