Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In-Memory Database #50

Closed
kevin-work opened this issue Jun 28, 2017 · 2 comments
Closed

In-Memory Database #50

kevin-work opened this issue Jun 28, 2017 · 2 comments
Assignees

Comments

@kevin-work
Copy link

kevin-work commented Jun 28, 2017

Does Audit.EntityFramework not work with in-memory database? I need it for testing. I am using EF Core with xUnit.

I have a context factory like so:

public class DatabaseContextFactory
{
    public DatabaseContext Create()
    {
        var serviceProvider = new ServiceCollection()
            .AddEntityFrameworkInMemoryDatabase()
            .BuildServiceProvider();

        var builder = new DbContextOptionsBuilder<DatabaseContext>();
        builder.UseInMemoryDatabase()
            .UseInternalServiceProvider(serviceProvider);

        return new DatabaseContext(builder.Options);
    }
}

Where my DatabaseContext inherits from AuditDbContext:

public sealed class DatabaseContext : AuditDbContext
{
    public DatabaseContext(DbContextOptions options)
        : base(options)
    {
    }
}

I get this error when running tests:

System.IndexOutOfRangeException : Index was outside the bounds of the array.
Stack Trace:
    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
    at Audit.EntityFramework.DbContextHelper.EnsureEntitiesIncludeIgnoreAttrCache(Type type)
    at Audit.EntityFramework.DbContextHelper.IncludeEntity(IAuditDbContext context, EntityEntry entry,  AuditOptionMode mode)
    at Audit.EntityFramework.DbContextHelper.<>c__DisplayClass19_0.<GetModifiedEntries>b__0(EntityEntry x)
    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    at Audit.EntityFramework.DbContextHelper.GetModifiedEntries(IAuditDbContext context)
    at Audit.EntityFramework.DbContextHelper.CreateAuditEvent(IAuditDbContext context)
    at Audit.EntityFramework.DbContextHelper.SaveChanges(IAuditDbContext context, Func`1 baseSaveChanges)
    at Audit.EntityFramework.AuditDbContext.SaveChanges()
@thepirat000
Copy link
Owner

thepirat000 commented Jun 28, 2017

Are you using latest version of the packages?

Can you provide a more complete sample?

I was unable to reproduce. I've tested using an in-memory database and performing insert/update operations with no problems.

Anyway, from the error I can imply that the static cache for the audit attributes is having problems with multiple threads trying to add the same value at the same time.

Will look into converting the cache to a thread-safe structure.

thepirat000 added a commit that referenced this issue Jun 28, 2017
@thepirat000
Copy link
Owner

Please try again targeting the latest version >= 7.0.9, the concurrency problem should be fixed now.

Feel free to re-open this issue if you are still having problems.

Regards

@thepirat000 thepirat000 self-assigned this Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants