Loading foo using the same context , since adding a new bar with the corresponding foo will not lead to duplication. I assume that your real code uses two different contexts.
The only thing that needs to be changed in the code (which will not compile since foo is IQueryable<Foo> and not foo ) should materialize foo , for example:
var foo = (from f in context.Foos where f.FooId == 1 select f).Single();
In addition, the code snippet is fine.
source share