Reducing Repositories to Aggregate Roots

You are allowed to have any method you want in your repository 🙂 In both of the cases you mention, it makes sense to return the user with phone list populated. Normally user object would not be fully populated with all the sub information (say all addresses, phone numbers) and we may have different methods … Read more

How should I remove all elements in a DbSet?

dbContext.Database.ExecuteSqlCommand(“delete from MyTable”); (No kidding.) The problem is that EF doesn’t support any batch commands and the only way to delete all entities in a set using no direct DML would be: foreach (var entity in dbContext.MyEntities) dbContext.MyEntities.Remove(entity); dbContext.SaveChanges(); Or maybe a litte bit cheaper to avoid loading full entities: foreach (var id in dbContext.MyEntities.Select(e … Read more

Using Include in Entity Framework 4 with lambda expressions

The RTM version of Entity Framework 4.1 actually includes extension methods in the EntityFramework.dll file, for eager loading with lambda through the Include function. Just include the DLL in your project and you should be able to write code like: var princesses1 = context.Princesses.Include(p => p.Unicorns).ToList(); Remember to add an Import/Using statement to include the … Read more

Why am I getting this error: No mapping specified for the following EntitySet/AssociationSet – Entity1?

This is because of the way EF4 works with model-first. When you first create a model-first model, it’s in a state that the SSDL does not exist. You can drag entities, associate them and so forth and yet, if you take a look at the SSDL on the EDMX file, you will see that none … 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

Entity framework self referencing loop detected [duplicate]

Well the correct answer for the default Json formater based on Json.net is to set ReferenceLoopHandling to Ignore. Just add this to the Application_Start in Global.asax: HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter .SerializerSettings .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; This is the correct way. It will ignore the reference pointing back to the object. Other responses focused in changing … Read more

Entity Framework – Add Navigation Property Manually

Yup – it’s not that straightforward. Here’s what you do: 1 – Right click on the designer, Add -> Association 2 – Setup the association and cardinalities (People *..1 Gender, People *..1 Race) 3 – Go into the Model Browser -> Associations 4 – Right click on your newly created associations, click Properties 5 – … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)