-1

Running my test I get the following error:

System.Data.Entity.ModelConfiguration.ModelValidationException : One or more validation errors were detected during model generation:

Castle.Proxies.ApplicationUserLogin: : EntityType 'ApplicationUserLogin' has no key defined. Define the key for this EntityType. Castle.Proxies.ApplicationUserRole: : EntityType 'ApplicationUserRole' has no key defined. Define the key for this EntityType. ApplicationUserLogins: EntityType: EntitySet 'ApplicationUserLogins' is based on type 'ApplicationUserLogin' that has no keys defined. ApplicationUserRoles: EntityType: EntitySet 'ApplicationUserRoles' is based on type 'ApplicationUserRole' that has no keys defined.

I have located the test to fail on a override for SaveChanges and specifically ChangeTracker.Entries().

I found the following thread suggesting to call OnModelCreating that would solve the problem but this did not work for me.

https://stackoverflow.com/a/34786782/3850405

The error is only occurring when I test the code, running it from a Controller works fine.

Test source:

https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx#code-snippet-4

NUnit test:

[Test]
public void CreateCustomerNumber_saves_a_CustomerNumber_via_context()
{
    var mockSet = new Mock<DbSet<CustomerNumber>>();

    var mockContext = new Mock<ApplicationDbContext>();
    //mockContext.Object.TestModelCreation(new DbModelBuilder());
    mockContext.Setup(c => c.CustomerNumbers).Returns(mockSet.Object);

    var service = new CustomerNumberService(mockContext.Object);

    service.Add(new CustomerNumberViewModel(){CustomerNumber = "AAA"});

    mockSet.Verify(m => m.Add(It.IsAny<CustomerNumber>()), Times.Once());
    mockContext.Verify(m => m.SaveChanges(), Times.Once());
}

Service, works fine calling directly:

public void Add(CustomerNumberViewModel model)
{
    var customerNumber = new CustomerNumber()
    {
        CustomerNumber = model.CustomerNumber,
    };

    db.CustomerNumbers.Add(customerNumber);
    //SaveChanges fails 
    db.SaveChanges();
}   

ApplicationDbContext:

//If the override is removed, everything works
public sealed override int SaveChanges()
{
    OnBeforeSave();
    return base.SaveChanges();
}

//Did not make any difference 
//public void TestModelCreation(DbModelBuilder model)
//{
//    OnModelCreating(model);
//}


protected void OnBeforeSave()
{
    var now = DateTime.Now;

    //This line causes the exception
    foreach (var changedEntity in ChangeTracker.Entries())
    {
        var entity = changedEntity.Entity as IEntity;
        if (entity != null)
        {
            switch (changedEntity.State)
            {
                case EntityState.Added:
                    entity.Created = now;
                    entity.Updated = now;
                    break;

                case EntityState.Modified:
                    Entry(entity).Property(x => x.Created).IsModified = false;
                    entity.Updated = now;
                    break;
            }
        }
    }
}
Ogglas
  • 62,132
  • 37
  • 328
  • 418

1 Answers1

0

The problem was the sealed modifier for SaveChanges. Switched to public override int SaveChanges() and everything worked.

Ogglas
  • 62,132
  • 37
  • 328
  • 418