UnintentionalCodeFirstException | Entity Framework Unit Testing with Effort.Ef6 using Database First

c# effort entity-framework entity-framework-6 unit-testing

Question

The circumstance

I have constructed my DbContext based on entity framework 6 and want to enable unit testing.DbContext and my models using the database-first strategy, and I now have a designer file ending in zzz-15 zzz.

the issue

My computer generatedDbContext executes the following to override the DbContext.OnModelCreating method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    throw new UnintentionalCodeFirstException();
}

I'm attempting to launch a fresh instance of myDbContext Using data from this piece, I created a dummy data access service. My programme doesn't encounterDatabase.SetInitializer; as the blog post's author drew attention to. My function Object() { [native code] } resembles his perfectly. MyDbContext initialization appears as follows:

public DatabaseEntities(DbConnection connection) 
     : base(connection, true) { }

which, when reaching the previously stated override, causes the following exception.OnModelCreating

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: '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'

Many queries on stackoverflow centre on unit testing with effort and utilising the database-first approach, but nobody else appears to be experiencing the same issues that I am.

what I've previously attempted

  • considerable period of time spent looking for a solution online.
  • Delete the overwritten commentOnModelCreating() .
  • I've previously made unsuccessful attempts to apply this remedy.
  • insult myDbContext This is obviously not an option, with Moq.

the query

How can I make an in-memory database and utilise it (hard work)?


Additional details

As soon as I remove theOnModelCreating() Upon accessing the DataContext for the first time, an exception will be raised. E.g.:

DbConnection effortConnection = Effort.DbConnectionFactory.CreatePersistent("MockedDatabaseEntities");

using (DatabaseEntities dataContext = new DatabaseEntities(effortConnection))
{
    dataContext.Students.Count(); // Exception throws here
}

System.Data.Entity.ModelConfiguration.ModelValidationException: 'One or more validation errors were detected during model generation: MyApplication.Shared.Model.KeyValuePair: : EntityType 'KeyValuePair' has no key defined. Define the key for this EntityType. KeyValuePairs: EntityType: EntitySet 'KeyValuePairs' is based on type 'KeyValuePair' that has no keys defined.'

1
1
9/12/2017 9:44:13 AM

Accepted Answer

I had to give the key for my KeyValuePair dataset, which was an issue.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<KeyValuePair>().HasKey(x => x.Key);
    base.OnModelCreating(modelBuilder, null);
}

I appreciate you, The author is Robert J. Engdahl..

0
9/12/2017 1:33:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow