Entity Framework Code First : Setting up One-To-One foreign key association using Annotations

One-to-one foreign key associations are not supported by Entitiy Framework. You must remove the foreign key and use shared primary keys (primary key of the dependent is its foreign key to the principal at the same time): public class StandardRack { public int Id {get;set} public StandardRelay StandardRelay {get;set} } public class StandardRelay { public … Read more

Entity Framework Database.SetInitializer simply not working

The database will only be created when you actually use the context. If you have overridden the Seed method in your initializer as follows: protected override void Seed(MyContext context){…} The Seed code will only run when you use an instance of MyContext. That is why it works when you use var ctx = new MyContext(); … Read more

How to update not every fields of an object using Entity Framework and EntityState.Modified

Let’s assume that you have a collection of the properties to be excluded: var excluded = new[] { “property1”, “property2” }; With EF5 on .NET 4.5 you can do this: var entry = context.Entry(obj); entry.State = EntityState.Modified; foreach (var name in excluded) { entry.Property(name).IsModified = false; } This uses a new feature of EF5 on … Read more

EF Code First DBContext and Transactions

Yes. SaveChanges uses transaction internally. Use TransactionScope to wrap multiple calls to SaveChanges Example: using(var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { // Do something context.SaveChanges(); // Do something else context.SaveChanges(); scope.Complete(); }

Organizationally, where should I put common queries when using Entity Framework Code First?

Looks like the repository pattern is solution for everything … Repository is not a silver bullet! I’m using the repository pattern with EF everyday because when I started my current project several months ago it looked like recommended solution. My conclusions: Repository makes interaction with EF much harder. Just browse questions related to EF tags … Read more

Entity Framework – Is there a way to automatically eager-load child entities without Include()?

No you cannot do that in mapping. Typical workaround is simple extension method: public static IQueryable<Car> BuildCar(this IQueryable<Car> query) { return query.Include(x => x.Wheels) .Include(x => x.Doors) .Include(x => x.Engine) .Include(x => x.Bumper) .Include(x => x.Windows); } Now every time you want to query Car with all relations you will just do: var query = … Read more

Better way to query a page of data and get total count in entity framework 4.1?

The following query will get the count and page results in one trip to the database, but if you check the SQL in LINQPad, you’ll see that it’s not very pretty. I can only imagine what it would look like for a more complex query. var query = ctx.People.Where (p => p.Name.StartsWith(“A”)); var page = … Read more