在 EF 6 中,可以使用约定向模型添加私有属性:
Types().Having(
NonPublicProperties)
.Configure(
(config, properties) =>
{
if (properties == null)
return;
foreach (var prop in properties)
config.Property(prop);
});
[...]
private IEnumerable<PropertyInfo>? NonPublicProperties (Type type)
{
var matchingProperties = type
.GetProperties(BindingFlags.SetProperty | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance)
.Where(p => CustomAttributeExtensions.GetCustomAttributes<ColumnAttribute>(p).Any())
.ToList();
// It is important to return null here if there are no elements to filter the type from the convention.
return matchingProperties.Count == 0 ? null : matchingProperties;
}
EF Core 中是否有同等的可能性?还是根本无法在 EF Core 模型中映射私有或受保护的属性?