在通过 Entity Framework 4.1 创建数据库时对列进行排序。具有流畅的 API

发布于 2024-11-28 13:39:54 字数 192 浏览 1 评论 0原文

我希望在通过实体框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妖妓 2024-12-05 13:39:54

我知道这是一个旧话题,但我想我会真正回答它。我同意,数据库中的列顺序没有区别。除非您在数据库中搜索数据,否则首先拥有逻辑/有用字段可能会很有用。

答案是使用 .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/

小霸王臭丫头 2024-12-05 13:39:54

如果要设置外键列的顺序,则必须将它们公开为属性(使用 外键关联而不是独立关联)。顺便提一句。数据库表中的列顺序并不重要。只有键中的列顺序才重要,这就是为什么没有兄弟支持的原因。

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.

计㈡愣 2024-12-05 13:39:54

我在 EF 迁移的帮助下使用独立关联解决了数据库中外键列顺序的问题。

禁用自动迁移并为您的数据模型创建初始迁移。

对于模型类:

public class Session
{
  public int? Id { get; set; }     
  public Track Track { get; set; }   
  public Car Car { get; set; }    
  public int Event { get; set; }
  public DateTime Date { get; set; }    
  public string Name { get; set; }    
}

将生成迁移代码:

CreateTable("dbo.Sessions", c => new
{
  Id = c.Int(nullable: false, identity: true),
  Event = c.Int(nullable: false),
  Date = c.DateTime(nullable: false),
  Name = c.String(nullable: false, maxLength: 64),
  Car = c.Int(nullable: false),
  Track = c.Int(nullable: false),
}) ...

现在只需通过向上移动外键列(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:

public class Session
{
  public int? Id { get; set; }     
  public Track Track { get; set; }   
  public Car Car { get; set; }    
  public int Event { get; set; }
  public DateTime Date { get; set; }    
  public string Name { get; set; }    
}

the migration code will be generated:

CreateTable("dbo.Sessions", c => new
{
  Id = c.Int(nullable: false, identity: true),
  Event = c.Int(nullable: false),
  Date = c.DateTime(nullable: false),
  Name = c.String(nullable: false, maxLength: 64),
  Car = c.Int(nullable: false),
  Track = c.Int(nullable: false),
}) ...

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.

感情废物 2024-12-05 13:39:54

如果您正在寻找列顺序,我认为这很容易。在 DbContext 类中,重写 OnModelCreating。然后抓取 modelBuilder,并从中提取 EntityTypeConfiguration。然后使用它配置顺序如下。

public class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserLogin, AppUserRole, AppUserClaim>
{
    public AppDbContext() : base("AvbhHis")
    {

    }

    public DbSet<PatientCategory> Product { get; set; }
    public DbSet<LookupBase> LookupBase { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder
            .Entity<PatientCategoryLookup>()
            .Map<PatientCategoryLookup>(m =>
            {
                m.ToTable("LookupPatientCategory");
                m.MapInheritedProperties();
            });
        EntityTypeConfiguration<PatientCategoryLookup> config = modelBuilder.Entity<PatientCategoryLookup>();
        config.Property(e => e.Id).HasColumnOrder(0);
        config.Property(e => e.PatientCatgCode).HasColumnOrder(1);
        config.Property(e => e.PatientCatgName).HasColumnOrder(2);
        config.Property(e => e.Description).HasColumnOrder(3);
        config.Property(e => e.ModifiedTime).HasColumnOrder(4);
        config.Property(e => e.History).HasColumnOrder(5);

        base.OnModelCreating(modelBuilder);
    }

}

最后您需要添加迁移,然后更新数据库。

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.

public class AppDbContext : IdentityDbContext<AppUser, AppRole, int, AppUserLogin, AppUserRole, AppUserClaim>
{
    public AppDbContext() : base("AvbhHis")
    {

    }

    public DbSet<PatientCategory> Product { get; set; }
    public DbSet<LookupBase> LookupBase { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder
            .Entity<PatientCategoryLookup>()
            .Map<PatientCategoryLookup>(m =>
            {
                m.ToTable("LookupPatientCategory");
                m.MapInheritedProperties();
            });
        EntityTypeConfiguration<PatientCategoryLookup> config = modelBuilder.Entity<PatientCategoryLookup>();
        config.Property(e => e.Id).HasColumnOrder(0);
        config.Property(e => e.PatientCatgCode).HasColumnOrder(1);
        config.Property(e => e.PatientCatgName).HasColumnOrder(2);
        config.Property(e => e.Description).HasColumnOrder(3);
        config.Property(e => e.ModifiedTime).HasColumnOrder(4);
        config.Property(e => e.History).HasColumnOrder(5);

        base.OnModelCreating(modelBuilder);
    }

}

And then finally you need to add migration and then update database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文