4

I've started working, and learning, about Entity Framework. Previously, when working with data access I've always tried keeping it in a separate DLL, trying to separate my concerns and prevent data model classes being dragged into the presentation layer.

Now, when I create an EDMX file it goes into the main project, when I moved them and the associated code into a data DLL, whilst it compiled when I ran the solution an error was thrown in the Entities constructor or rather its base constructor in the ObjectContext class.

Is it possible to achieve this complete separation?

I saw this question which appears to be related but reading the comments it appears as though there is only a partial separation.

Edit This is the error I was getting:

Unable to load the specified metadata resource.

I've found this answer so I'll see if that solves my problem.

Community
  • 1
  • 1
Daniel Hollinrake
  • 1,768
  • 2
  • 19
  • 39
  • 2
    Clean separations are definitely possible, viz POCO's on one assembly, EDMX / context in another, and a client in a third. What exact error do you get? It might just be a matter of changing namespace, or the path to the edmx in the .tt file? – StuartLC Sep 18 '13 at 08:50
  • That's good to know. I rolled back my changes last night so can't get the exact error but the exception was thrown on this line public GlobalEntities() : base("name=GlobalEntities", "GlobalEntities") within the Global.Designer.cs class. I'm fairly certain the error was along the lines of it couldn't find something, which is what made me think there was an issue with it not being 'in context'. – Daniel Hollinrake Sep 18 '13 at 08:59
  • 1
    @StuartLC thanks for telling me it is possible. I've got there now. Ta. – Daniel Hollinrake Sep 18 '13 at 12:37

1 Answers1

6

Simply re-create the edmx file in the other assembly instead of copying it. Just know that any assembly/program which uses that assembly will need to have its app or web config file contain database connection string which has the same name as what was created in your edmx's app config file.

It is not uncommon to keep the data access EF files in a separate assembly or behind web services. Although it is a matter of preference whether to allow the POCOs (models) to be exposed directly or not.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 1
    Hiya, that's what I'm trying to do. I think there's some conflict due to me having copied it to begin with. I think I might be better off removing everything and starting again. This time adding the EDMX files to the data assembly. Thanks for your answer. – Daniel Hollinrake Sep 18 '13 at 12:10
  • @DanielHollinrake EF is picky in that. I always found that there are times where, simply deleting and redo-it are the best way to avoid bugs. Under the covers there are generated files and they can easily become out of sync and some nuance of movement maybe causing the flaky issue. – ΩmegaMan Sep 18 '13 at 12:30
  • Beginner's mistakes I guess and being overly cautious. I created one EDMS and copied the others across. Next time, I'll probably put them in the right place to begin with. Thanks for your help. – Daniel Hollinrake Sep 18 '13 at 12:36
  • @DanielHollinrake We all have done it, trust me...ultimately you learned and its working which is a good thing. – ΩmegaMan Sep 18 '13 at 12:40
  • Hello guys, same architecture I am planning to implement. but I am using MVC project as presentation layer. Since in presentation layer, I want to use data annotations around the entities (generated dll of separate project). I tried the partial class by name in my Model namespace as same as EF project but unfortunate. Also I tried MetadataType. Any another resolution ? – Red Swan Apr 30 '14 at 05:03