如何对拥有的类型使用自定义名称约定? [英] How to use custom name convention with owned types?

查看:71
本文介绍了如何对拥有的类型使用自定义名称约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

不使用我的约定我会得到

Without using my convention I got

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

好像我不需要将转换应用于某些实体

Looks like I need to not apply my transformation to some of entity:

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

之后,我得到了:


'ProcessInitialization.ProcessContext#MyProcessContext'上的键{'ProcessInitializationCorrelationId'}和'ProcessInitialization'上的键''CorrelationId'}都映射到'process_initialization.PK_process_initialization',但具有不同的列({'process_initialization_correlation_id'}和{'correlation_id'})。

The keys {'ProcessInitializationCorrelationId'} on 'ProcessInitialization.ProcessContext#MyProcessContext' and {'CorrelationId'} on 'ProcessInitialization' are both mapped to 'process_initialization.PK_process_initialization' but with different columns ({'process_initialization_correlation_id'} and {'correlation_id'}).


推荐答案

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

Owned types require special processing.

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

First, you recognize them by using IEntityType.IsOwned() method.

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

Then, when the entity type is owned, you should skip the table name mapping (because they share the same table as the root owner entity). Also owned types have shadow PK properties which should be skipped, and for other properties you should build column name from the path from root owner to the owned type. The owner information is accessible through DefiningEntityType and DefiningNavigationName properties of IEntityType.

将所有内容应用于代码:

Applying all that to your code:

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
    foreach (var prop in entity.GetProperties())
    {
        if (entity.IsOwned() && prop.IsPrimaryKey()) continue;
        IEntityType propOwner = entity;
        string propName = prop.Name, columnName = null;
        do
        {
            if (!propMap.TryGetValue(propName, out var name))
                name = AddUndercoresToSentence(propName, preserveAcronyms).ToLowerInvariant();
            columnName = columnName == null ? name : name + "_" + columnName;
            propName = propOwner.DefiningNavigationName;
            propOwner = propOwner.DefiningEntityType;
        }
        while (propName != null);
        prop.Relational().ColumnName = columnName;
    }

    if (entity.IsOwned()) continue;
    var entName = entity.DisplayName();
    if (!entMap.TryGetValue(entName, out var tableName))
        tableName = AddUndercoresToSentence(entName, preserveAcronyms).ToLowerInvariant();
    entity.Relational().TableName = tableName;
}

这篇关于如何对拥有的类型使用自定义名称约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆