Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when Triggers added on both Parent and Child class #1

Closed
ConradPoohs opened this issue Feb 15, 2015 · 3 comments
Closed

Exception when Triggers added on both Parent and Child class #1

ConradPoohs opened this issue Feb 15, 2015 · 3 comments
Assignees

Comments

@ConradPoohs
Copy link

Trying to add Triggers in both a parent (CreatedEntity) and child (ModifiedCreatedEntity) class, but I keep hitting an InvalidCastException (details below) on context.SaveChanges()

Moving both the triggers to either the parent or child class resolves the problem, but isn't as nice design-wise, since I would like some entity classes to inherit from the parent (with its properties and associated triggers) and other entity classes to inherit from the child (with both the parent and child properties and triggers). Any help or advice would be greatly appreciated.

Sample Code:

using System;
using System.Data.Entity;
using EntityFramework.Triggers;

class CreatedEntity : ITriggerable
{
    public CreatedEntity()
    {
        this.Triggers().Inserting += entry => { entry.Entity.InsertedDateTime = DateTime.Now; };
    }
    public DateTime InsertedDateTime { get; set; }

}

class ModifiedCreatedEntity : CreatedEntity
{
    public ModifiedCreatedEntity()
    {
        //this.Triggers().Inserting += entry => { entry.Entity.InsertedDateTime = DateTime.Now; };
        this.Triggers().Inserting += entry => { entry.Entity.ModifiedDateTime = DateTime.Now; };
        this.Triggers().Updating += entry => { entry.Entity.ModifiedDateTime = DateTime.Now; };
    }
    public Int64 Id { get; protected set; }
    public DateTime ModifiedDateTime { get; set; }

}

class Context : DbContext
{
    public DbSet<ModifiedCreatedEntity> Entities { get; set; }

    public override Int32 SaveChanges() { return this.SaveChangesWithTriggers(base.SaveChanges); }
}

class Program
{


    static void Main(string[] args)
    {
        using (var context = new Context())
        {
            try
            {
                var entity = new ModifiedCreatedEntity();
                context.Entities.Add(entity);
                context.SaveChanges();
            }
            finally
            {
                context.Database.Delete();
            }
        }
    }
}

Exception:

System.InvalidCastException was unhandled
  HResult=-2147467262
  Message=Unable to cast object of type 'EntityFramework.Triggers.Triggers`1[CreatedEntity]' to type 'EntityFramework.Triggers.Triggers`1[ModifiedCreatedEntity]'.
  Source=EntityFramework.Triggers
  StackTrace:
       at EntityFramework.Triggers.Extensions.Triggers[TTriggerable](TTriggerable triggerable) in c:\Development\EntityFramework.Triggers\EntityFramework.Triggers\Extensions.cs:line 27
       at ModifiedCreatedEntity..ctor() in c:\Users\user\Documents\Visual Studio 2013\Projects\TriggerTestApp\TriggerTestApp\Program.cs:line 20
       at Program.Main(String[] args) in c:\Users\user\Documents\Visual Studio 2013\Projects\TriggerTestApp\TriggerTestApp\Program.cs:line 45
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
@NickStrupat
Copy link
Owner

Thanks for the input.

I have some code that does this same thing, and it seems to be broken with this version which means there was a regression. I designed it to handle this situation, and I've obviously overlooked something. I'm looking at the code now.

NickStrupat added a commit that referenced this issue Feb 16, 2015
Added a test to make sure base class triggers don't break again
@NickStrupat
Copy link
Owner

@ConradPoohs I have pushed a fix to the master branch, as well as the appropriate test to make sure it doesn't break base class triggers again. Please do a pull on your clone or update your NuGet package. Enjoy!

@NickStrupat NickStrupat self-assigned this Feb 16, 2015
@NickStrupat NickStrupat reopened this Feb 16, 2015
@ConradPoohs
Copy link
Author

Got the updated package and the inherited triggers exactly as designed now. Thank you so much for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants