Database Reference
In-Depth Information
EF has the ability to update the tables to match any changes in the mapped models and
even update the models from changes done to the tables. This is done to keep the mapping
of the tables to entities synced.
Entity Framework uses Fluent API's lambda expressions, as does NServiceBus; see
ht-
tp://msdn.microsoft.com/en-us/data/jj591620.aspx
for more on this topic of Entity Frame-
work.
Here's a snippet of Entity Framework code where we get the database context for the
model,
PayQueueEntities
. We get a collection of all the rows in the
Payments
table called
payment_rows
. We exercise the lambda
Where
clause to retrieve the first
row that has any primary of the message that we will update the database from; if none are
found, we add the record as follows:
using (var context = new PayQueueEntities())
{
// Get the payment rows
var payment_rows = context.Payments;
// Fluent API, check to see if there already is a
payment with this EventId (PK)
var payment = payment_rows.Where(x => (x.EventId
== details.EventId)).FirstOrDefault();
/***
* If no payment in rows
* Add row
* Otherwise update row
* *****/