EntityFramework-Plus icon indicating copy to clipboard operation
EntityFramework-Plus copied to clipboard

[SO] - How to use code-first with EntityFramework-Plus' audit feature?

Open JonathanMagnan opened this issue 5 years ago • 7 comments

Reference: https://stackoverflow.com/questions/58711828/how-to-use-code-first-with-entityframework-plus-audit-feature

JonathanMagnan avatar Nov 06 '19 15:11 JonathanMagnan

Hello @makoshichi,

My developer created a project (See the attachment) and didn't have an issue with the migration.

Could you provide more information about your problem? We will try to help you

Test.EFPlusCoreMigration.zip

JonathanMagnan avatar Nov 06 '19 15:11 JonathanMagnan

Hi @JonathanMagnan

Thanks for your prompt answer. I'll review the attached test today and give you further insight on my issue, should it be necessary.

makoshichi avatar Nov 08 '19 12:11 makoshichi

Awesome @makoshichi

Don't hesitate to contact us with more information!

Looking forward to hearing from you,

Jon

JonathanMagnan avatar Nov 08 '19 13:11 JonathanMagnan

Hey @JonathanMagnan

I figured out why yours works and mine doesn't: we have lazy loading enabled. Unfortunatelly I just briefly hinted at it in my original stack overflow post:

It looks good enough save for two properties: public List<AuditEntryProperty> Properties { get; set; } and public AuditEntry Parent { get; set; }. As they're not marked as virtual, adding a migration will fail.

At the time, I knew that "the virtual thing" was the cause but I didn't know why. And it is simple: properties must be marked as virtual in order to enable lazy loading . And as it turns out (and I failed to mention), this is the exact output error I get whenever I try to add a migration that tries to "create tables" with AuditEntry in them:

Navigation property 'Properties' on entity type 'AuditEntry' is not virtual. UseLazyLoadingProxies requires all entity types to be public, unsealed, have virtual navigation properties, and have a public or protected constructor.

If I disable Lazy Loading, the migration is successfully added and I can run update-database as usual. In our case, here it is:

    public abstract class DesignTimeDbContextFactoryBase<TContext> :
        IDesignTimeDbContextFactory<TContext> where TContext : DbContext
        {
            //...
            private TContext Create(string connectionString)
            {
                 //...
                 var optionsBuilder = new DbContextOptionsBuilder<TContext>();
                 //optionsBuilder.UseLazyLoadingProxies(); //GOTCHA!
                 optionsBuilder.UseSqlServer(connectionString);

                 return CreateNewInstance(optionsBuilder.Options);
             }
    }

Do you think you guys could take a look into it? I can live without it for a while, but sooner or later, someone is bound to need it.

makoshichi avatar Nov 08 '19 17:11 makoshichi

Hello @makoshichi ,

Sure, we will look at it ;)

JonathanMagnan avatar Nov 11 '19 14:11 JonathanMagnan

Hello @JonathanMagnan

Any updates on this issue?

makoshichi avatar Nov 27 '19 21:11 makoshichi

Hello @makoshichi ,

Unfortunately, we heavily miss time.

I tried last week (the reason why it took time to answer) but without success.

JonathanMagnan avatar Dec 04 '19 21:12 JonathanMagnan