将更改保存到实体框架6中的数据库中

本文关键字:数据库 框架 实体 保存 | 更新日期: 2023-09-27 18:21:08

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

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

为什么会发生这种情况,如何解决?

数据库更新代码:

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

将更改保存到实体框架6中的数据库中

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

澄清尝试:

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