0
我试图用LINQ在我的数据库中插入一个对象。我的主键是自动增量。我是否需要在代码中增加id或Entity Framework可以做到这一点?实体框架LINQ插入
我有我的DbContext:
TravelAgencyEntities db = new TravelAgencyEntities();
我试图插入用户对象:
User newUser = new User();
newUser.FirstName = firstName.Text.ToString();
newUser.LastName = lastName.Text.ToString();
newUser.Email = email.Text.ToString();
newUser.Password = password.Text.ToString();
db.Users.Add(newUser);
db.SaveChanges();
它返回一个例外:
{"An error occurred while updating the entries. See the inner exception for details."}
我认为这是错误因为我没有在我的对象中设置id,但它应该是自动增量。有谁知道这个问题?
内ERROR:
Cannot insert explicit value for identity column in table 'User' when IDENTITY_INSERT is set to OFF
内部例外说什么? – juharr
那么...... InnerException是什么? –
+ \t \t InnerException \t {“当IDENTITY_INSERT设置为OFF时,无法在表'User'中插入标识列的显式值。”} \t System.Exception {System.Data.SqlClient.SqlException} – user6824563