0

We are Using Service fabric Actor application,in that we have multiple actors. if i want to Update 10 records each record acts as like different individual instance.so when we insert it will create new ObjectContext everytime. so we con't store cache data in context level. so my datamodel is like

public class StudentData {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
     public String StudentId { set; get; }
     public string StudentName {get;set;}
     public String StudentAge { set; get; }
     public string StudentDob {get;set;}
     public String StudentSTD { set; get; }
     public string StudentEmail {get;set;}
     public String StudentAddress { set; get; }
     public string StudentReligion {get;set;}

}

And when we want to update 10 students 10 object instances will create. so for every instance it will call the below method. so below method will call 10 times as different instance id.

public async Update(){
using(var context = new DatabaseContext()){
        context.InfoObjectDatas.Attach(studentObj);
        context.Entry(studentObj).State = System.Data.Entity.EntityState.Modified;
        await context.SaveChangesAsync();
     } }
chinna
  • 79
  • 1
  • 10
  • Using Stored Procedures could be a good start... After that, we might need some more infos about your software architecture to provide more help. – Atlasmaybe Aug 02 '17 at 14:51
  • How to run stored procedure in entity framework? my query is like update set uname="" where id='chinna'.how can i write stored procedure for this. and how to execute that stored procedure through entity framework? – chinna Aug 02 '17 at 15:27
  • Software Architecture is Service fabric Actor architecture. we have 4 actor applications. in that one is objectactor. here we have multiple objects, when we update 10 objects means 10 actor instances will create. so each actor is different instance. no similarities between these actors. so when we do update operation 10 times ExecuteSqlCommand() method will call. so it is taking much time. – chinna Aug 02 '17 at 16:24
  • SO is full of answers for executing SP through EF : https://stackoverflow.com/questions/20901419/how-to-call-stored-procedure-in-entity-framework-6-code-first You could also see here how to create a SP : https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/create-a-stored-procedure Plus, why wouldn't you use EF at the first place ? Using ExecuteSqlCommand means you bypass the EF functions... http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx – Atlasmaybe Aug 03 '17 at 07:40
  • @Atlasmaybe I have followed what you suggested me in your recent comment. i have replaced SP command with entityframework. i have used savechanges() for updating entries. compared to previous now little bit faster to updating query. but still savechanges() taking little time (10 records -> 1.2sec). how can i reduce this time. sometimes it is taking much time some time very less time. why it is taking like that? – chinna Aug 03 '17 at 09:41
  • Can you post your data model and your update process (code or pseudocode)? – Atlasmaybe Aug 03 '17 at 13:01
  • @Atlasmaybe I will post as Answer please check – chinna Aug 03 '17 at 15:16
  • You should make an edit instead of an answer. – Atlasmaybe Aug 03 '17 at 15:26
  • Looking at your data model, nothing seems too much complicated (except for the "everything is a string", which is crappy as hell). For you code, you should instanciate the context outside of the for, and the SaveChanges shoud also be done after the loop. – Atlasmaybe Aug 03 '17 at 15:30
  • @Atlasmaybe i have to change now or is it ok for you? – chinna Aug 03 '17 at 15:30
  • i just written for loop for your understanding. means u have to think like 10 object instances each one has different actor id. each one is like different service. so everytime the below method will call. and create new DatabaseContext. – chinna Aug 03 '17 at 15:33
  • Can you post your update process code ? (make an edit this time ;)) – Atlasmaybe Aug 03 '17 at 15:45
  • i have edited my question please check – chinna Aug 03 '17 at 15:51
  • I can't find any bottleneck in your code... Maybe it's your architecture? Have you tried to update 10 students in a dummy app (without Service fabric Actor)? – Atlasmaybe Aug 03 '17 at 15:56
  • After your reply only i have changed the logic of update. before i have used Storedprocedure ExecuteSqlCommand() query. now its working fine. thanks @Atlasmaybe – chinna Aug 03 '17 at 16:11
  • Sorry, it's not clear for me. What have you done that make it faster now? – Atlasmaybe Aug 04 '17 at 08:11
  • As per your suggestion above (why wouldn't you use EF at the first place ? Using ExecuteSqlCommand means you bypass the EF functions ) . i have changed my update logic from stored procedure to entityframework. so now its working faster. – chinna Aug 05 '17 at 12:15
  • Ok fine. Wish you well. – Atlasmaybe Aug 07 '17 at 07:37

1 Answers1

0

We are Using Service fabric Actor application,in that we have multiple actors. if i want to insert 10 records each record acts as like different individual instance.so when we insert it will create new ObjectContext everytime. so we con't store cache data in context level. so my datamodel is like

public class StudentData {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
     public String StudentId { set; get; }
     public string StudentName {get;set;}
     public String StudentAge { set; get; }
     public string StudentDob {get;set;}
     public String StudentSTD { set; get; }
     public string StudentEmail {get;set;}
     public String StudentAddress { set; get; }
     public string StudentReligion {get;set;}

}

And when we want to insert 10 students 10 object instances will create. so for every instance it will call . so i am using like for loop for 10 objects.

public async insert(){
using(var context = new DatabaseContext()){
        Student st=new Student();
        context.StudentData.Add(st);
        context.StudentData.SaveChanges();
     } }
chinna
  • 79
  • 1
  • 10