183

I've been programming for a while and have used LINQ-To-SQL and LINQ-To-Entities before (although when using entities it has been on a Entity/Table 1-1 relationship - ie not much different than L2SQL)

I've been doing a lot of reading about Inversion of Control, Unit of Work, POCO and repository patterns and would like to use this methodology in my new applications.

Where I'm struggling is finding a clear, concise beginners guide for EF4 which doesn't assume knowledge of EF1.

The specific questions I need answered are:

Code first / model first? Pros/cons in regards to EF4 (ie what happens if I do code first, change the code at a later date and need to regenerate my DB model - Does the data get preserved and transformed or dropped?)

Assuming I'm going code-first (I'd like to see how EF4 converts that to a DB schema) how do I actually get started? Quite often I've seen articles with entity diagrams stating "So this is my entity model, now I'm going to ..." - Unfortunately, I'm unclear if they're created the model in the designer, saved it to generate code then stopped any further auto-code generation -or- They've coded (POCO)? classes and the somehow imported them into the deisgner view?

I suppose what I really need is an understanding of where the "magic" comes from and how to add it myself if I'm not just generating an EF model directly from a DB.

I'm aware the question is a little vague but I don't know what I don't know - So any input / correction / clarification appreciated.

Needless to say, I don't expect anyone to sit here and teach me EF - I'd just like some good tutorials/forums/blogs/etc. for complete entity newbies

Basic
  • 26,321
  • 24
  • 115
  • 201
  • 3
    be really REALLY careful with the lifetime of your connections: http://bit.ly/fi83NV It's something you should really be aware of when abstracting contexts into repositories. It could appear to be working but actually slowly clocking up more and more open connections – BritishDeveloper Mar 17 '11 at 15:28
  • @BRitishDeveloper - Very good advice. This did actually catch us out but in the opposite way - We were using an IoC container to retrieve repositories and had an issue where the context assigned to the repository would close the connection after a length of time but wouldn't get flagged as disposed/similar. We eventually extended the context ourselves with an IsDisposed() that checked bith the usual disposal state and the connection state allowing us to build another if required. – Basic Mar 17 '11 at 18:39
  • Another handy tip is that when getting a new context, objects associated with the old context won't have the appropriate change tracking and will cause context mis-match issues - So if you've got a long-running app and change context mid-execution, you need to re-retrieve all your entities. To make it more interesting, we've actually had to have 2 running side-by-side at times and ended up writing some code to map between the 2 nicely... – Basic Mar 17 '11 at 18:40
  • 1
    @Basiclife I ran into that same problem :) I've been meaning to write up my thoughts about updating detached entities for a while and you've just encouraged me to do just that: http://www.britishdeveloper.co.uk/2011/03/how-to-update-detached-entity-in-entity.html – BritishDeveloper Mar 18 '11 at 12:09

12 Answers12

56

These articles might be of interest...the series really gets into the advantages and disadvantages of a POCO approach.

Link

Link

Link

In these articles the author mentions future articles that describe best practices in implementing Repository and Unit of Work patterns, but I can't find them. These articles are well written and I'd like to read more from this author.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
KellySandwiches
  • 929
  • 9
  • 10
  • 2
    As someone already comfortable with Entity Framework using the designer, this was a great intro to POCO. – nathanchere Sep 28 '10 at 02:54
  • 1
    If your looking for the Unit of Work follow up it's located at http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx – Mike Apr 25 '12 at 16:31
11

I've come across this: http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx

Which gives you step-by-step for code first. It does require the CTP 3 for EF4 (Download linked from that article).

This is pretty close to what I was after (although ideally a solution that doesn't rely on a CTP would've been preferable)

Basic
  • 26,321
  • 24
  • 115
  • 201
7

I do recommend that you take a half hour or so and generate a stable EF1.0 model in your current VS. That will get you a long way towards understanding the metaphors and concepts of EF 4.0. Just whip up a simple Customer, Products and Orders db...I recommend doing your own and not using Northwind.

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
4

This is a great question, but tough to keep up to date as Entity Framework continues to mature. Probably the best place to start that will stay up to date into the future is Microsoft's EF page.

A few other links I found helpful while Googling (focused on Code First):

Dan
  • 573
  • 6
  • 9
3

You can take Lerman's book or something simplier like "Pro linq object-relational mapping". All concepts are still the same with POCO, except that now you should disable code generation and map directly to your model in edmx csdl (or create your own POCO generator). All mapping principles are the same also. Anyhow in run time you are working with proxy which is derived from your POCO object so you should concern about interception support (virtualization of your POCO properties).

Voice
  • 1,547
  • 16
  • 31
3

There are also these tutorials:

slang
  • 626
  • 7
  • 26
Daniel
  • 8,133
  • 5
  • 36
  • 51
  • His project structure looks just like an old nHibernate based project I was working on. Cept for all the WCF jazz, which I am keen on refreshing myself on. Solid link. – Merritt Aug 25 '10 at 20:18
2

Here's a walkthrough on the POCO Template for the Entity Framework that looked pretty good. You might also want to check out the ADO.NET team blog. If you want to start at the beginning(EF v1.0) as a base for your EF knowledge, I found Julia Lerman's Programming Entity Framework book very complete.

DaveB
  • 9,470
  • 4
  • 39
  • 66
  • Thanks - I hadn't seen the book but I've read both the links provided. The Template walkthrough is useful in explaining how additional functionality can be added to POCO objects once they're defined (eg Lazy loading) but (and I may be missing something obvious here) it doesn't actually explain how to get started (ie simply creating a class as specified doesn't make it an entity nor associate it with a model) I've had a similar experience with the blog. I will consider getting the book though - It looks promising - Thank you. – Basic Mar 19 '10 at 18:46
  • 2
    Regarding the book of Julia Lerman it's worth to be mentioned that she is working on a second edition covering EF4: http://learnentityframework.com/LearnEntityFramework/book/here-i-go-again-programming-entity-framework-2nd-edition-for-ef4/ . I remember that I've read somewhere that the planned publish date is in May this year but I can't find the source anymore. Also I just found this site: http://www.nakedobjects.net/home/index2.shtml – Slauma Mar 19 '10 at 19:24
  • Slauma, the link you provided looked ike exactly what I need - except it's using a 3rd party "Naked Obects" library which seems to be obfuscating the complexity somehow - For a minute, I thought you'd cracked it – Basic Mar 20 '10 at 12:25
2

Dont have the rep to comment on an answer, but here is a follow up to Jinkinz answer:

Using Repository and Unit of Work patterns with Entity Framework 4.0:

http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

woggles
  • 7,444
  • 12
  • 70
  • 130
1

If you are going to use disconnected cenarios, I recommend you to read Julie Lerman book: "Programming DbContext", in special Chapter 4.

I found a lot of examples in blogs, etc, but almost all of them are about connected cenarios.

I'm starting too. and these book helped me a lot. By the way, I bought her three books.

Rodolfo
  • 61
  • 8
1

Julia Lerman has a nice series of introductory videos, about 10 minutes each. They're introductory, but there are plenty of practical tips that get some potential learning roadblocks out of the way. I especially liked her demonstration of watching the actual SQL go by using SQL Server Profiler.

David Pope
  • 6,457
  • 2
  • 35
  • 45
0

Wow, lots of answers. How about an example which contains a tweaked version of T4 templates that generate POCO + interfaces + repositories altogether?

https://entityinterfacegenerator.codeplex.com

Believe2014
  • 3,894
  • 2
  • 26
  • 46
  • Interesting and handy for testing repositries/contexts, but why would you need to abstract the entities themselves? By definition they shouldn't have any functional code inside them. – Basic May 11 '14 at 17:51
  • You are correct. Mostly, people won't need to have separate interfaces. But it does help people who wants to solve circular references and want to share the interfaces, not the actual classes, with a third party. This will help a lot if your company needs to pass an audit with 3rd party integration which requires no detail implementation in sharing. – Believe2014 May 12 '14 at 04:40
0

I was looking for the same answers and came across http://www.dotnetcurry.com/ShowArticle.aspx?ID=599

At the end of the link, there are few links which will get you rolling with Entity Framework 4.

Hope this helps

DotNetInfo
  • 3,344
  • 6
  • 33
  • 39