[英]Prefix Column Naming Convention in Entity Framework
請考慮EF6代碼優先的數據庫。 我有:
public class Base
{
public int Id { get; set; }
}
public class Desc1 : Base
{
public string Foo { get; set; }
}
public class Desc2 : Base
{
public int? Foo { get; set; }
}
我希望表Base
具有三列(以自動方式):
Base_Id INT NOT NULL
Desc1_Foo VARCHAR(MAX) NULL
Desc2_Foo INT NULL
我的意思是,以其類名開頭。 我知道我可以使用EntityTypeConfiguration
來做到這一點,但是我的數據庫有150個表,因此無法配置每一列。
因此,我嘗試使用命名約定:
public class PrefixConvention : IStoreModelConvention<EdmProperty>
{
public void Apply(EdmProperty property, DbModel model)
{
property.Name = property.DeclaringType.Name + "_" + property.Name;
}
}
...
modelBuilder.Conventions.Add(new PrefixConvention());
但是property.DeclaringType.Name
僅向我顯示Base
,而不向我顯示Desc1
或Desc2
,它始終會創建以下內容:
Base_Id INT NOT NULL
Base_Foo VARCHAR(MAX) NULL
Base_Foo1 INT NULL
我研究了EdmModel
類,但是找不到找到源自表中字段的類名稱的方法!
好的,我開發了自己的PrefixConvention。 要使用它:
modelBuilder.Conventions.Add(new PrefixConvention());
在這里, PrefixConventionclass
:
using System.Collections.Generic;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
namespace Habitus.Toolkit.Database
{
public class PrefixConvention : IStoreModelConvention<EdmProperty>
{
public void Apply(EdmProperty property, DbModel model)
{
if (property.Name == "Id" || property.Name == "Discriminator" || property.DeclaringType.IsView()) return;
var fragments = model
.ConceptualToStoreMapping
.EntitySetMappings
.SelectMany(esm => esm.EntityTypeMappings)
.SelectMany(etm => etm.Fragments)
.Where(f => f.StoreEntitySet.ElementType == property.DeclaringType)
.ToList();
var propertyMappings = fragments
.SelectMany(f => f.PropertyMappings)
.ToList();
var path = GetPropertyPath(property, propertyMappings);
if (path?.Any() == false)
{
throw new System.Exception($"Can't process property {property.DeclaringType.FullName}.{property.Name}");
}
var first = path.First();
var names = path.Select(pm => pm.Name).ToList();
property.Name = $"{first.DeclaringType.Name}__{string.Join("_", names)}";
}
private List<EdmProperty> GetPropertyPath(EdmProperty property, List<PropertyMapping> propertyMappings)
{
var result = new List<EdmProperty>();
var scalarPropertyMapping = propertyMappings
.OfType<ScalarPropertyMapping>()
.Where(pm => pm.Column == property)
.FirstOrDefault();
if (scalarPropertyMapping != null)
{
result.Add(scalarPropertyMapping.Property);
}
else
{
var complexPropertyMappings = propertyMappings
.OfType<ComplexPropertyMapping>()
.ToList();
foreach (var complexPropertyMapping in complexPropertyMappings)
{
var recursivePropertyMappings = complexPropertyMapping
.TypeMappings
.SelectMany(tm => tm.PropertyMappings)
.ToList();
var recursiveResult = GetPropertyPath(property, recursivePropertyMappings);
if (recursiveResult.Any())
{
result.Add(complexPropertyMapping.Property);
result.AddRange(recursiveResult);
break;
}
}
}
return result;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.