Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to separate logic from controller in an MVC Entity Framework app - What is happening here?
    primarykey
    data
    text
    <p>Based on my <a href="https://stackoverflow.com/questions/7822869/mvc-security-problems-in-my-application-best-strategy-for-separating-logic">last question</a>, I have tried to separate the business logic from my controller completely.</p> <p>This however has left a problem which I understand why, but not how to fix.... And, I do not understand why it is doing what is doing.</p> <p>In my controller, I had the following:</p> <pre><code>public User GetCurrentUser() { User user = db.Users.SingleOrDefault(x =&gt; x.UserName == User.Identity.Name); return user; } </code></pre> <p>I now know about <code>[NonAction]</code> which fixes the security concern - however, I know this doesn't follow best practices of not having any non controller stuff in a controller. For this reason, I moved to a new class and modified it to the following:</p> <pre><code>public User GetCurrentUser(string name) { User user = db.Users.SingleOrDefault(x =&gt; x.UserName == name); return user; } </code></pre> <p>I have an edit method which before simply set various fields in the user object, then called <code>db.SaveChanges()</code>. This however is now causing issues - I believe it is due to calling the command on a <code>db</code> object that doesn't actually have the object loaded. </p> <p>But, the part that I really do not understand is when I am redirected back to the home page and perform <code>GetCurrentUser()</code> again, I am presented with the edited details I changed... These are not stored in the database and it is only when I restart the application it goes back to the database results.</p> <p>I am finding this very confusing! What is happening here and where are the object being stored?</p> <p>And, how do I fix it? I have tried making the new Class's db function public and calling it's <code>SaveChanges()</code> method, but, this is resulting in the same problem - data that is not being saved to the database.</p> <p>Anyway, quite frankly, I really liked calling it via just <code>GetCurrentUser()</code>, I wanted this due to the fact I wanted to change the way the user was loaded in the future - but, now that I have to call it via <code>GetCurrentUser(User.Identity.Name)</code>, and make other modifications, I think it wouldn't be that much harder to just skip on the method and call the Lambda query directly instead... It just seems it will save a lot of trouble.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload