编辑:更新后,仍然需要帮助。带实体框架的WebAPI,代码优先:不生成插入对象的正确标识
问题是为什么不添加具有正确ID的用户?
我正在使用WebAPI,每当我通过Fiddler发送POST请求时,它都会向用户添加不正确的ID。
我有实体框架自动生成和递增我的POCO类(用户)的ID,而不是添加ID为1的用户,它现在行为,如果有数据库中的用户已经只是创建用户ID为例如16.
我检查了Visual Studios Server Explore中的表格,我右键单击表格,然后选择显示表格数据并没有任何内容。
我试着清理并重建解决方案,并检查表中是否有用户但没有。
这是在Web.config文件中我的连接字符串:
<add name="ConnectoDbContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=CONNECTO_f8dc97e46f8b49c2b825439607e89b59;Integrated Security=True;AttachDbFilename=|DataDirectory|\Connecto.mdf" />
我用代码优先的方法到一个新的数据库。
我也检查了Microsoft SQL Server 2012,SQL Server Management Studio,在那里我看到一个名为CWS.Models.ConnectoDbContext的数据库,所以我假设它是同一个数据库。不过,我必须承认我经历过不一致的情况,有时甚至会有旧桌子,尽管我的解决方案中再也没有它们。
在SQL Server Management Studio中,我已检查UserId是标识列。在Visual Studio中,我还检查了UserId列,它说,Is Identity: True
。
我在Visual Studio和SQL Server Management Studio中都运行了以下DBCC CHECKIDENT ("[User]", NORESEED);
。
SQL Server Management Studio中返回:
Checking identity information: current identity value 'NULL', current column value 'NULL'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
Visual Studio的回报:
Checking identity information: current identity value '16', current column value 'NULL'. DBCC execution completed. If DBCC printed error messages, contact your system administrator.
他们都返回不同的东西这使我相信,他们没有 “连接” 中,它是不一样的DB?
任何帮助,将不胜感激
/两次
DB中的ID列是标识列吗?如果您之前有数据,而您之前已取出数据,则数据库将保留先前生成的标识,并将从那里继续使用 – Psytronic 2013-04-26 19:40:54
@Psytronic是,它是标识列。我将如何解决这个问题? – brk 2013-04-26 19:46:30
你确定ef没有连接到不同的数据库吗? – 2013-04-26 20:00:47