0

Table definition:

CREATE TABLE [Index].[Dependency] (
    [RevisionId]                    INT            NOT NULL,
    [MapCode]                       VARCHAR (10)   NOT NULL,
    [GroupId]                       SMALLINT       NOT NULL,
    [DependencyTypeId]              TINYINT        NOT NULL,
    [InputThreshold]                 DECIMAL (9, 2) NOT NULL,
    [InjectMapCode]                  VARCHAR (10)   NULL,
    [InjectAmount]                 DECIMAL (9, 2) NOT NULL,
    CONSTRAINT [UQ_Dependency_RevisionId_MapCode_GroupId] UNIQUE NONCLUSTERED ([RevisionId] ASC, [MapCode] ASC, [GroupId] ASC),
    CONSTRAINT [FK_Dependency_MapCode] FOREIGN KEY ([MapCode]) REFERENCES [Index].[Material] ([MapCode]),
    CONSTRAINT [FK_Dependency_InjectMapCode] FOREIGN KEY ([InjectMapCode]) REFERENCES [Index].[Material] ([MapCode]),
    CONSTRAINT [FK_Dependency_Revision] FOREIGN KEY ([RevisionId]) REFERENCES [Index].[DependencyRevision] ([DependencyRevisionId]),
    CONSTRAINT [FK_Dependency_DependencyType] FOREIGN KEY ([DependencyTypeId]) REFERENCES [Index].[DependencyType] ([DependencyTypeId]),
    CONSTRAINT [FK_Dependency_Group] FOREIGN KEY ([GroupId]) REFERENCES [Index].[Group] ([GroupId])
);

My attempt to map to it:

public class DependencyMap : EntityTypeConfiguration<Entities.Dependency>
{
    public DependencyMap()
    {
        ToTable("Dependency", "Index");
        HasKey(md => new { md.RevisionId, md.MapCode, md.GroupId });
        Property(s => s.RevisionId).IsRequired().HasColumnName("RevisionId");
        Property(s => s.MapCode).IsRequired().HasMaxLength(10).HasColumnName("MapCode");
        Property(s => s.GroupId).IsRequired().HasColumnName("GroupId");
        Property(s => s.DependencyTypeId).IsRequired().HasColumnName("DependencyId");
        Property(s => s.InputThreshold).IsRequired().HasColumnName("InputThreshold").HasPrecision(9, 2);
        Property(s => s.InjectMapCode).IsOptional().HasColumnName("InjectEngineMapCode").HasMaxLength(10);
        Property(s => s.InjectAmount).HasColumnName("InjectAmount").IsRequired().HasPrecision(9, 2);

    }
}

When I try to call a method that retrieves from the table an exception is thrown on

'HasKey(md => new { md.RevisionId, md.MapCode, md.GroupId })'

saying that 'properties expression is not valid. The expression should represent a property' when mapping the table. If anyone could show me how to properly map a table like this without explicitly defined primary key it would be much appreciated. Thanks.

John Doe
  • 205
  • 6
  • 17
  • How about adding a RowId and have that as a primary key (which wouldn't have any effect on your model)? – PmanAce Jun 25 '16 at 18:32
  • 1
    Without your model classes, we can't help you. – Eris Jun 25 '16 at 18:38
  • What is the primary key of the database table? And in the C# code you have to make sure you're referring to properties, not fields. Without seeing the `Dependency` class we can't help you any further. – Gert Arnold Jun 25 '16 at 21:20
  • Possible duplicate of [Mapping composite keys using EF code first](http://stackoverflow.com/questions/19792295/mapping-composite-keys-using-ef-code-first) – hbulens Jun 26 '16 at 08:44

0 Answers0