You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add option to ignore all properties for model #14405
When using a shared model with a lot of properties that should not be mapped it would be helpful to be able to define a property mapping type on the entity. I imagine it like this:
publicclassMyContext:DbContext{protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<MyModel>().UseBindingMode(BindingMode.Ignore);}}publicenumBindingMode{/// <summary>/// Represents the default binding mode. Public properties will be added to the model./// </summary>Default,/// <summary>/// Represents the ignore binding mode. Public properties will not be added to the model unless explicitly defined./// </summary>Ignore}
Currently it's needed to specify each ignored property separately. When using existing databases and existing models this is a repetitive work and bloats up the model definitions.
Note for triage: as discussed, this is likely covered by public conventions. However, it might be worth keeping this issue open for sugar on top of public conventions based on the current experience which requires:
Removing PropertyDiscoveryConvention from two places
Removing PropertyMappingValidationConvention, which may have other side effects.
@Chris579 Some code that works around this by modifying the conventions EF uses is shown below. However, note that this code makes use of internal APIs and as such may be broken in any future release of EF Core. Use at your own risk.
The general idea is to register a custom SqlServerConventionSetBuilder that removes the property discovery convention. Here's the basic idea
publicclassMySqlServerConventionSetBuilder:SqlServerConventionSetBuilder{publicMySqlServerConventionSetBuilder(RelationalConventionSetBuilderDependenciesdependencies,ISqlGenerationHelpersqlGenerationHelper):base(dependencies,sqlGenerationHelper){}publicoverrideConventionSetAddConventions(ConventionSetconventionSet){conventionSet=base.AddConventions(conventionSet);// Find the PropertyDiscoveryConvention instance and remove itvarpropertyDiscoveryConvention=(PropertyDiscoveryConvention)conventionSet.EntityTypeAddedConventions.SingleOrDefault(c =>c.GetType()==typeof(PropertyDiscoveryConvention));if(propertyDiscoveryConvention!=null){conventionSet.EntityTypeAddedConventions.Remove(propertyDiscoveryConvention);conventionSet.BaseEntityTypeChangedConventions.Remove(propertyDiscoveryConvention);}// Find the PropertyMappingValidationConvention instance and remove itvarpropertyMappingValidationConvention=(PropertyMappingValidationConvention)conventionSet.ModelBuiltConventions.SingleOrDefault(c =>c.GetType()==typeof(PropertyMappingValidationConvention));if(propertyMappingValidationConvention!=null){conventionSet.ModelBuiltConventions.Remove(propertyMappingValidationConvention);}returnconventionSet;}}
To use this, call ReplaceService when configuring the context:
This custom convention seems to works when I have an additional property of a basic type like a string (no more 'Invalid column name' error), but doesn't seem to work with properties that are of non-native types.
For example: public KeyValuePair<int, int> SharedTo { get; set; }
I still get an error in Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate:
System.InvalidOperationException : 'The property 'DeviceBucket.SharedTo' could not be mapped, because it is of type 'KeyValuePair<int, int>' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
In my situation, the C# model is used in a broader scope than just for the database. I have much more properties in my C# model than in my SQL table, and other programmers could had new ones at any time that we don't want to save in the database. As chris579 mentioned, I would prefer having to implicitly specify the properties that are in the database rather than having to implicitly specify those to be ignored.
In a perfect world, I would have expected EF to only automatically map properties where there is a column of the same name in the database without having to ignore or include explicitly anything other than the exceptions (different names/conversion rules).
edit:
After a few more tests, the first two versions of the convention (the one who also remove PropertyMappingValidationConvention) works fine. The alternate one also works if I comment the line with IsCandidatePrimitiveProperty . But I can't really say I understand what this is doing and what impact it would provoke.
In addition, it doesn't ignore Class type properties, it tries to automatically add them to the list of known entities, and then throw an exception because the key isn't defined. A property of type Tuple throw an exception about the constructor having bad parameters. How can I just tell it to ignore them all?
Activity
ajcvickers commentedon Jan 14, 2019
Note for triage: as discussed, this is likely covered by public conventions. However, it might be worth keeping this issue open for sugar on top of public conventions based on the current experience which requires:
PropertyDiscoveryConventionfrom two placesPropertyMappingValidationConvention, which may have other side effects.@Chris579 Some code that works around this by modifying the conventions EF uses is shown below. However, note that this code makes use of internal APIs and as such may be broken in any future release of EF Core. Use at your own risk.
The general idea is to register a custom
SqlServerConventionSetBuilderthat removes the property discovery convention. Here's the basic ideaTo use this, call ReplaceService when configuring the context:
Here's a more general version that removes conventions of any type specified:
ajcvickers commentedon Jan 16, 2019
Slightly different workaround, used in the same way, which avoids having to remove the
PropertyMappingValidationConvention.:ajcvickers commentedon Jan 16, 2019
See also #4356
Dunge commentedon Jul 31, 2019
This custom convention seems to works when I have an additional property of a basic type like a string (no more 'Invalid column name' error), but doesn't seem to work with properties that are of non-native types.
For example:
public KeyValuePair<int, int> SharedTo { get; set; }I still get an error in Microsoft.EntityFrameworkCore.Metadata.Internal.Model.Validate:
In my situation, the C# model is used in a broader scope than just for the database. I have much more properties in my C# model than in my SQL table, and other programmers could had new ones at any time that we don't want to save in the database. As chris579 mentioned, I would prefer having to implicitly specify the properties that are in the database rather than having to implicitly specify those to be ignored.
In a perfect world, I would have expected EF to only automatically map properties where there is a column of the same name in the database without having to ignore or include explicitly anything other than the exceptions (different names/conversion rules).
edit:
After a few more tests, the first two versions of the convention (the one who also remove
PropertyMappingValidationConvention) works fine. The alternate one also works if I comment the line withIsCandidatePrimitiveProperty. But I can't really say I understand what this is doing and what impact it would provoke.In addition, it doesn't ignore Class type properties, it tries to automatically add them to the list of known entities, and then throw an exception because the key isn't defined. A property of type Tuple throw an exception about the constructor having bad parameters. How can I just tell it to ignore them all?