DataContext.CreateDatabase 正在创建具有随机顺序列的数据库
我有 ac#.Net 桌面应用程序,其中使用 Linq 作为 ORM,使用 SQL Server Express 作为数据库。因此,当我使用 dataContext.CreateDatabase() 方法时,90% 的情况下它会按照与 linq.cs 中相同的顺序创建列。在剩下的 10% 中,几列的顺序是混乱的。 (提到的百分比是近似值。Linq.cs 是从 SqlMetal 生成的)
是否有发生这种情况的特定原因,或者是否有一些设置可以控制它......? 我想到使用顺序,因为列数约为 30。并且有 15 到 20 个表。
更新
我们以混合方式使用数据库(即也直接执行 SQL 命令)。这就是我寻找列顺序的原因之一。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在其内部,LINQ-to-SQL 使用 SQL 语言。当它创建表时,如果您记录 SQL Server,您将看到 SQL 语句,例如 ALTER TABLE ADD COLUMN...
ADD COLUMN 不允许设置列位置。更一般地说,SQL(这种语言,无论是 Oracle、MySQL 还是 SQLServer)不提供控制列位置的工具,因此列的顺序仅取决于它们的创建顺序。
这种行为是有意为之的。您永远不应该依赖列顺序。大多数数据访问框架明确建议反对它,并且它们不保证您在任何时候拥有的列顺序的恒定性。
另外,在您的情况下,您使用的是 LINQ-to-SQL,因此除非您打算以混合方式使用它(即也直接执行 SQL 命令),否则顺序并不重要,因为您将通过相应的命名来访问列特性。
因此,如果您确实以混合方式使用它,请确保在执行 SqlCommands 时始终指定列名称。不要写成
INSERT INTO MYTABLE VALUES (1,2,3)
,而是写成INSERT INTO MYTABLE(COL1, COL2, COL3) VALUES (1,2,3)
。另外,我不太明白为什么你说你要使用顺序,因为有 30 列。首先,30个并不高(也不是20张表),其次,如果可以通过位置调用(如ADDRESS1、ADDRESS2、ADDRESS3),一般说明数据库设计有缺陷。请参阅第一范式。
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 insteadINSERT 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.