This project is read-only.

OData metadata doesn't respect fluent API Keys for tables without identity columns

Topics: ASP.NET Web API
Jun 20, 2014 at 4:17 PM
The metadata exposed from an OData service is missing the Key node for entities that have keys defined in fluent API that aren't identity columns.

Here's a sample of the entity and the mapping in a separate code first fluent API configuration.
public class HelpButtonTombstone
    public int HelpButtonId { get; set; }
    public DateTime DeletedDate { get; set; }

public class HelpButtonTombstoneMap : EntityTypeConfiguration<HelpButtonTombstone>
    public HelpButtonTombstoneMap()
        // Primary Key
        this.HasKey(t => t.HelpButtonId);

        // Properties
        this.Property(t => t.HelpButtonId)

        // Table & Column Mappings
        this.Property(t => t.HelpButtonId).HasColumnName("HelpButtonId");
        this.Property(t => t.DeletedDate).HasColumnName("DeletedDate");
The metadata exposed is missing the <Key> node and client proxy generation fails:

<EntityType Name="HelpButtonTombstone">
<Property Name="HelpButtonId" Type="Edm.Int32" Nullable="false"/>
<Property Name="DeletedDate" Type="Edm.DateTime" Nullable="false"/>

The workaround is to add the [Id] attribute in the entity class but this is a bug as the entity already has a key defined in the fluent API. Other entities that have identity columns are exposed correctly in the metadata without adding the redundant [Id] attribute. This wasn't an issue with the same entity model exposed with WCF Data Services.