views:

137

answers:

3

I am incorporating DotNetOpenAuth to my asp.net website. I am new with Entity framework. Database.DataContext.AddToUser(user) located in LogingFrame.aspx.cs does not add a user into the database. User and AuthenticationToken records are filled correctly. The same code from template project, which points to the same database, works fine. Probably I missed something during incorporation. Could you please point what I have to check? Please let me know if you need any code to be provided.

A: 

Did you forget to call DataContext.SaveChanges()?

jfar
Good thing to check for generally, but not necessary for the project template since it calls SaveChanges for the user automatically at the close of an HTTP request.
Andrew Arnott
+1  A: 

...does not add a user into the database. User and AuthenticationToken records are filled correctly.

Your question seems to contradict itself, or I'm reading it wrong. It sounds like you're saying no user is added, but then a user is added.

Let me take a stab at it though and let you know how this project template works. A database transaction wraps all database changes within a single HTTP request. It's built into the RelyingPartyLogic assembly to have this behavior. Also, at the end of a successful HTTP request (one that didn't result in an unhandled exception) SaveChanges() is called and the transaction is committed. If an unhandled exception is thrown, the changes and transaction are rolled back. This helps to protect the integrity of your database.

A side-effect of this is, however, that if you're debugging through an "add a user" method and after you see AddToUser executed you jump to the users table to see if it was added, it won't be there because SaveChanges hasn't yet been called and the transaction hasn't been committed yet.

Per standard Entity Framework behavior, SaveChanges must be called for your changes to be persisted in the database, but as I said before, the RelyingPartyLogic library makes this call for you. But you may sometimes need to call SaveChanges yourself in your own code in order to (for example) get the ID of a newly added row. This works even within a transaction before committing it.

Hope that helps.

Andrew Arnott
A: 

Thank you for responds! They point me to right direction. The reason of issue was that I have not registered RelyingPartyLogic.Database in httpModules of web.config. That cause Application_EndRequest event of RelyingPartyLogic.Database were not registered and SaveChanges were not called.

Denis
With StackOverflow, for a response such as this you should use comments either on someone else's answer or your own question.
Andrew Arnott