Entity Framework Join 3 Tables

I think it will be easier using syntax-based query: var entryPoint = (from ep in dbContext.tbl_EntryPoint join e in dbContext.tbl_Entry on ep.EID equals e.EID join t in dbContext.tbl_Title on e.TID equals t.TID where e.OwnerID == user.UID select new { UID = e.OwnerID, TID = e.TID, Title = t.Title, EID = e.EID }).Take(10); And you should … 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 4 – AddObject vs Attach

ObjectContext.AddObject and ObjectSet.AddObject: The AddObject method is for adding newly created objects that do not exist in the database. The entity will get an automatically generated temporary EntityKey and its EntityState will be set to Added. When SaveChanges is called, it will be clear to the EF that this entity needs to be inserted into … Read more

What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

So far, I know of these effects. Lazy Loading: Any virtual ICollections will be lazy-loaded unless you specifically mark them otherwise. More efficient change tracking. If you meet all the following requirements then your change tracking can use a more efficient method by hooking your virtual properties. From the link: To get change tracking proxies, … Read more

What’s the difference(s) between .ToList(), .AsEnumerable(), AsQueryable()?

There is a lot to say about this. Let me focus on AsEnumerable and AsQueryable and mention ToList() along the way. What do these methods do? AsEnumerable and AsQueryable cast or convert to IEnumerable or IQueryable, respectively. I say cast or convert with a reason: When the source object already implements the target interface, the … Read more

LINQ to Entities case sensitive comparison

That’s because you are using LINQ To Entities which is ultimately convert your Lambda expressions into SQL statements. That means the case sensitivity is at the mercy of your SQL Server which by default has SQL_Latin1_General_CP1_CI_AS Collation and that is NOT case sensitive. Using ObjectQuery.ToTraceString to see the generated SQL query that has been actually … Read more

Validation failed for one or more entities. See ‘EntityValidationErrors’ property for more details [duplicate]

To be honest I don’t know how to check the content of the validation errors. Visual Studio shows me that it’s an array with 8 objects, so 8 validation errors. Actually you should see the errors if you drill into that array in Visual studio during debug. But you can also catch the exception and … Read more