Code related info:
- Refer to entity-framework 6.13 by NuGet under Visual Studio 2013
- Use database-first mode, that is, create .edmx file with visual Studio template ADO.NET Entity Data Model -> EF Designer from database
-
My DbContext code:
public partial class MySampleEntities : DbContext { public MySampleEntities () { } public MySampleEntities (string nameOrConnectionString) : base(nameOrConnectionString) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } ... } -
One method to modify model with below sample code:
using (var dbContext = GetDbContext()) { ... dbContext.Entry(blobItem).State = EntityState.Modified; await dbContext.SaveChangesAsync(); }
Unit Test Cases info:
- Use NUnit and Moq
-
Some Unit Test code:
var mockContext = new Mock<MySampleEntities>();
-
Test passed with initial database schema, after some database table column names changed, and execute Visual Studio "Update Model from Database..." command on .edmx diagram, update all corresponding model properties, build code, and finally failed to run unit test on code "dbContext.Entry(blobItem).State = EntityState.Modified" with below exception:
The model backing the 'MySampleEntities' context has changed since the database was created. Consider using Code First Migrations to update the database (http://ift.tt/WvrniC).
Tried Solutions:
- Use Database.SetInitializer(null) in constructors of MySampleEntities
- Failed to execute NuGet Package Manager commands like "Enable-Migrations", "Update-Database" etc with below exception, seems it is for Code-First mode:
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.
My Questions:
- How to resolve such issue to make unit test case run successfully?
Aucun commentaire:
Enregistrer un commentaire