EntityFramework.DynamicFilters
EntityFramework.DynamicFilters copied to clipboard
Q about table attributes
Hi!
We have a number of applications which use a shared library to access a common database (Sql Server). However, each application would like to define it's own filters - some using soft delete, some using tenancy, some only seeing most recent versions and so on.
I was hoping to do this by defining a BaseDbContext in the shared library and then have each application define a CustomDbContext extending BaseDbContext and declaring the required filters there.
This, however, doesn't work, as each filter gets mapped to a table attribute, so the framework will always think the database is out of sync with the model.
The only solution I can see is to define all the filters in the shared library. Unfortunately, this means that if we need to define a new filter for a single application, we must release new versions of all programs and do a database migration?
Are there other options?
Hello @NielsUll ,
I'm not sure to understand fully the problem.
Why not simply add a global event such this:
public static Action<DbModelBuilder> DbModelBuilderBuilder;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if(DbModelBuilderBuilder != null)
{
DbModelBuilderBuilder(modelBuilder);
}
// ...code...
base.OnModelCreating(modelBuilder);
}
This way, every project will be able to hook in the OnModelCreating event and add any filter required by the specific applications.
Let me know if that helped you,
Best Regards,
Jonathan
OK, let me explain further - perhaps I am misunderstaning something?
I have an existing BaseDbContext implementation and a database - and my test runs. I add the line
modelBuilder.Filter("MyNewFilter", (MyEntity e) => e.Name.StartsWith("T"));
to the BaseDbContext.OnModelCreating.
Now, when starting a test, I get
System.InvalidOperationException: The model backing the 'BaseDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).. ` Aborting test execution.
This will be a problem, since adding a filter in one application will cause other applications to fail :-(
I added Database.SetInitializer<MyContext>(null); in startup and it worked