DataContext.CreateDatabase is creating the database with columns in random order
I am having a c#.Net desktop application in which I use Linq as ORM and SQL server Express as my database. So, when I use dataContext.CreateDatabase() method, 90 % of the times it is creating the columns in the same order as in linq.cs. In that remaining 10%, the order of the few columns is jumbled. (percentages mentioned are approximate. Linq.cs is generated from开发者_如何学C SqlMetal)
Is there a specific reason why it happens or is there some setting for controlling that..? I thought of using the order since the number columns is around 30. And there are 15 to 20 tables as such.
UPDATE
We are using the database in a hybrid way(i.e., also execute SQL commands directly). That is one reason why i am looking for order of columns.
In its internals, LINQ-to-SQL uses the SQL language. When it creates tables, if you log your SQL Server, you will see SQL statements, like ALTER TABLE ADD COLUMN...
ADD COLUMN does not allow setting a column position. More generally, SQL (the language, weather it's Oracle, MySQL or SQLServer) does not provide tools to control columns position, so the order of the columns is only dependent on the order in which they have been created.
This behavior is intended. You should not rely on column order ever. Most data access frameworks explicitly advice against it and they don't guarantee the constancy of the order of columns you have at any point.
Plus, in your case, you're using LINQ-to-SQL, so unless you are going to use it in an hybrid way (ie also execute SQL commands directly), order will not matter, since you will access columns through corresponding named properties.
So, if indeed you use it in an hybrid way, be sure to always specify column names when you execute SqlCommands. Do not write INSERT INTO MYTABLE VALUES (1,2,3)
, write instead INSERT INTO MYTABLE(COL1, COL2, COL3) VALUES (1,2,3)
.
Also, I don't quite understand why you say you are going to use order since there are 30 columns. First, 30 is not high (neither is 20 tables), and second, if they can be called by their position (like ADDRESS1, ADDRESS2, ADDRESS3) generally it means that the database design is flawed. See 1st normal form.
精彩评论