2

In my model I have a detail table that will contain data for each day of the week, so it has this definition:

public class JournalDay
{
    public int JournalID { get; set; }
    public DayOfWeek Day { get; set; }
    public TimeSpan From { get; set; }
    public TimeSpan To { get; set; }

    public virtual Journal Journal { get; set; }
}

In the EF Fluent API mapping class I define the composite key like this:

HasKey(jd => new { jd.JournalID, jd.Day });

But the table structure generated by Entity Framework only has the first column in the primary key:

CONSTRAINT [PK_dbo.Journals] PRIMARY KEY CLUSTERED 
(
    [JournalID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

Is this problem caused by the second column being a DayOfWeek enum ?? I've used enums before on composite keys and it never gave this problem

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Manuel Castro
  • 2,460
  • 1
  • 19
  • 14
  • Did you try to cast enum to underlying type? e.g. `HasKey(jd => new { jd.JournalID, (byte)jd.Day });` (or int). – abatishchev Dec 03 '14 at 00:00
  • @abatishchev that syntax gives an error: Invalid anonymous type member declarator, when I've used enums it's not necessary to do that casting – Manuel Castro Dec 03 '14 at 00:15
  • Yes but it's simple to fix: `HasKey(jd => new { jd.JournalID, Day = (byte)jd.Day });`. What will be the result? – abatishchev Dec 03 '14 at 00:16

0 Answers0