简体   繁体   English

尝试使用C#从ASP.net MVC 4查询SQL Server

[英]Trying to Query a SQL Server from ASP.net MVC 4 using C#

I am trying to use a query to find the last ID (entry) in the table called ContributorUser in the database FPTContributorUsers and then add in a new entry thus assigning it the next available ID. 我正在尝试使用查询在数据库FPTContributorUsers中查找名为ContributorUser的表中的最后一个ID(条目),然后添加一个新条目,从而为其分配下一个可用ID。

the below code allows me to add data to the table in the database however when I run it the ID (new entry to the table) shows as 0 and not 4. because I currently have three entries in my table 下面的代码允许我将数据添加到数据库中的表中,但是当我运行它时,ID(表的新条目)显示为0而不是4.因为我目前在表中有三个条目

[HttpPost]
public ActionResult AddContributor(ContributorUsers AddCont)
{
  if (AddCont.UserID == null)
  {
    throw new HttpException(404, "Please enter a valid RacfId");
  }
  else
  {
    FPTContributorUsers NewUser = new FPTContributorUsers();
    NewUser.UserID = AddCont.UserID;
    NewUser.ID = AddCont.ID;
    db.ContributorUsers.Add(NewUser);
    db.SaveChanges();
    return RedirectToAction("index");
  }
}  

Have you thought about just making that the primary key and having it auto increment so you don't need to determine what it is yourself? 您是否考虑过将其作为主键并使其自动增加,这样您就不需要确定它本身是什么? That's usually the best way to go about handling actual ID's in my experience. 根据我的经验,这通常是处理实际ID的最佳方式。

To do this in SQL Server follow these steps 要在SQL Server中执行此操作,请执行以下步骤

To do this in MySQL follow these steps 要在MySQL中执行此操作,请执行以下步骤

As a note, if you do this you will need to update your EF model. 请注意,如果您这样做,则需要更新EF模型。

The other way to do it if you can't edit the database is to use MAX() for that column which will return the highest ID value, then just add one to it, no EF model updating required. 如果你不能编辑数据库,另一种方法就是对该列使用MAX(),它将返回最高的ID值,然后只需添加一个,不需要更新EF模型。

Try removing the "NewUser.ID = AddCont.ID" line and wait to get the NewUser.ID until after "db.SaveChanges()" 尝试删除“NewUser.ID = AddCont.ID”行并等待获取NewUser.ID,直到“db.SaveChanges()”之后

else
  {
    FPTContributorUsers NewUser = new FPTContributorUsers();
    NewUser.UserID = AddCont.UserID;
    db.ContributorUsers.Add(NewUser);
    db.SaveChanges();
    return RedirectToAction("index");
  }

If the NewUser.ID property is an Identity Field in the database, it will not get populated until after the record is created during the SaveChanges() transaction commit. 如果NewUser.ID属性是数据库中的标识字段,则在SaveChanges()事务提交期间创建记录之后才会填充它。

在调用NewUser.ID之前尝试Reload()在SaveChanges()之后的数据库上下文,以便上下文是最新的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#ASP.NET MVC 5和SQL Server - C# ASP.NET MVC 5 and SQL Server 如何在C#asp.net中使用SqlDataReader对SQL Server查询使用参数,该参数是查询本身的一部分 - How to use a parameter for a SQL Server query that is part of the query itself using SqlDataReader in C# asp.net 如何从ASP.NET MVC C#中的SQL Server数据库流式传输mp3文件 - How to stream mp3 file from a SQL Server database in ASP.NET MVC C# ASP.NET MVC C#-从SQL Server数据库读取问题 - asp.net mvc c# - problem with reading from sql server database 从SQL Server或asp.net/C#(mvc 3)页面将数据导入excel - importing data to excel from SQL Server or asp.net/C# (mvc 3) page 如何使用 Asp.net MVC 和 C# 从 SQL Server 搜索基于纬度和经度的记录 - How to search Latitude and Longitude based record from SQL Server use Asp.net MVC and C# 在 C# 和 Z9E0DA8438E1E68A1C30FVCB78 中使用 ajax 从 SQL 服务器填充下拉列表 - Populating dropdown from SQL Server with ajax in C# and ASP.NET MVC 如何将 SQL Server 过程变量设置为从 C# ASP.NET MVC 程序输入的数据? - How to set SQL Server procedure variables to inputted data from C# ASP.NET MVC program? 使用SQL Server CE数据库慢速查询的C#/ ASP.NET网站 - C#/ASP.NET Web Site using SQL Server CE Database Slow Query 如何修改查询以使用ASP.NET C#与Sql Server获取每月的出勤报告 - how to modify the query to get monthly attendence report using asp.net c# with Sql Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM