Generate POCO classes in different project to the project with Entity Framework model

Actually the T4 templates in EF 4.0 were designed with this scenario in mind 🙂 There are 2 templates: One for the Entities themselves (i.e. ModelName.tt) One for the ObjectContext (i.e. ModelName.Context.tt) You should put the ModelName.tt file in you POCO project, and just change the template to point to the EDMX file in the … Read more

How to declare one to one relationship using Entity Framework 4 Code First (POCO)

Three methods: A) Declare both classes with navigation properties to each other. Mark one of the tables (the dependent table) with the ForeignKey attribute on its Primary Key. EF infers 1-to-1 from this: public class AppUser { public int Id { get; set; } public string Username { get; set; } public OpenIdInfo OpenIdInfo { … Read more

Entity Framework 4 / POCO – Where to start? [closed]

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

How to add validation to my POCO(template) classes

You can’t add it directly (unless you modify T4 template to create them for you) but you can try to use trick introduced in ASP.NET dynamic data. All POCO classes are defined as partial. So lets define your partial part: using System.ComponentModel.DataAnnotations; [MetadataType(typeof(MyClassMetadata))] public partial class MyClass { private class MyClassMetadata { [Required] public object … Read more

Update relationships when saving changes of EF4 POCO objects

Let’s try it this way: Attach BlogPost to context. After attaching object to context the state of the object, all related objects and all relations is set to Unchanged. Use context.ObjectStateManager.ChangeObjectState to set your BlogPost to Modified Iterate through Tag collection Use context.ObjectStateManager.ChangeRelationshipState to set state for relation between current Tag and BlogPost. SaveChanges Edit: … Read more

Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

Here is a working example. Keypoints are: Declaration of Accounts Use of JsonProperty attribute . using (WebClient wc = new WebClient()) { var json = wc.DownloadString(“http://coderwall.com/mdeiters.json”); var user = JsonConvert.DeserializeObject<User>(json); } – public class User { /// <summary> /// A User’s username. eg: “sergiotapia, mrkibbles, matumbo” /// </summary> [JsonProperty(“username”)] public string Username { get; set; … Read more

‘POCO’ definition

“Plain Old C# Object” Just a normal class, no attributes describing infrastructure concerns or other responsibilities that your domain objects shouldn’t have. EDIT – as other answers have stated, it is technically “Plain Old CLR Object” but I, like David Arno comments, prefer “Plain Old Class Object” to avoid ties to specific languages or technologies. … Read more

Plain Old CLR Object vs Data Transfer Object

A POCO follows the rules of OOP. It should (but doesn’t have to) have state and behavior. POCO comes from POJO, coined by Martin Fowler [anecdote here]. He used the term POJO as a way to make it more sexy to reject the framework heavy EJB implementations. POCO should be used in the same context … Read more