C
C#5mo ago
MeJiGer

[ERCore7.0] Customize ModelValidator {Microsoft.EntityFrameworkCore.Infrastructure}

i making CutsmonModelValidator to rewirte ValidateNoMutableKeys() method to design another logic. now i hope use CutsmonModelValidator replace EFCore underly ModelValidator or RelationalModelValidator. sorry, i comfrom asian contry and my english is weak. IDE:VS 2022 Environment: .Net7 & EFCore 7.0 CutsmonModelValidator:
public class CutsmonModelValidator :
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator {

public CutsmonModelValidator() : base(null) {

}
public CutsmonModelValidator(ModelValidatorDependencies dependencies) :base(dependencies) {

}

protected override void ValidateNoMutableKeys(IModel model, IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger) {
/*
* my validate logic
*/


//base.ValidateNoMutableKeys(model, logger);
}
}
public class CutsmonModelValidator :
Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator {

public CutsmonModelValidator() : base(null) {

}
public CutsmonModelValidator(ModelValidatorDependencies dependencies) :base(dependencies) {

}

protected override void ValidateNoMutableKeys(IModel model, IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger) {
/*
* my validate logic
*/


//base.ValidateNoMutableKeys(model, logger);
}
}
i trying follow code in Program.cs
EntityFrameworkRelationalServicesBuilder.CoreServices.Remove(typeof(IModelValidator));
ServiceCollection Services = new ServiceCollection();
EntityFrameworkServicesBuilder EntityFrameworkServicesBuilder;
EntityFrameworkServicesBuilder = new EntityFrameworkServicesBuilder(Services);
EntityFrameworkServicesBuilder.TryAddProviderSpecificServices((ServiceCollectionMap ByServices) => {
ByServices.TryAddSingleton<IModelValidator, ExtendRelationalModelValidator>();
});
EntityFrameworkRelationalServicesBuilder.CoreServices.Remove(typeof(IModelValidator));
ServiceCollection Services = new ServiceCollection();
EntityFrameworkServicesBuilder EntityFrameworkServicesBuilder;
EntityFrameworkServicesBuilder = new EntityFrameworkServicesBuilder(Services);
EntityFrameworkServicesBuilder.TryAddProviderSpecificServices((ServiceCollectionMap ByServices) => {
ByServices.TryAddSingleton<IModelValidator, ExtendRelationalModelValidator>();
});
and run test code try insert new entity, but i got exception.
InvalidOperationException: The database provider attempted to register an implementation of the 'IModelValidator' service. This is not a service defined by Entity Framework and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method.
InvalidOperationException: The database provider attempted to register an implementation of the 'IModelValidator' service. This is not a service defined by Entity Framework and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method.
What should I do?
0 Replies
No replies yetBe the first to reply to this messageJoin