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); }
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.
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.
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.
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.
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
Hi, David. Is this code compiled in a separate assembly?
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
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?
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.
I've used ADO.NET EF many times side-by-side with umbraco and never experienced something like this to be honest...
If you separate the classes into separate files, do you get the same issue (shouldn't make a difference, but I'm curious)?
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
is working on a reply...