From: paul.mercea <pau...@al...> - 2017-08-25 20:52:18
|
The trigger from you database has nothing to do with your c# code.You can use a stored procedure to insert data and returning latest id (also you can read current sequence value but you can have wrong value if other inserts happening). Sent from my Samsung Galaxy smartphone. -------- Original message --------From: "Mr. John via Firebird-net-provider" <fir...@li...> Date: 8/25/17 20:28 (GMT+02:00) To: "For Users and Developers of the Firebird .NET Providers" <fir...@li...> Cc: "Mr. John" <mr_...@ya...> Subject: [Firebird-net-provider] EF - last inserted ID is 0 HiI'm using FB 2.5,EF Firebird 5.9.1,Asp MVC 5 with VS 2017 on a table I have a trigger to generate ID: CREATE OR ALTER TRIGGER TRIG_MYTABLE_BI FOR MYTABLE ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF ((NEW.ID IS NULL) OR (NEW.ID=0)) THEN NEW.ID=NEXT VALUE FOR GEN_MYTABLE_ID; END in my code I insert data like this: myTableEnt newRec= new myTableEnt() { NAME='MYNAME', .... }; db.myTableEnt.Add(newRec); db.SaveChanges(); return ToJson(new { msg = newRec.ID.ToString() }); newRec.ID is returned always 0,but in database value is generatedwhat I've done wrongthanks |
From: Mr. J. <mr_...@ya...> - 2017-08-26 04:38:51
|
Hi.Thanks Paul for your answerFor my first code I've followed this: https://stackoverflow.com/questions/4068084/how-to-find-an-identity-of-the-last-inserted-row-in-entity-frameworkand that seems not to apply to FB?I so I can insert ID without trigger,directly from my code,something like this: myTableEnt newRec= new myTableEnt() { ID= GetNextSequenceValue(ref db, "SELECT NEXT VALUE FOR GEN_MYTABLE_ID FROM RDB$DATABASE") NAME='MYNAME', .... }; ...public long GetNextSequenceValue(ref myEnt ctx, string sql) { var rawQuery = ctx.Database.SqlQuery<long>(sql); var task = rawQuery.SingleAsync(); long nextVal = task.Result; return nextVal; } thanks From: paul.mercea <pau...@al...>To: For users and developers of the Firebird .NET providers <fir...@li...>Cc: Mr. John <mr_...@ya...>Sent: Friday, August 25, 2017, 11:35:11 PM GMT+3Subject: Re: [Firebird-net-provider] EF - last inserted ID is 0 The trigger from you database has nothing to do with your c# code.You can use a stored procedure to insert data and returning latest id (also you can read current sequence value but you can have wrong value if other inserts happening). Sent from my Samsung Galaxy smartphone. -------- Original message --------From: "Mr. John via Firebird-net-provider" <fir...@li...> Date: 8/25/17 20:28 (GMT+02:00) To: "For Users and Developers of the Firebird .NET Providers" <fir...@li...> Cc: "Mr. John" <mr_...@ya...> Subject: [Firebird-net-provider] EF - last inserted ID is 0 HiI'm using FB 2.5,EF Firebird 5.9.1,Asp MVC 5 with VS 2017 on a table I have a trigger to generate ID: CREATE OR ALTER TRIGGER TRIG_MYTABLE_BI FOR MYTABLE ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF ((NEW.ID IS NULL) OR (NEW.ID=0)) THEN NEW.ID=NEXT VALUE FOR GEN_MYTABLE_ID; END in my code I insert data like this: myTableEnt newRec= new myTableEnt() { NAME='MYNAME', .... }; db.myTableEnt.Add(newRec); db.SaveChanges(); return ToJson(new { msg = newRec.ID.ToString() }); newRec.ID is returned always 0,but in database value is generatedwhat I've done wrongthanks |
From: Daniel R. <da...@ac...> - 2017-08-26 13:37:24
|
<html><head><title>Re: [Firebird-net-provider] EF - last inserted ID is 0</title> </head> <body> <span style=" font-family:'Courier New'; font-size: 9pt;">Hi,<br> <br> At August 26, 2017, 1:38 AM, Mr. John via Firebird-net-provider wrote:<br> <br> <a name="ydp6aee214eyahoo_quoted_4196587569"></a> </span><table> <tr> <td width=2 bgcolor= #0000ff><br> </td> <td><span style=" font-family:'verdana'; font-size: 10pt;">Hi.<br> Thanks Paul for your answer<br> For my first code I've followed this: https://stackoverflow.com/questions/4068084/how-to-find-an-identity-of-the-last-inserted-row-in-entity-framework<br> and that seems not to apply to FB?</td> </tr> </table> <br> <span style=" font-family:'Courier New'; font-size: 9pt;">That's how I do it. Just make certain that you are using the <span style=" font-size: 10pt;">[DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute on the ID column in the myTableEnt entity.<br> <br> <br> <span style=" font-family:'calibri'; font-size: 9pt; color: #c0c0c0;"><i>-- <br> Best regards,<br> Daniel Rail<br> Senior Software Developer<br> ACCRA Solutions Inc. (</i></span></span></span><a style=" font-family:'calibri'; font-size: 9pt;" href="http://www.accra.ca">www.accra.ca</a><span style=" font-family:'calibri'; font-size: 9pt; color: #c0c0c0;"><i>)<br> ACCRA Med Software Inc. (</i></span><a style=" font-family:'calibri'; font-size: 9pt;" href="http://www.filopto.com">www.filopto.com</a><span style=" font-family:'calibri'; font-size: 9pt; color: #c0c0c0;"><i>)</body></html> |
From: Mr. J. <mr_...@ya...> - 2017-08-26 16:00:47
|
HiI'm using DbFirst,if I set [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute on the ID column after a model refresh it will be lostthanks From: Daniel Rail <da...@ac...>To: "fir...@li..." <fir...@li...>Sent: Saturday, August 26, 2017, 4:37:44 PM GMT+3Subject: Re: [Firebird-net-provider] EF - last inserted ID is 0 Hi, At August 26, 2017, 1:38 AM, Mr. John via Firebird-net-provider wrote: | | Hi. Thanks Paul for your answer For my first code I've followed this: https://stackoverflow.com/questions/4068084/how-to-find-an-identity-of-the-last-inserted-row-in-entity-framework and that seems not to apply to FB? | That's how I do it. Just make certain that you are using the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute on the ID column in the myTableEnt entity. -- Best regards, Daniel Rail Senior Software Developer ACCRA Solutions Inc. (www.accra.ca) ACCRA Med Software Inc. (www.filopto.com)------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot_______________________________________________ Firebird-net-provider mailing list Fir...@li... https://lists.sourceforge.net/lists/listinfo/firebird-net-provider |