EF Core 2.1拥有的类型有哪些更改?

时间:2018-07-28 16:48:53

标签: c# entity-framework-core ef-core-2.1

Docs的最后一次更改是在02/26/2018,但是我需要修复一些重大更改。

我有MyProcessContext类型,用于许多实体:

public class RequestData
{
    public Guid CorrelationId { get; set; }
    public DateTime Updated { get; set; }

    public MyProcessContext ProcessContext { get; set; }
}

public class MyProcessContext
{
    public int ClientId { get; set; }
}

public class EntityConfiguration : IEntityTypeConfiguration<RequestData>
{
    public void Configure(EntityTypeBuilder<RequestData> builder)
    {
        // all mapped by convention

        builder.OwnsOne(x => x.ProcessContext);

        builder.Property(x => x.Updated).IsRowVersion();

        builder.HasKey(x => x.CorrelationId);
        builder.Property(x => x.CorrelationId).ValueGeneratedNever();
    }
}

它可以与EF Core 2.0一起正常工作。在调试视图中,对于每种拥有类型(RequestData.ProcessContext#MyProcessContext),我都有不同的实体类型:

  EntityType: RequestData
    Properties: 
      CorrelationId (Guid) Required PK AfterSave:Throw 0 0 0 -1 -1
        Annotations: 
          Relational:ColumnName: correlation_id      
      Updated (DateTime) Required Concurrency BeforeSave:Ignore AfterSave:Ignore ValueGenerated.OnAddOrUpdate 11 11 -1 -1 0
        Annotations: 
          Relational:ColumnName: updated
    Navigations: 
      ProcessContext (<ProcessContext>k__BackingField, MyProcessContext) ToDependent RequestData.ProcessContext#MyProcessContext 0 -1 1 -1 -1
    Keys: 
      CorrelationId PK
    Annotations: 
      Relational:TableName: process_request
      RelationshipDiscoveryConvention:NavigationCandidates: System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]

  EntityType: RequestData.ProcessContext#MyProcessContext
    Properties: 
      RequestDataCorrelationId (no field, Nullable<Guid>) Shadow Required PK FK AfterSave:Throw 0 0 0 0 -1
      ClientId (int) Required 1 1 -1 -1 -1      
    Keys: 
      RequestDataCorrelationId PK
    Foreign keys: 
      RequestData.ProcessContext#MyProcessContext {'RequestDataCorrelationId'} -> RequestData {'CorrelationId'} Unique ToDependent: ProcessContext
    Annotations: 
      Relational:TableName: process_request
      RelationshipDiscoveryConvention:NavigationCandidates: System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]    

升级后调试视图包含

  EntityType: RequestData
      ...
      ProcessContext (<ProcessContext>k__BackingField, MyProcessContext) ToDependent MyProcessContext 0 -1 1 -1 -1
      ...
  EntityType: MyProcessContext
    Properties: 
      RequestDataCorrelationId (no field, Guid) Shadow Required PK FK AfterSave:Throw 0 0 0 0 -1
      ClientId (int) Required 1 1 -1 -1 -1
    Keys: 
      RequestDataCorrelationId PK
    Foreign keys: 
      MyProcessContext {'RequestDataCorrelationId'} -> RequestData {'CorrelationId'} Unique Ownership ToDependent: ProcessContext
    Annotations: 
      Relational:TableName: process_request
      RelationshipDiscoveryConvention:NavigationCandidates: System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]

MyProcessContext这样构建的模型是通常的实体。在类似

的代码中
foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                var entityIsOwned = entity.IsOwned();

entityIsOwned是正确的,但entity.DefiningNavigationName现在为null。

我需要custom name convention的名字。

1 个答案:

答案 0 :(得分:1)

到目前为止,唯一的正式更改(EF Core 2.1)是添加了[Owned] Attribute

然而,显然实现中发生了重大变化,因此DefiningEntityTypeDefiningNavigationName属性不再可靠(无论如何都没有记录-只是“标准”生成的无用”定义实体类型。” “获取定义导航的名称。” )。

通过实验,我发现只有在多个实体中使用拥有的类型时,才会填充它们。不知道为什么要这样做,但主要要点是我们不应该使用它们,而且EF Core代码本身似乎也不再使用它们。

根据我的发现,IsOwnedDefiningEntityTypeDefiningNavigationName项由具有IsOwnership属性等于true的外键确定。

换句话说:

var ownershipFk = entityType.GetForeignKeys().FirstOrDefault(fk => fk.IsOwnership);
var isOwned = ownershipFk != null;
var definingEntityType = ownershipFk?.PrincipalEntityType;
var definingNavigationName = ownershipFk?.PrincipalToDependent.Name;