简体   繁体   English

SQL Server实体框架插入不起作用

[英]SQL Server Entity Framework Insert Not Working

I'm using Entity Framework Code First and SQL Server in my application. 我在应用程序中使用Entity Framework Code First和SQL Server。 In one place of my code, i tried to simply insert data into the database. 在我的代码的一个地方,我试图将数据简单地插入数据库中。 I've got collection of objects like 我收集了像

public class DrivingStatistic
{
    public int Id { get; set; }
    public int CarId { get; set; }
    public virtual Car Car { get; set; }
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }

    public BLL.Consts.DrivingStatisticType Type { get; set; }
    public double Value { get; set; }
}

I add it to my object set by simply: 我可以简单地将其添加到我的对象集中:

foreach(var entity in data)
    _objectSet.Add(entity);
unitOfWork.SaveChanges();

Everything works fine on my local database, but on a remote server, nothing happens. 在我的本地数据库上一切正常,但是在远程服务器上什么也没有发生。 Almost nothing. 几乎没有。

I used SQL Server Profiler and saw, that insert command was created and executed correctly, here it is 我使用了SQL Server Profiler,看到插入命令已正确创建并执行,在这里

exec sp_executesql N'INSERT [dbo].[DrivingStatistics]([CarId], [DateFrom], 
  [DateTo], [Type], [Value])
  VALUES (@0, @1, @2, @3, @4)
  SELECT [Id]
  FROM [dbo].[DrivingStatistics]
  WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()',N'@0 int,@1 
  datetime2(7),@2 datetime2(7),@3 int,@4 float',@0=1000,@1='2017-08-25 
  07:00:00',@2='2017-08-25 08:00:00',@3=4,@4=0

Identity in my table was incremented, but no row was inserted. 表中的身份已增加,但未插入任何行。 I invoked this SQL command manually via Microsoft SQL Management Studio, and everything was successfully committed to Db, but Id was a bigger number than expected, that's I know, that Identity was previously incremented. 我通过Microsoft SQL Management Studio手动调用了此SQL命令,并且所有内容都已成功提交给Db,但Id的数量比预期的大,这就是我所知道的,Identity以前已增加。

Of course, there is no problem with inserting other entities in other parts of the code. 当然,在代码的其他部分插入其他实体也没有问题。

If anyone has seen such issue before, please advise me what could cause this problem. 如果以前有人遇到过此类问题,请告诉我可能导致此问题的原因。

Any help would be very appreciated :) 任何帮助将不胜感激:)

EDIT: 编辑:

  • CarId is the foreign key and it is the only one dependency in this table. CarId是外键,它是此表中唯一的一个依赖项。
  • DrivingStatisticType is an enum. DrivingStatisticType是一个枚举。
  • EntityFramework didn't throw any exception EntityFramework没有引发任何异常
  • Entity Framework is not trying to reinsert those rows. 实体框架不尝试重新插入这些行。
  • There is nothing in SQL Server Log about those inserts SQL Server日志中没有关于这些插入的任何内容

I make it to work by changing 我通过改变使它起作用

foreach(var entity in data)
    _objectSet.Add(entity);
unitOfWork.SaveChanges();

to

foreach(var entity in data)
{
    _objectSet.Add(entity);
    unitOfWork.SaveChanges();
}

But if anyone knows why in some situation it is not possible to add multiple rows and commit them together, please tell me. 但是,如果有人知道为什么在某些情况下无法添加多个行并将它们一起提交,请告诉我。 I'm really curious what Was going about here. 我真的很好奇这里会发生什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM