将更改保存到Entity Framework 6中的数据库

时间:2014-09-09 10:43:16

标签: c# asp.net-mvc-5 entity-framework-6

我正在尝试使用Entity Framework 6数据库第一个数据库更新用户的状态,在MVC 5中,它们处于活动状态或非活动状态,但更改后的状态不会在数据库中更新。

但是,当我关闭程序并再次调试时,状态已在视图和数据库中更新。

为什么会以这种方式发生这种情况以及如何解决?

数据库更新代码:

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);
    }
}

修改 控制器:

//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 });
}

调用以下方法:

//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();
}

字典只保存数据库名称,如果你需要我能找到它的代码,那就完美无缺。

修改2

观点:

@{
    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>
            }
        } 

1 个答案:

答案 0 :(得分:1)

我遇到了类似的错误,您应该使用using语句来包装数据库上下文。

澄清尝试:

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