Skip to content

Using EntityFramework 6.2 model store breaks filtering #132

@mcnallys

Description

@mcnallys
  • Version 1.4.10.2

Entity framework 6.2 beta introduces a new feature that caches the compiled models; when using this feature "OnModelCreating" is not called.

From: https://blogs.msdn.microsoft.com/dotnet/2017/05/23/announcing-ef-6-2-beta-1/

image

I was able to get this to work through some "hacks" to the code, but I am curious as to what you think a full solution should be.

I had to mark DynamicFilterDefinition as public, and [Serializable]. The predicate expression would need to be serialized through another mechanism- perhaps a string and using System.Linq.Dynamic. The attribute name also needed to be left as just "DynamicFilter" for the following portion to work.

We have a class that inherits from "DbConfiguration" and in the constructor we can call
SetMetadataAnnotationSerializer("DynamicFilter", () => new DynamicFilterSerializer());

See "Non-String Annotations" from https://entityframework.codeplex.com/wikipage?title=Code%20First%20Annotations

DynamicFilterSerializer is just a simple wrapper around a binary formatter and base64 string. This will make entityframework rebuild the DynamicFilterDefinition object when it recreates the compiled model from the store.

Finally this allows us to reinitialize the filters through another hack like so:

public OurContext(string sqlConnectionString) : base(sqlConnectionString)
{
var mb = new DbModelBuilder();
mb.ResetDynamicFilters();
mb.Filter(DELETED_EVENT_FILTER_NAME, (Type e) => e.IsRemoved, false);
mb.Filter(DELETED_TRAIT_FILTER_NAME, (Type t) => t.IsRemoved, false);
mb.Filter(DELETED_RESOURCE_FILTER_NAME, (Type r) => r.IsRemoved, false);
}

OnModelCreating must still exist for when it has to be compiled the first time.

I am willing to do the legwork on this and submit a pull request but am curious as to the approach you would like to see for this before I committed to some code.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions