我正在尝试通过Entity Framework 6.1.3向MS Sql Server数据库插入约50,000行数据,但执行时间太长。我按照这个答案的方法禁用了AutoDetectChangesEnabled,并在添加每1000个实体后调用了SaveChanges。但仍需要大约7-8分钟的时间。我已在远程服务器和本地服务器上尝试过,没有太大的区别。我不认为这是正常的。我是否忘记了什么?以下是我的代码:
static void Main(string[] args)
{
var personCount = 50000;
var personList = new List<Person>();
var random = new Random();
for (int i = 0; i < personCount; i++)
{
personList.Add(new Person
{
CreateDate = DateTime.Now,
DateOfBirth = DateTime.Now,
FirstName = "Name",
IsActive = true,
IsDeleted = false,
LastName = "Surname",
PhoneNumber = "01234567890",
PlaceOfBirth = "Trabzon",
Value0 = random.NextDouble(),
Value1 = random.Next(),
Value10 = random.NextDouble(),
Value2 = random.Next(),
Value3 = random.Next(),
Value4 = random.Next(),
Value5 = random.Next(),
Value6 = "Value6",
Value7 = "Value7",
Value8 = "Value8",
Value9 = random.NextDouble()
});
}
MyDbContext context = null;
try
{
context = new MyDbContext();
context.Configuration.AutoDetectChangesEnabled = false;
int count = 0;
foreach (var entityToInsert in personList)
{
++count;
context = AddToContext(context, entityToInsert, count, 1000, true);
}
context.SaveChanges();
}
finally
{
if (context != null)
context.Dispose();
}
}
private static MyDbContext AddToContext(MyDbContext context, Person entity, int count, int commitCount, bool recreateContext)
{
context.Set<Person>().Add(entity);
if (count % commitCount == 0)
{
context.SaveChanges();
if (recreateContext)
{
context.Dispose();
context = new MyDbContext();
context.Configuration.AutoDetectChangesEnabled = false;
}
}
return context;
}
人类:
public class Person
{
public int Id { get; set; }
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
[MaxLength(50)]
public string PlaceOfBirth { get; set; }
[MaxLength(15)]
public string PhoneNumber { get; set; }
public bool IsActive { get; set; }
public DateTime CreateDate { get; set; }
public int Value1 { get; set; }
public int Value2 { get; set; }
public int Value3 { get; set; }
public int Value4 { get; set; }
public int Value5 { get; set; }
[MaxLength(50)]
public string Value6 { get; set; }
[MaxLength(50)]
public string Value7 { get; set; }
[MaxLength(50)]
public string Value8 { get; set; }
public double Value9 { get; set; }
public double Value10 { get; set; }
public double Value0 { get; set; }
public bool IsDeleted { get; set; }
}
来自分析器的查询跟踪:
exec sp_executesql N'INSERT [dbo].[Person]([FirstName], [LastName], [DateOfBirth], [PlaceOfBirth], [PhoneNumber], [IsActive], [CreateDate], [Value1], [Value2], [Value3], [Value4], [Value5], [Value6], [Value7], [Value8], [Value9], [Value10], [Value0], [IsDeleted])
VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18)
SELECT [Id]
FROM [dbo].[Person]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()',N'@0 nvarchar(50),@1 nvarchar(50),@2 datetime2(7),@3 nvarchar(50),@4 nvarchar(15),@5 bit,@6 datetime2(7),@7 int,@8 int,@9 int,@10 int,@11 int,@12 nvarchar(50),@13 nvarchar(50),@14 nvarchar(50),@15 float,@16 float,@17 float,@18 bit',@0=N'Name',@1=N'Surname',@2='2017-01-19 10:59:09.9882591',@3=N'Trabzon',@4=N'01234567890',@5=1,@6='2017-01-19 10:59:09.9882591',@7=731825903,@8=1869842619,@9=1701414555,@10=1468342767,@11=1962019787,@12=N'Value6',@13=N'Value7',@14=N'Value8',@15=0,65330243467041405,@16=0,85324223938083377,@17=0,7146566792925152,@18=0
我希望只使用EF来解决这个问题。我知道有很多替代方案,但假设没有其他选择。
主要问题在于,我使用了与我引用答案相同的方法。它可以在191秒内插入560000个实体。但是我只能在7分钟内插入50000个。