views:

36

answers:

2

I do this to save new invoince in my Invoices database table:

                // insert invoice into EDM
                edmx.AddToInvoices(newinvoice);
                // save EDM changes to datastore
                edmx.SaveChanges();

I have a trigger on one of the columns that gets computed dynamically by the database. What is the 1) easiest way to get that value out of the database immediatelly after it changes, 2) What is the fastest way?

Thanks

A: 

Since the Entity Framework can't know anything about the trigger, you'd have to reload the entity with a new query. I strongly recommend finding a different solution, if possible. Triggers can be evil.

Dave Swersky
Well since you started this discussion, what are my other options for supporting autoincrement primary keys and their custom derivates (which I generate in my triggers) with Entity Framework? I am working with VS2010 RC2 so I'm pretty current when it comes to .NET framework version. Apparently, as I see it from the above example, nothing changes in Entity object after calling SaveChanges(), so they pretty much aren't supported.
mare
No, that's not right, @mare. If you set StoreGeneratedPattern (see my answer) then autoincrement works fine. However, the GUI designer can't know what your trigger does, so unlike an `IDENTITY` column, it won't pick this up automatically when you map.
Craig Stuntz
+1  A: 

You can either call Refresh:

MyEntities.Refresh(RefreshMode.StoreWins, someEntity);

...or configure the column in SSDL as store-generated if you never set it on the client.

Craig Stuntz
Craig, as usual, with the perfect answer. ;) Refresh worked.With regards to SSDL, it says: "Note This attribute is not currently generated by Edmgen.exe and must be added manually.". Do you know if this is going to change with the final version of .NET Framework 4.0?
mare
Are you using EdmGen at all? VS will certainly use `StoreGeneratedPattern` when it sees an identity column. So the EF and its tooling do support it; I don't know the particulars of EdmGen because I don't normally use it. You can get the source code for EdmGen2 if you want to enhance it. http://code.msdn.microsoft.com/EdmGen2
Craig Stuntz
No I'm not using it, I was under impression that VS uses it under the hood, but if it doesn't then it's fine.
mare
I don't think VS generates the EDMX via EdmGen, though I could be wrong. It may use it for code generation (in EF 1), though. But that's a different feature. At any rate, it's really easy to look and see if `StoreGeneratedPattern` is there.
Craig Stuntz