Adding new object to C# Entity Collection using base class

208 Views Asked by At

I've created an extension method on a collection of a base class that I'd like to be able to implement with my derived (entity) classes. Using these classes with Entity Framework Core. Simplified Example:

Class BaseClass
{
    DateTime StartDate { get; set; }
    DateTime? EndDate { get; set; }
    DateTime? DeletedAt { get; set; }
}

Class DerivedClass : BaseClass
{
    String Name { get; set; }
}

My extension methods operate on ICollection or IEnumerable of BaseClass. For example, this works fine:

public static int DeleteActiveSingleMembershipAndOpenPrevious(this IEnumerable<BaseClass> baseCollection)
{
    var activeMemberships = baseCollection
            .Where(bc =>
                bc.EndDate == null &&
                bc.DeletedAt == null
            );
    if (activeMemberships == null || activeMemberships.Count() != 1)
    {
        return 0;
    }
    else
    {
        activeMemberships.First().DeletedAt = DateTime.Now;
        var nextRecentActive = baseCollection.Where(bc => bc.DeletedAt == null).OrderByDescending(bc => bc.StartDate).FirstOrDefault();
        if (nextRecentActive != null)
        {
            nextRecentActive.EndDate = null;
        }
        return baseCollection.Where(bc => bc.DeletedAt == null).Count();
    }
}

var collectionFromDatabase = _dbContext.DerivedClass;

int total = collectionFromDatabase.DeleteActiveSingleMembershipAndOpenPrevious()

//(THE ABOVE CODE IS WORKING FINE)

I can then (outside of the extension method) save my database context, and the changes persist to the database. However, when I try to ADD a new item to a collection of a class derived from BaseClass, the addition does not occur in my original collection that gets passed to the extension method. For example:

public static int AddNewMembershipRecord(this ICollection<BaseClass> baseCollection, BaseClass newMembership)
{
    var activeMemberships = baseCollection
            .Where(mc =>
                mc.EndDate == null &&
                mc.DeletedAt == null
            );
    //Handle Existing Active Membership
    if (activeMemberships == null || activeMemberships.Count() != 1)
    {
        //Do Nothing
    }
    else
    {
        activeMemberships.First().EndDate = newMembership.StartDate;
    }

    //Add New Membership to Collection
    baseCollection.Add(newMembership);//This add does not get added to the collection that is passed to this method
    //return new total count
    return baseCollection.Where(col => col.DeletedAt == null).Count();
}

var collectionFromDatabase = _dbContext.DerivedClass;
DerivedClass newItem = new DerivedClass
{
    StartDate = DateTime.Now,
    EndDate = null,
    DeletedAt = null,
    Name = "Someone's Name"
}
int total = collectionFromDatabase.AddNewMembershipRecord(newItem);
//collectionFromDatabase at this point remains unchanged.  total will not change.

I've tried using both IEnumerable<BaseClass> and ICollection<BaseClass> as the parameter types (instead of using the .Add method, I'm using .Concat method if I'm using IEnumerable<BaseClass> as the parameter. I've also tried using various generic types in my extension method to convert between BaseClass and DerivedClass but have not had any luck with that either.

How can I add an item to the collection from the database context in my extension method, and have it persist to the collection that gets passed (and not remain scoped in my method)?

0

There are 0 best solutions below