Я боюсь, что это невозможно. Вам нужно будет выполнить довольно сложный поиск рефракции, чтобы найти все типы со свойством с типом ICollection <ITranslation>. Я предлагаю вам пойти по более короткому пути и определить ITranslationContainer или ITranshable, как я указал в своем комментарии, и заставить FileType и ElementType реализовать его. Тогда просто пройдите все ChangeTracker.Entries <ITranshable>
ужно создать общий способ добавления пропущенных языков записей ко всем сущностям, в которых реализуется определенный интерфейс. Я узнал, как получить мою коллекционную собственность, но я все еще не знаюкак добавить новые значения на него раньше приступить к сохранению.
После кусочка моегоpublic override int SaveChanges()
обработки.
foreach (var translationEntity in ChangeTracker.Entries(<ITranslation>))
{
if (translationEntity.State == EntityState.Added)
{
var translationEntries = translationEntity.Entity.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite &&
x.GetGetMethod().IsVirtual &&
x.PropertyType.IsGenericType == true &&
typeof(IEnumerable<ILanguage>).IsAssignableFrom(x.PropertyType) == true);
foreach (var translationEntry in translationEntries)
{
//Add missing items.
}
}
}
Классы примеров кода
public partial class FileType : ITranslation
{
public long FileTypeId { get; set; }
public string AcceptType { get; set; }
public virtual ICollection<FileTypeTranslation> FileTypeTranslations { get; set; }
public FileType()
{
this.FileTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class FileTypeTranslation : EntityTranslation<long, FileType>, ILanguage
{
[Required]
public string TypeName { get; set; }
}
public partial class ElementType : ITranslation
{
public long ElementTypeId { get; set; }
public string Code { get; set; }
public virtual ICollection<ElementTypeTranslation> ElementTypeTranslations { get; set; }
public ElementType()
{
this.ElementTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class ElementTypeTranslation : EntityTranslation<long, ElementType>, ILanguage
{
[Required]
public string Description { get; set; }
}