Dataservicecontext in WCF Service- insertion is slow savechanges()

I have a list which has 1000 records List<people> a = new List<people> {get; set;} And I have try to insert a each record at a time.

foreach( var d in a)
{
servicereferences.MiddleTier data = new servicereferences.MiddleTier();
data.id=a.id;
data.name=a.name;
data.AddObject("datas",data);
}
SaveChanges(data);

So by using Entity Framework -DataServiceContext Addobject() I've added a all data's into addobject method.

Savechanges insert the records a table into database.

It takes more time in dataservicecontext, likely it takes around 6 mins to insert the loop data into database.

Any suggestion's Please!! ( I have to separate into a batch as 1 request to save 100 or 200 records at a time)

Source: https://learn.microsoft.com/en-us/dotnet/api/system.data.services.client.dataservicecontext?view=netframework-4.8

Upvotes: 0

Views: 108

Answers (1)

Jiayao
Jiayao

Reputation: 568

Addobject() maybe like this: context.TableName.AddObject(TableEntityInstance);

Means:

TableName: the name of the table in the database.
TableEntityInstance: an instance of the table entity class.

Here is a example:

public void UpdatePlayerScreen(byte[] imageBytes, string installationKey)
{
  var player = (from p in this.ObjectContext.Players where p.InstallationKey == installationKey select p).FirstOrDefault();

  var current = (from d in this.ObjectContext.Screenshots where d.PlayerID == player.ID select d).FirstOrDefault();

  if (current != null)
  {
    current.Screen = imageBytes;
    current.Refreshed = DateTime.Now;

    this.ObjectContext.SaveChanges();
  }
  else
  {
    Screenshot screenshot = new Screenshot();

    screenshot.ID = Guid.NewGuid();
    screenshot.Interval = 1000;
    screenshot.IsTurnedOn = true;
    screenshot.PlayerID = player.ID;
    screenshot.Refreshed = DateTime.Now;
    screenshot.Screen = imageBytes;

    this.ObjectContext.Screenshots.AddObject(screenshot);
    this.ObjectContext.SaveChanges();
  }
}

Upvotes: 0

Related Questions