4

For a school project I am building an application that has to be splitted in multiple projects.

The projects that will be made are class libraries, a wcf service and a asp.net MVC4 application.

The class libraries will be a state library, a stateless library and a datalogic library.

Ideally entity framework should be in the datalogic library, the generated models should be placed in the state library and the classes in stateless contain static methods to save those models.

However I can't seem to get the models generated by Entity Framework (v5) to another class library (State). Is there some way to do this? Or do I think wrong, should I use another architecture?

This problem is solved: Entity Framework 5 and Visual Studio 2012 POCO Classes in Different Project

Advice on architecture is still welcome:

I am willing to change this structure, if mine is proven wrong or less efficient as the one adviced.

(Other information I may need to provide is that the WCF service will pass through the methods of the stateless library and the ASP.Net MVC4 application is using those methods to get the models and some controller functionality)

Community
  • 1
  • 1
Nick N.
  • 12,902
  • 7
  • 57
  • 75
  • Are you adding references to the various projects as necessary? – Kenneth K. Mar 06 '13 at 19:19
  • 1
    We do a similar thing to this but use CodeFirst EF not db/modelfirst – undefined Mar 06 '13 at 19:19
  • @Luke McGregor I don't have the option to use code first since the database is already there. I could but then I can't use the benefits of autogenerating the models. What do you think? – Nick N. Mar 06 '13 at 19:22
  • @Kenneth K. I am adding the references, but the problem is the models are under a tt file they are not movable at the moment. – Nick N. Mar 06 '13 at 19:22
  • Thanks for the quick replies btw:) – Nick N. Mar 06 '13 at 19:23
  • 1
    Are you using the EF DbContext POCO Generator? It's then reasonably straight forward to have the .edmx context and poco models in different assemblies - http://msdn.microsoft.com/en-US/data/jj206878 – Neil Thompson Mar 06 '13 at 19:35
  • I am not using the POCO generator, as the models generated by Entity Framework are already quite POCO. However I am checking this out right now, maybe I should use it. But then again, what will happen to my existing models? – Nick N. Mar 06 '13 at 19:38
  • This is probably a namespace issue. Add the appropriate `using` method to the top of your code in the State library. – Bobson Mar 06 '13 at 19:41
  • @Bobson It is not a namespace issue, it's a call for advice. I would like to copy created ".cs" files under the tt into a separate project. But maybe I am just thinking wrong. How do you do this? – Nick N. Mar 06 '13 at 19:45
  • 1
    having a database already doesnt prevent you using codefirst. Personally i handstich POCO classes based on the database and use the modelbuilder if i want to represent it differently in the model. However you can also use the CodeFirst Generator to produce a codefirst model from an existing db. – undefined Mar 06 '13 at 19:46
  • all three "libraries" are in one project? – EdmundYeung99 Mar 06 '13 at 19:47
  • 1
    Using database first, or code first, is a matter of preference and isn't really the issue here IMO. Using POCOs should make it easy to separate the models and context as @NeilThompson suggested – EdmundYeung99 Mar 06 '13 at 19:51
  • @Luke McGregor Thanks Luke, I am going to try the CodeFirst Generator, is it also able to regenerate when I make changes to the code/database either (doesn't matter where I have to make the changes actually) – Nick N. Mar 06 '13 at 19:51
  • @EdmundYeung99 I will also try those Poco's but I prefer the code first style with its annotations and stuff. – Nick N. Mar 06 '13 at 19:53
  • http://stackoverflow.com/questions/12375090/entity-framework-5-and-visual-studio-2012-poco-classes-in-different-project - Problem and Answer explained very well here, thanks all, I had to look better! Any advice about my architecture is still welcome though. – Nick N. Mar 06 '13 at 19:55

1 Answers1

4

(So you can mark an answer)

Are you using the EF DbContext POCO Generator?

It's then reasonably straight forward to have the .edmx context and poco models in different assemblies - msdn.microsoft.com/en-US/data/jj206878

As you discovered - this post on Entity Framework 5 and Visual Studio 2012 POCO Classes in Different Project explains the procedure well.

Julie Lerman also has downloadable solutions from her books & courses that show POCo T4 templates and lots of other good practices

Community
  • 1
  • 1
Neil Thompson
  • 6,356
  • 2
  • 30
  • 53