在通过 Entity Framework 4.1 创建数据库时对列进行排序。具有流畅的 API
我希望在通过实体框架 4.1 创建数据库后对数据库表列进行排序。方法如下:
1)PK 2) 所有外键列 3) 所有复杂类型列 4)所有其他列
问题是不可能通过流畅的API设置外键的顺序,例如基本属性的HasColumnOrder。(所有外键列都是最后一列)
有一些想法吗?
谢谢克里斯
I want have an order of my database table columns after creating DB by entity framework 4.1. in the following way:
1) PK
2) all foreign key columns
3) all complex types columns
4) all Other Columns
The problem is that it is no possibility to set the Order for foreign key's by fluent API, like for example HasColumnOrder for primitive properties.(all foreign key columns are the last columns)
Are there some ideas?
Thanks
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道这是一个旧话题,但我想我会真正回答它。我同意,数据库中的列顺序没有区别。除非您在数据库中搜索数据,否则首先拥有逻辑/有用字段可能会很有用。
答案是使用 .HasColumnOrder(x) 方法。只需输入一个从零开始的数字,表示您希望字段的顺序。请参阅此处的示例:http://hanksnh.wordpress.com/2011/04/08/inheritance-with-entity-framework-4-1/
I know this is an old thread, but I thought I would actually answer it. I agree, the column order in the DB makes no difference. Except for when you are in the DB searching on data, then it can be useful to have the logical/useful fields first.
The answer is with the .HasColumnOrder(x) method. Just put in a zero-based number of what order you want the field to be in. See an example here: http://hanksnh.wordpress.com/2011/04/08/inheritance-with-entity-framework-4-1/
如果要设置外键列的顺序,则必须将它们公开为属性(使用 外键关联而不是独立关联)。顺便提一句。数据库表中的列顺序并不重要。只有键中的列顺序才重要,这就是为什么没有兄弟支持的原因。
If you want to set order for foreign key columns you must expose them as properties (use foreign key association instead of independent association). Btw. order of columns in database table doesn't matter. Only order of columns in a key matters and it is why there is no brother support for this.
我在 EF 迁移的帮助下使用独立关联解决了数据库中外键列顺序的问题。
禁用自动迁移并为您的数据模型创建初始迁移。
对于模型类:
将生成迁移代码:
现在只需通过向上移动外键列(Car、Track)来重新排序。当您创建新数据库并打开表时,列顺序将如预期的那样。
I have solved the problem with foreign key columns order in the database while using independent association with the help of EF migrations.
Disable automatic migrations and create initial migration for your data model.
For model class:
the migration code will be generated:
Now just reorder the foreign key columns (Car, Track) by moving them up. When you create new database and open the table, the column order will be like expected.
如果您正在寻找列顺序,我认为这很容易。在 DbContext 类中,重写 OnModelCreating。然后抓取 modelBuilder,并从中提取 EntityTypeConfiguration。然后使用它配置顺序如下。
最后您需要添加迁移,然后更新数据库。
If you are looking for column order, I think its pretty easy. In your DbContext class, override OnModelCreating. Then grab modelBuilder, and from it pull out EntityTypeConfiguration. Then using it configure the order as follows.
And then finally you need to add migration and then update database.