-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should pre-convention configuration overwrite data annotations? #28541
Comments
Intended behavior. The bulk configuration has same priority as fluent API since it is explicitly specified by user. |
Oh. That is sad :-) I would have expected, that pre-convention configurations are always overwritten by explicit (fluent or data annotation) configuration. But thanks a lot for clarifying. For anyone interested in a workaround: I use this extension method based on https://stackoverflow.com/a/50852517/909237 public static class ModelBuilderExtensions
{
#pragma warning disable EF1001
public static void SetMaxStringLength(this ModelBuilder modelBuilder, int stringLength)
{
// https://stackoverflow.com/a/50852517/909237
// Use explicit setting instead of pre convention configuration, since the latter breaks [MaxLength] attributes
// https://docs.microsoft.com/en-us/ef/core/modeling/bulk-configuration#pre-convention-configuration
var stringProperties = modelBuilder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => p.ClrType == typeof(string))
.OfType<Property>();
foreach (var property in stringProperties)
property.Builder.HasMaxLength(stringLength, ConfigurationSource.Convention);
}
} protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.SetMaxStringLength(256);
} |
Actually, I'm not sure if this is really true: According to So is it really intended, that pre-convention overrides |
We discussed this, and while the naming is perhaps unfortunate, and the experience of overriding with an annotation would be nice, the reality is that this method sets up low-level type mapping which acts like explicit configuration. Public conventions, which are planned for EF7, will allow a convention to be written that will allow overriding via data annotations. |
For anyone else finding this and intending to use it to avoid the warning about Precision not configured (without overriding any attributes you did set) you can do:
|
I use pre-convention configuration to set the default maximum string length for my context to 256:
When I override this with a
[StringLength(100)]
or[MaxLength(100)]
attribute, the default value (here 256) is used and the attribute ignored. Overwriting the string length with fluent configuration is working.Is this intended behavior or an issue?
I personally prefer to set string length in attributes, since this is configured directly next to the property.
Include provider and version information
EF Core version: 6.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .Net 6.0
Kind regards
Peter
The text was updated successfully, but these errors were encountered: