Mapping Multiple Primary Keys in Entity Framework


This tip shows one simple solution to allow multiple primary keys mapping in entities.

When a datatable has multiple keys, the entity framework by default gets confused for mapping the primary keys. By assigning an Order value to the Column attribute when mapping the primary keys solves this problem. This Order value should be in the same order as these columns appear in the datatable.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace LeafyJava.Models
    public class Event
        [Column("pub_id", Order = 0)]
        public string PubId { get; set; }
        [Column("auth_id", Order = 1)]
        public string AuthId { get; set; }
        public string Description { get; set; }      


Popular posts from this blog

Distributed Transaction Management with REST and Try Confirm/Cancel Pattern

Spring JMS and JPA XA Transactions with WebLogic

Creating a Library Project for Spring Boot