I've got a table "Expenses" that contains an FK value to an "ExpenseType" table. Right now, if I want to save a new expense, I first get the int value of the FK, do a lookup to the ExpenseType table, and assign the object to the expense object. For example:
//create new expense
var e = new Expense();
//lookup the associated expense type
var et = context.ExpenseTypeSet.First(e -> e.expenseTypeID == 10);
//set field values
e.expenseName = "Some name";
e.expenseType = et;
//save
context.Save();
Is there a way to create/insert a record without first being forced to do a lookup against the FK table? The lookup is not needed and forces extra database hits. I already know the FK ID... so is there a way to just set the ID and allow it to be happy?
Thanks in advance -
You can do this using the EntityReference too:
e.expenseTypeReference.EntityKey
= new EntityKey("MyContainer.expenseTypeSet", "expenseTypeID", 10);
I have a tips series that covers this and related issues:
The one you might want to look at in particular is this Tip 7 which talks about creating fake properties for FKs. You don't need to go that far, unless you want to, but the principles are useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With