Discrimnator 열에 대한 인덱스를 자동으로 추가하는 사용자 지정 Entity Framework 마이그레이션 스캐 폴더 및 SQL 생성기를 구현하려고합니다. 이 솔루션을 구현하기 위해 어떤 열이 판별 자인지 감지하고 싶지만 지금까지 공식적이거나 비공식적 인 방법을 찾지 못했습니다.
SqlServerMigrationSqlGenerator 및 CSharpMigrationCodeGenerator 클래스 의 Generate () 메서드에 인수로 전달되는 ColumnModel 개체 의 내용을 검사하는 것으로 시작 했지만이를 가리키는 항목을 찾을 수 없습니다.
생성기 내부에서 판별자를 감지하는 수단이 있습니까? 아니면 컨텍스트에 포함 된 메타 데이터에서 가져온 것일까 요?
Github의 소스 코드를 살펴보고 정상적인 사람이해야하는 것보다 EF에 더 깊이 들어가면 작동하는 솔루션을 찾았다 고 생각합니다. 다른 사람이 필요할 경우를 대비하여 여기에 게시하십시오.
var metadata = ((IObjectContextAdapter)this).ObjectContext.MetadataWorkspace;
var entityTypes = metadata.GetItems<EntityType>(DataSpace.SSpace);
// These are the entity sets
var entitySets = metadata.GetItems<EntityContainer>(DataSpace.CSpace).Single().EntitySets;
foreach (var entitySet in entitySets)
{
// The mapping for the entity set
var mapping = metadata.GetItems<EntityContainerMapping>(DataSpace.CSSpace).Single().EntitySetMappings.Single(s => s.EntitySet == entitySet);
// If there is more than one mapping for the entity set...
if (mapping.EntityTypeMappings.Count > 0)
{
// This shows that these mappings belong to a hierarchy
var hierarchyMapping = mapping.EntityTypeMappings.SingleOrDefault(etm => etm.IsHierarchyMapping);
if (hierarchyMapping == null)
continue;
// Get the conditions that contain the discriminator columns and values
var conditions = mapping.EntityTypeMappings.SelectMany(etm => etm.Fragments.Single().Conditions).OfType<ValueConditionMapping>().ToList();
if (conditions.Select(cc => cc.Column).Distinct().Count() > 1)
{
Debug.WriteLine($"{mapping.EntitySet.Name} has multiple mappings one of them being a hierachy mapping, but the fragments' conditions refer more than one distinct edm property");
continue;
}
if (conditions.Select(cc => cc.Column).Distinct().Count() < 1)
{
Debug.WriteLine($"{mapping.EntitySet.Name} has a hierachy mapping, but none of the fragments' conditions are ValueConditionMappings");
continue;
}
// This is the discriminator's name in the database
var discriminatorColumn = conditions.First().Column.Name;
}
}
기본 판별 자 열과 사용자 지정 판별 자 열을 사용하여이 솔루션을 테스트했으며 두 경우 모두 정상적으로 작동했습니다. 또한 TPT 및 TPC와 엔티티 분할 및 테이블 분할로 테스트했으며 모든 경우에 차별자가 표시되지 않았으므로 제대로 작동하는 것 같습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다