Order rows with Entity Framework

asp.net asp.net-mvc c# entity-framework-6 sql

Question

I'm using EF in order to insert and retrieve info from DB, there is any way to insert new row but at the specified position,

Like i have 10 rows with IDs ranging from 0 to 9 and new row i'm inserting will be on the position 4?

I'm using ASP.NET MVC 5 and LINQ.

Thank you.

1
1
11/10/2015 8:50:56 PM

Popular Answer

So, I would do so quickly: I would plan the database to not auto increment primary key and saving would so that the id is attributed according to the specific location. Obviously put an IF to verify that it is available, and if I would start a review cycle to the cascade of subsequent ID or positioning the value traded in the end.

for example

MyTable table = myDb.MyTable.Find(id);  //position
if (table==null)
{ table.id=position; table.Field=value; myDb.SaveChanges() }  
else
{
var temp = table.id;
var max = table.count(x=> x.id).value;
table.id=max+1;myDb.SaveChanges();
table.id=id; table.Field=value; myDb.SaveChanges();
}

sorry if translate is no good! ;-)

0
11/10/2015 9:13:47 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow