EntityFramework-Plus
EntityFramework-Plus copied to clipboard
[SO] - How to use code-first with EntityFramework-Plus' audit feature?
Reference: https://stackoverflow.com/questions/58711828/how-to-use-code-first-with-entityframework-plus-audit-feature
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
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.
Awesome @makoshichi
Don't hesitate to contact us with more information!
Looking forward to hearing from you,
Jon
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.
Hello @makoshichi ,
Sure, we will look at it ;)
Hello @JonathanMagnan
Any updates on this issue?
Hello @makoshichi ,
Unfortunately, we heavily miss time.
I tried last week (the reason why it took time to answer) but without success.