1

I have two model classes (Tickets and Activities), where Activities is more like a look-up table, and is not suppose to hold any navigational properties or FK's of other entities.

A record in Tickets is supposed to have a single Activity, but in the whole table of Tickets, there will be many of the same Activities. Activities needs to be a table, and not (for example) an enum, because of the possibility of future updates and amendments.

Although this seems trivial enough, I keep encountering errors like Identity is Set to off.

My models:

public class Tickets
{
    public int ID {get; set;}

    // fk
    public int ActivityID {get; set;

    // nav property
    public Activity Activity {get; set;}
    // other properties.        
}

public class Activity
{
    public int ID {get; set;}
    public int RankOrder {get; set;}
    //other properties
}

I have specified a domainmapping for the Tickets model:

    builder.HasOne(t => t.Activity)
        .WithMany()
        .HasForeignKey(t => t.ActivityID);

based on this SO-post and blogpost: here and here but I don't get it to work, my most recent error message is:

SqlException: Cannot insert explicit value for identity column in table 'Activity' when IDENTITY_INSERT is set to OFF.

Also, with this mapping it seems that EF created a Ticket column in the Activity table, something that I don't want.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
CMorgan
  • 645
  • 2
  • 11
  • 33

1 Answers1

0

Try this

builder.Entity<Ticket>() 
         .HasOne(t => t.Activity)
        .WithMany(a => a.Ticket)
        .HasForeignKey(t => t.ActivityID);
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • 1
    my Activity table doesn't have any navigational properties. So `.WithMany( a => a.Ticket)` isn't possible. I changed my code to make it more clear, maybe the `string Activity` was seen as a nav prop. – CMorgan Feb 02 '20 at 14:53