如何使用自有类型的自定义名称约定?

时间:2017-12-08 15:06:53

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

public class ProcessInitialization
{
    public Guid CorrelationId { get; set; }
    public MyProcessContext ProcessContext { get; set; }
}

public class MyProcessContext
{
    public int? ProcessId { get; set; }
}

没有使用我的惯例,我得到了

SELECT `process`.`CorrelationId`
FROM `ProcessInitialization` AS `process`
WHERE `process`.`ProcessContext_ProcessId` = 8

我的约定来自snake_case

PascalCase
public static void SetSimpleUnderscoreTableNameConvention(this ModelBuilder modelBuilder,
    bool preserveAcronyms,
    IDictionary<string, string> propertyMap = null,
    IDictionary<string, string> entityMap = null
    )
{
    var propMap = propertyMap ?? new Dictionary<string, string>();
    var entMap = entityMap ?? new Dictionary<string, string>();

    foreach (var entity in modelBuilder.Model.GetEntityTypes())
    {
        foreach (var prop in entity.GetProperties())
        {
            if (propMap.ContainsKey(prop.Name))
            {
                prop.Relational().ColumnName = propMap[prop.Name];
            }
            else
            {
                var underscoredProp = AddUndercoresToSentence(prop.Name, preserveAcronyms);
                prop.Relational().ColumnName = underscoredProp.ToLowerInvariant();
            }
        }

        var entName = entity.DisplayName();

        if (entMap.ContainsKey(entName))
        {
            entity.Relational().TableName = entMap[entName];
        }
        else
        {
            var underscored = AddUndercoresToSentence(entity.DisplayName(), preserveAcronyms);
            entity.Relational().TableName = underscored.ToLowerInvariant();
        }
    }
}

随着查询变得无效:

SELECT `process`.`correlation_id`
FROM `process`
LEFT JOIN `process_initialization._process_context#_my_process_context` AS `process.ProcessContext` ON `process`.`correlation_id` = `process.ProcessContext`.`process_initialization_correlation_id`
WHERE `process.ProcessContext`.`process_id` = 8

看起来我不需要将我的转换应用于某些entity

var underscored = AddUndercoresToSentence(entity.DisplayName(), preserveAcronyms);
if (underscored.Contains("#")) // any other way to understand it is special?
    continue;
entity.Relational().TableName = underscored.ToLowerInvariant();

之后我得到了:

  

&#39; ProcessInitialization.ProcessContext#MyProcessContext&#39;上的键{&#39; ProcessInitializationCorrelationId&#39;}和{&#39; CorrelationId&#39;} on&#39; ProcessInitialization&#39;都映射到&#39; process_initialization.PK_process_initialization&#39;但是使用不同的列({&#39; process_initialization_correlation_id&#39;}和{&#39; correlation_id&#39;})。

1 个答案:

答案 0 :(得分:2)

拥有的类型需要特殊处理。

首先,您使用IEntityType.IsOwned()方法识别它们。

然后,当拥有实体类型时,您应该跳过表名映射(因为它们与根所有者实体共享同一个表)。还拥有的类型具有应跳过的影子PK属性,对于其他属性,您应该从从root用户到拥有类型的路径构建列名。可以通过DefiningEntityType的{​​{1}}和DefiningNavigationName属性访问所有者信息。

将所有内容应用到您的代码中:

IEntityType