Как я могу избавиться от жесткого кодирования при создании нового EntityKey в Entity Framework - PullRequest
0 голосов
/ 06 ноября 2008

Как мне избавиться от жесткого кодирования при создании нового EntityKey в Entity Framework

1 Ответ

2 голосов
/ 17 июня 2009
GetType(Model).Name
GetType(Account).Name

, что означает:

Public Shared Function GetName(Of TEntity As Type)() As String
    Dim type = GetType(TEntity)
    If Not type.IsSubclassOf(GetType(EntityObject)) Then Throw New Exception("Item must inherit from EntityObject.")
    Return GetType(ObjectContextName).Name & "." & type.Name
End Function

Если вы плюрализировали свои наборы сущностей в .NET 3.5 (службы плюрализации которых не существует), используйте:

private static string ObjectContextName = typeof(Rlp.Data.Entities).Name;
public static string GetEntityName<TEntity>([Optional, DefaultParameterValue(false)]bool pluralize) where TEntity : Type
{
    Type type = typeof(TEntity);
    if (!type.IsSubclassOf(typeof(EntityObject))) throw new Exception("Item must inherit from EntityObject.");
    string entitySet = type.Name;
    if (pluralize) entitySet = Pluralizer.ToPlural(entitySet);
    return ObjectContextName + "." + entitySet;
}

Или даже:

private static string ObjectContextName = typeof(Rlp.Data.Entities).Name;
private const string EntityIdSuffix = "Id";
public static EntityKey CreateEntityKey<TEntity>(string id , [Optional, DefaultParameterValue(false)]bool pluralize) where TEntity : Type
{
    Type type = typeof(TEntity);
    if (!type.IsSubclassOf(typeof(EntityObject))) throw new Exception("Item must inherit from EntityObject.");
    string entity = type.Name;
    string entitySet;
    entitySet = pluralize? Pluralizer.ToPlural(entity): entity;
    return new EntityKey(ObjectContextName + "." + entitySet, entity + EntityIdSuffix, id);  
}

Помощник Pluralizer от: Простой английский Существительное Pluralizer в C #

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...