c# - How can I set up my model to perform this transaction? -


i have portion of database created with

/*    create table of scores of games played. every game have score recorded, there    corresponding name if user enters 1 */ create table scores ( id int identity(1,1) not null primary key,                       score int not null,                       name varchar (50)                      );  /*      create table of text logs of games played. these reviewed sniff out cheating.   */ create table gamelogs ( id int identity(1,1) not null primary key,                          scoreid int not null foreign key references scores(id) on delete cascade on update cascade,                          logtext varchar (8000)                        );  

and i'm using model generated entity framework try , perform equivalent of following transaction.

 insert scores (score, name) values (somenumber, somestring);   select max(id) new_id scores;   insert gamelogs (scoreid, logtext) values (new_id, someotherstring); 

the corresponding classes generated entity are

public partial class score {     public score()     {         gamelogs = new hashset<gamelog>();     }      public int id { get; set; }      [column("score")]     public int score1 { get; set; }      [stringlength(50)]     public string name { get; set; }      public virtual icollection<gamelog> gamelogs { get; set; } } 

public partial class gamelog {     public int id { get; set; }      public int scoreid { get; set; }      [stringlength(8000)]     public string logtext { get; set; }      public virtual score score { get; set; } } 

public partial class snakedb : dbcontext {     public snakedb()         // comment/uncomment base(...) depending on project being deployed @ moment         // local database:         //: base("name=snakedb")         // remove database:          : base("name=remote_snakedb")     {     }      public virtual dbset<bannedip> bannedips { get; set; }     public virtual dbset<gamelog> gamelogs { get; set; }     public virtual dbset<ip> ips { get; set; }     public virtual dbset<score> scores { get; set; }      protected override void onmodelcreating ( dbmodelbuilder modelbuilder )     {         modelbuilder.entity<gamelog>()             .property(e => e.logtext)             .isunicode(false);          modelbuilder.entity<ip>()             .hasmany(e => e.bannedips)             .withrequired(e => e.ip)             .willcascadeondelete(false);          modelbuilder.entity<score>()             .property(e => e.name)             .isunicode(false);     } } 

in 1 of controllers have method

    [httppost]     public actionresult submitscore ( namescorelog nsp )     {          // submit name , score scores database         sd.scores.add( new score { name = nsp.name, score1 = nsp.score } );          // ...           sd.submitchanges();      } 

which plan perform transaction mentioned. right clueless how going extract id last element added sd.scores, i.e. equivalent of

select max(id) new_id scores;  

which, way, realize flawed way of getting id last added score, given score inserted in between time added score , made above query.

can give me guidance on how attack problem?

try this:

  var score = new score { name = nsp.name, score1 = nsp.score };   sd.scores.add(score);   db.savechanges();   var id = score .id; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -