Entity Framework 5 deep copy/clone of an entity

One cheap easy way of cloning an entity is to do something like this: var originalEntity = Context.MySet.AsNoTracking() .FirstOrDefault(e => e.Id == 1); Context.MySet.Add(originalEntity); Context.SaveChanges(); the trick here is AsNoTracking() – when you load an entity like this, your context do not know about it and when you call SaveChanges, it will treat it like … Read more

How to “warm-up” Entity Framework? When does it get “cold”?

What would be the best approach to have high availability on my Entity Framework at anytime? You can go for a mix of pregenerated views and static compiled queries. Static CompiledQuerys are good because they’re quick and easy to write and help increase performance. However with EF5 it isn’t necessary to compile all your queries … Read more

Get return value from stored procedure

I found it! I can read the return value with an output parameter that has to be used in this way: // define a new output parameter var returnCode = new SqlParameter(); returnCode.ParameterName = “@ReturnCode”; returnCode.SqlDbType = SqlDbType.Int; returnCode.Direction = ParameterDirection.Output; // assign the return code to the new output parameter and pass it to … Read more

How do I enable EF migrations for multiple contexts to separate databases?

The 2nd call to Enable-Migrations is failing because the Configuration.cs file already exists. If you rename that class and file, you should be able to run that 2nd Enable-Migrations, which will create another Configuration.cs. You will then need to specify which configuration you want to use when updating the databases. Update-Database -ConfigurationTypeName MyRenamedConfiguration

Batch update/delete EF5

There are two open source projects allowing this: EntityFramework.Extended and Entity Framework Extensions. You can also check discussion about bulk updates on EF’s codeplex site. Inserting 100k records through EF is in the first place wrong application architecture. You should choose different lightweight technology for data imports. Even EF’s internal operation with such big record … Read more

What are Independent Associations and Foreign Key Associations? [duplicate]

Just my opinions about WHY to use independent or foreign key associations: Independent associations Pros: This is correct way to go in object oriented world. In object oriented world we are using references inside our aggregates, not some magic keys. Cons: With pure POCO you don’t know if principal relation is really NULL or just … Read more

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

Could not load file or assembly Microsoft.SqlServer.management.sdk.sfc version 11.0.0.0

Problem: (Sql server 2014) This issue happens when assembly Microsoft.SqlServer.management.sdk.sfc version 12.0.0.0 not found by visual studio. Solution: just go to http://www.microsoft.com/en-us/download/details.aspx?id=42295 and download: ENU\x64\SharedManagementObjects.msi for X64 OS or ENU\x86\SharedManagementObjects.msi for X86 OS, then install it, and restart visual studio. PS: You may need install DB2OLEDBV5_x64.msi or DB2OLEDBV5_x86.msi too. Problem: (Sql server 2012) This issue … 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