Creating Primary Key Configurations
 
            
                In This Topic
            
    
    The PrimaryKey method of the EntityConfiguration class is used to define the list of the properties of a primary key. This method returns an instance of the PrimaryKeyConfiguration class.
    
 
    The example of defining a primary key, which consists of one property:
 
    
builder.Entity<Company>()
    .PrimaryKey(company => company.Id);
 
builder.Entity(typeof(Company))
    .PrimaryKey("Id");
 
Type type = Type.GetType("CrmDemoContext.Company");
PropertyInfo property = type.GetProperty("Id");
builder.Entity(type)
    .PrimaryKey(property);
 
     
    
    The example of defining a complex primary key:
     
    
    
builder.Entity<OrderDetail>()
    .PrimaryKey(orderDetail => new { orderDetail.OrderID, orderDetail.ProductID });
 
builder.Entity(typeof(OrderDetail))
    .PrimaryKey("OrderID", "ProductID");
 
     
    
    The example of a database-generated primary key:
    
    
    
builder.Entity<Company>()
    .PrimaryKey(company => company.Id)
        .DbGenerated();