简体   繁体   中英

Save changes to a database in Entity Framework 6

I am trying to update the status of a user using Entity Framework 6 database first database, in MVC 5, where they are either active or inactive, but the changed status does not update in the database.

However, when I close the program and debug again the status has updated in the view and the database.

Why could this happening this way and how could it be solved?

database update code:

public void StatusChange(string userName)
{
    string currentStatus = amadeusUser.getUserStatus(userName);
    int userID = amadeusUser.getUserID(userName);
    if (currentStatus == "Active")
    {
        SaveStatus(1360, userID, userName);
    }
        else if (currentStatus == "Inactive")
    {
        SaveStatus(193, userID, userName);
    }
    }

private void SaveStatus(int status, int userID, string userName)
{        
    try
    {
        var rowChanger = amadeus.AmadeusUsers.Find(userID);
        rowChanger.Status = status;
        amadeus.SaveChanges();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

Edit Controller:

//The controller calls a interface that calles the correct database's class
public ActionResult ChangeStatus(string userName, string applicationName)
{
    foreach (KeyValuePair<Application.Applications, IRole> entry in Application.RoleMap)
    {
        if (entry.Key.ToString() == applicationName)
        {
            IRole role = entry.Value;
            role.StatusChange(userName);
        }
    }
    return RedirectToAction("UserDetails", new { userName = userName });
}

Calls the following method:

//The controller calls a interface that calles the correct database's class
public ActionResult UserDetails(string userName)
{
    Dictionary<string, string> statuses = new Dictionary<string, string>();

    foreach (KeyValuePair<Application.Applications, IUser> entry in Application.UserMap)
    {
            IUser user = entry.Value;
            statuses.Add(entry.Key.ToString(), user.getUserStatus(userName));
    }
    ViewBag.ApplicationStatuses = statuses;
    ViewBag.UserName = userName;

    return View();
}

The dictionary only holds the database names and that works perfectly, if you need the code I can find it.

Edit 2

The View:

@{
    ViewBag.Title = "User Details";
}

<h2>User Details</h2>

<p><b>@ViewBag.UserName</b></p>

<table class="table">
     <tr>
         <th>
             Application Name
         </th>
        <th>
           Status
        </th>
         <th>

         </th>

    </tr>

        @if (ViewBag.ApplicationStatuses.Count > 0)
        {
            @*Iterating Amadeus model using ViewBag *@
            foreach (var item in ViewBag.ApplicationStatuses)
            {
            <tr>
                <td>
                    @item.Key
                </td>
                <td>
                    @item.Value
                </td>
                <td>
                    @Html.ActionLink("Change Status", "ChangeStatus", "User", new { userName = ViewBag.userName, applicationName = item.Key }, new { @class = "enableDisable" })
                </td>
                <td>
                    @Html.ActionLink("View Permissions", "Roles", new { userID = ViewBag.UserName, applicationName = item.Key })*
                </td>
            </tr>
            }
        } 

I have had a similar error, You should use a using statement to wrap your database context.

To clarify try:

using(dbEntity amadeus = new dbEntities())
{
    if (entry.Key.ToString() == applicationName)
    {
        IRole role = entry.Value;
        role.StatusChange(userName);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM