简体   繁体   English

实体框架组合主键-如何获取列顺序

[英]Entity framework composite primary key - how to get order of columns

I have an entity which I have configured using the Fluent API. 我有一个使用Fluent API配置的实体。 Notice the HasColumnOrder on the properties. 注意属性上的HasColumnOrder。

public class SomeEntityMap : EntityTypeConfiguration<SomeEntity>
{
    public SomeEntityMap ()
    {
        // Primary Key
        this.HasKey(t => new { t.Id, t.BarId });

        this.ToTable("SomeEntity", "Foo");
        this.Property(t => t.Id).HasColumnName("Id").HasColumnOrder(0);
        this.Property(t => t.BarId).HasColumnName("BarId").HasColumnOrder(1);

        // additional properties removed for brevity
    }
}

As part of a generic method, I want to be able to find an entity's keys so I can find an entity using 作为通用方法的一部分,我希望能够找到实体的键,以便可以使用

DbSet().Find() DbSet()。find()方法

to which I need to pass the primary key values in the correct order . 我需要以正确的顺序将主键值传递给该键。

I can get the key names from the ObjectContext but I cannot see where to get the order. 我可以从ObjectContext获取键名,但是看不到从哪里获得订单。 Can anyone help? 有人可以帮忙吗?

Try this: 尝试这个:

   var adapter = (IObjectContextAdapter)db;
   var objectContext = adapter.ObjectContext;
   var objectSet = objectContext.CreateObjectSet<SomeEntity>();
   var entitySet = objectSet.EntitySet;
   var keyNames = entitySet.ElementType.KeyMembers             
                 .Select(e => e.Name).ToList();

If you want to find the auto generated keys, then add this Where before the Select : 如果你想找到自动生成的密钥,然后添加此Where的前Select

.Where(p => p.MetadataProperties.Any(m => m.PropertyKind == PropertyKind.Extended
                                  && Convert.ToString(m.Value) == "Identity"))

Not need to Add code as this 不需要这样添加代码

// Primary Key
this.HasKey(t => new { t.Id, t.BarId });

Only user 仅限使用者

// Primary Key
this.HasKey(t =>t.Id );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取表的PRIMARY KEY COLUMNS(如果是COMPOSITE主键) - How to get PRIMARY KEY COLUMNS of a table (in case of COMPOSITE primary key) 在Entity Framework中定义复合主键 - Define composite primary key in Entity Framework 具有复合主键的实体框架“主键的违规” - Entity Framework “Violation of primary key” with composite primary key 如何使用复合键在 model 的实体框架核心中获取单个实体? - How to get single entity in Entity Framework Core of model with composite key? 实体框架-具有主键和复合外键的多对多 - Entity Framework - many to many with primary key and composite foreign key 具有复合键的实体框架:违反PRIMARY KEY约束 - Entity Framework with composite key: Violation of PRIMARY KEY constraint Entity Framework 5.0非主键的复合外键-可能吗? - Entity Framework 5.0 composite foreign key to non primary key - is it possible? 具有一个主键的实体框架组合键删除 - Entity Framework composite key delete with one primary key 如何使用实体框架核心 3.1.1 为拥有的实体类型设置复合主键? - How to set the composite primary key for owned entity types using entity framework core 3.1.1? 如何使用Entity Framework将DbContext添加到具有复合主键的实体 - How can i add DbContext to entity with composite primary key with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM