jeudi 5 octobre 2017

Add items to an entity collection via ChangeTracker

I chose to put a complete example of my code in order to demonstrate what I exactly need. Briefly, what I would like to do, is to get a generic code in public override int SaveChanges() that could work to all entities that implements a translation rather than write a one-by-one.

ENTITIES

public partial class EntityOne
{
    public long EntityOneId { get; set; }
    public string SomeProperty { get; set; }

    public virtual ICollection<EntityOneTranslation> EntityOneTranslations { get; set; }

    public EntityOne()
    {
        this.EntityOneTranslations = new HashSet<EntityOneTranslation>();
    }
}

public class EntityOneTranslation : EntityTranslation<long, EntityOne>
{
    public string LocalizedEntityOneProp1 { get; set; }
    public string LocalizedEntityOneProp1 { get; set; }
}

public partial class EntityTwo
{
    public long EntityTwoId { get; set; }
    public string SomeProperty { get; set; }

    public virtual ICollection<EntityTwoTranslation> EntityTwoTranslations { get; set; }

    public EntityTwo()
    {
        this.EntityTwoTranslations = new HashSet<EntityTwoTranslation>();
    }
}

public class EntityTwoTranslation : EntityTranslation<long, EntityTwo>
{
    public string LocalizedEntityTwoProp1 { get; set; }
    public string LocalizedEntityTwoProp2 { get; set; }
}

public class EntityTranslation<TEntityKey, TEntity> : ILanguage
{
    [Key, Column(Order = 0), ForeignKey("Entity")]
    public TEntityKey EntityKey { get; set; }
    [Key, Column(Order = 1), ForeignKey("Language")]
    public long LanguageId { get; set; }

    public virtual TEntity Entity { get; set; }
    public virtual Language Language { get; set; }
}

INTERFACE

public interface ILanguage
{
    long LanguageId { get; set; }
}

Here is the target

How would I get the entity navigation property using reflection/or something in order to reuse my code that could work too all entities that has a translation property collection?

I already tried to ask the same thing from 2 other posts, but I didn't give all the info. I guess that's why nobody could give me the expected answer.

Adding new entries over entity navigation property collection

Cast PropertyInfo to Collection

SAVE CHANGES OVERRIDE

public override int SaveChanges()
{
    foreach (var entityOneEntry in ChangeTracker.Entries<EntityOne>())
    {
        if (entityOneEntry.State == EntityState.Added)
        {

            //Get entity localized properties values of current language.
            var currentLanguageEntry = entityOneEntry.Entity.EntityOneTranslations.FirstOrDefault();
            var localizedEntityOneProp1 = currentLanguageEntry.LocalizedEntityOneProp1;
            var localizedEntityOneProp2 = currentLanguageEntry.LocalizedEntityOneProp2;

            //Get all languages but the current one.
            var languages = Language.Where(l => l.LanguageId != currentCulture.Key);

            //Add missing translations copying the same values.
            foreach (var language in languages)
                entityOneEntry.Entity.EntityOneTranslations.Add(new EntityOne()
                {
                    LanguageId = language.LanguageId,
                    LocalizedEntityOneProp1 = localizedEntityOneProp1,
                    LocalizedEntityOneProp2 = localizedEntityOneProp2
                });
        }
    }

    foreach (var entityOneEntry in ChangeTracker.Entries<EntityTwo>())
    {
        if (entityOneEntry.State == EntityState.Added)
        {

            //Get entity localized properties values of current language.
            var currentLanguageEntry = entityOneEntry.Entity.EntityTwoTranslations.FirstOrDefault();
            var localizedEntityTwoProp1 = currentLanguageEntry.LocalizedEntityTwoProp1;
            var localizedEntityTwoProp2 = currentLanguageEntry.LocalizedEntityTwoProp2;

            //Get all languages but the current one.
            var languages = Language.Where(l => l.LanguageId != currentCulture.Key);

            //Add missing translations copying the same values.
            foreach (var language in languages)
                entityOneEntry.Entity.EntityTwoTranslations.Add(new EntityTwo()
                {
                    LanguageId = language.LanguageId,
                    LocalizedEntityTwoProp1 = localizedEntityTwoProp1,
                    LocalizedEntityTwoProp2 = localizedEntityTwoProp2
                });
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire