Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David F. Hill 122 posts 242 karma points
    Jan 03, 2012 @ 02:28
    David F. Hill
    0

    Entity Framework prevents Document.BeforePublish from firing

    Hello Umbraco Colleagues,
    I've encountered one of the strangest things I've seen with Umbraco to date:
    When a class inherited from "DbContext" (Entity Framework 4.1) exists, the Umbraco Document.BeforePublish stops firing.

    Run this simple code with and without the "DbContext" and see if you replicate the problem.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using umbraco;
    using umbraco.cms.businesslogic.web;
    using umbraco.BusinessLogic;

    using System.Data.Entity;

    namespace TestDb
    {
        public class TestEntities : DbContext
        {
        }
    }

    namespace TestEvent
    {
        public class TestEvent : umbraco.BusinessLogic.ApplicationBase
        {
            public TestEvent()
            {
                Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
            }

            void Document_BeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (sender.ContentType.Alias == "PublicPage")
                {
                    sender.getProperty("bodyText").Value += "|" + DateTime.Now.ToString() + "|";
                    sender.Save();
                }
            }
        }
    }

    Without DbContext, the event handler fires as expected. With DbContext, it does not.

    I have been unable figure out why this would be the case. Namespace has no effect.
    It is preventing a custom event handler from working when using EF 4.1. I've tried it in Umbraco 4.7.0 and 4.7.1
    If anyone has a solution, please let me know.

    Thanks,

    David Hill

  • Rodion Novoselov 694 posts 859 karma points
    Jan 03, 2012 @ 03:11
    Rodion Novoselov
    0

    Hi, David. Is this code compiled in a separate assembly?

  • David F. Hill 122 posts 242 karma points
    Jan 03, 2012 @ 03:40
    David F. Hill
    0

    Hi Rodion,

    No it isn't. Funny you should ask because I am currently trying it out by compiling it in a separate dll.

    What does this need to be in a separate assembly when it's in a separate namespace?

    Edit: I meant - Why does this need to be compiled in a separate assembly?

    Thanks, David

  • Rodion Novoselov 694 posts 859 karma points
    Jan 03, 2012 @ 05:20
    Rodion Novoselov
    0

    It's strange. I have tried your code placing it inside App_Code and it works ok. I used EF 4.2. What version of EF do you have?

  • Rodion Novoselov 694 posts 859 karma points
    Jan 03, 2012 @ 05:30
    Rodion Novoselov
    0

    Anyway, I think there's definitely something wrong with loading a DLL where your code resides, since all classes derived from ApplicationBase are instantiated by loading a certain set of dlls and looking for classes that implement IApplication inside those dlls.

    Also you can inspect the log for messages "Error loading IApplication: bla-bla-bla" for investigation.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Jan 03, 2012 @ 09:31
    Stefan Kip
    0

    I've used ADO.NET EF many times side-by-side with umbraco and never experienced something like this to be honest...

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Jan 04, 2012 @ 15:27
    Tim
    0

    If you separate the classes into separate files, do you get the same issue (shouldn't make a difference, but I'm curious)?

  • David F. Hill 122 posts 242 karma points
    Jan 04, 2012 @ 18:32
    David F. Hill
    0

    Hi Tim,

    I didn't think it would make any difference if they were compiled in the same file or separate files - but it does!

    In fact, compiling them in separate files is how I got around the problem.

    So here is what I've figured out so far:  Compiled into a single assembly, the mere presence of a class derived from DbContext prevents the custom event handler for Umbraco Document.BeforePublish (in another class, in another namespace) from firing. Compiled into two separate assemblies, each class works as expected.

    I have no explanation for why..

    David

Please Sign in or register to post replies

Write your reply to:

Draft