Home > Net >  Asp.net core2.2 how to make the seed data generated in the row of good order?
Asp.net core2.2 how to make the seed data generated in the row of good order?

Time:10-08

 
///& lt; Summary>
///initializes the system classification
///& lt;/summary>
///& lt; Param name="context" & gt;
Public static void SeedSystemCategory (ApplicationDbContext context)
{
If (context) DT_SystemCategory) Any ())
{
return;
}
The context. DT_SystemCategory. AddRange (
New M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
},
New M_SystemCategory
{
Name="units",
PID=0,
The Order=2
},
New M_SystemCategory
{
Name="loss rate of stone,"
PID=0,
The Order=3
},
New M_SystemCategory
{
Name="machine type",
PID=0,
The Order=4
},

);
The context. The SaveChanges ();
}


I use this to generate the database data, but the results in the database to generate, the order is random, what is the way to order it generated?

CodePudding user response:

What you said what database is a database? You said "sequence is random," order is here? Is your observation to order? Or the order of the query down?

In general, a relational database (including MSSQL, oracle, mysql, etc.) is the mathematical basis of in set theory,
And the order of the collection is no concepts, data insertion order, it doesn't matter to a relational database can be delete order,
Select * from SystemCategory return record sequence is not defined, we can not depend on its performance,

Relational database query, only in the order by clause, the order to be meaningful, such as
Select * from SystemCategory the order by [order] there is a clear order of significance,

CodePudding user response:

If you want automatic sorting, SortList If the database then SELECT * FROM TB ORDER BY field ASC or DESC

CodePudding user response:

Did I know the people in the EF to initialize the database natural I describe, so in this I am not tired above,

CodePudding user response:

The context. DT_SystemCategory. AddRange (
New M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
},
New M_SystemCategory
{
Name="units",
PID=0,
The Order=2
},
New M_SystemCategory
{
Name="loss rate of stone,"
PID=0,
The Order=3
},
New M_SystemCategory
{
Name="machine type",
PID=0,
The Order=4
},

);
This way, can you change to similar context. M_SystemCategory. Add (new M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
})
The context. M_SystemCategory. Add (new M_SystemCategory
{
Name="units",
PID=0,
The Order=2
})
. To the last savechange
If still won't do, every add, just save once

CodePudding user response:

Put the data into the LIst Then call context AddRange (List)
Not to go, sometimes some things can not be province, to eliminate the BUG came

CodePudding user response:

The
reference 1/f, hands on the keyboard to knock very light response:
context. DT_SystemCategory. AddRange (
New M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
},
New M_SystemCategory
{
Name="units",
PID=0,
The Order=2
},
New M_SystemCategory
{
Name="loss rate of stone,"
PID=0,
The Order=3
},
New M_SystemCategory
{
Name="machine type",
PID=0,
The Order=4
},

);
This way, can you change to similar context. M_SystemCategory. Add (new M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
})
The context. M_SystemCategory. Add (new M_SystemCategory
{
Name="units",
PID=0,
The Order=2
})
. To the last savechange
If still won't do, every time the add, save a


Does not support the add method, error directly

CodePudding user response:

So try
The context. DT_SystemCategory. AddRange (
New M_SystemCategory
{
Name="priority",
PID=0,
The Order=1
})

The context. DT_SystemCategory. AddRange (new M_SystemCategory
{
Name="units",
PID=0,
The Order=2
})
The context. The SaveChanges ();
  •  Tags:  
  • C#
  • Related