Выпуск
В настоящее время я пытаюсь установить префикс моих таблиц и их столбцов с помощью установленного атрибута. Я использую Entity Framework Core. Я правильно понял префикс имени таблицы, но я не могу понять, что это за столбцы. У меня такое чувство, что мне нужно использовать отражение.
Я оставил свою (вероятно, неудачную) попытку отражения. Есть ли у кого-то способ установить имя столбца в сущности?
код
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Debugger.Launch();
//Loop though each entity to set table names correctly
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
//Get the custom attributes of the entity
var attributes = entity.ClrType.GetCustomAttributes(true);
//Throw exception if there isn't any custom attribute applied to the class
if (attributes.Length == 0)
throw new ArgumentNullException(nameof(entity), "Entity is missing table prefix.");
//Get the table prefix
var prefix = attributes[0];
//Set the table name
entity.Relational().TableName = prefix + entity.Relational().TableName;
//Loop through all the columns and apply the prefix
foreach (var prop in entity.GetProperties())
{
var propInfo = entity.ClrType.GetProperty(prop.Name);
propInfo.SetValue(entity.ClrType, prefix + prop.Name, null);
}
}
}