4

I need to manually enter primary key values in a database, as i need the key values to be in sync with an Enum. However using DatabaseGeneratedoption.None doesn't appear to work. Even when i delete the database and start fresh, the Id column in the JobType still has an auto-incrmeneting primary key.

Is it anything to do with the fact that i'm using an enum as a primary key?

Job Entity

public class Job : EntityBase
{
    public long JobId { get; set; }       
    public JobTypeEnum JobTypeId { get; set; }
    public JobType JobType { get; set; }      
}

Job Type

public class JobType 
{         
    public JobTypeEnum Id { get; set; }
    public String Name { get; set; }
    public String Description { get; set; }       
}

Configuration Classes

public class JobTypeConfiguration : EntityTypeConfiguration<JobType>
{
    public JobTypeConfiguration()
    {
        HasKey(x => x.Id);
        Property(x => x.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        Property(x => x.Name).HasMaxLength(20);
        Property(x => x.Description).HasMaxLength(255);
    }
}

public class JobConfiguration : EntityTypeConfiguration<Job>
{
    public JobConfiguration()
    {
        HasKey(x => x.JobId);
        Property(x => x.JobId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);          
         HasRequired(x => x.JobType).WithMany().HasForeignKey(x => x.JobTypeId);

    }
}

Model Builder

modelBuilder.Configurations.Add(new JobConfiguration());
modelBuilder.Configurations.Add(new JobTypeConfiguration());

EDIT:

I've tracked down a probable cause. I have a Migration that creates tables for Elmah. If i exclude that migration, then it works fine. If i include it, then it doesn't.

Bizarre...

MrBliz
  • 5,830
  • 15
  • 57
  • 81

1 Answers1

2

I have taken your code and added it to my project. This is the Migration code generated:

    public override void Up()
    {
        CreateTable(
            "dbo.Jobs",
            c => new
                {
                    JobId = c.Long(nullable: false, identity: true),
                    JobTypeId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.JobId)
            .ForeignKey("dbo.JobTypes", t => t.JobTypeId, cascadeDelete: true)
            .Index(t => t.JobTypeId);

        CreateTable(
            "dbo.JobTypes",
            c => new
                {
                    Id = c.Int(nullable: false),
                    Name = c.String(maxLength: 20),
                    Description = c.String(maxLength: 255),
                })
            .PrimaryKey(t => t.Id);


    }

You can see that the dbo.JobTypes table does not have identity: true. You may find that you have fallen into the problem described here: How to alter column from DatabaseGenerated.Identity to DatabaseGenerated.None

The simplest thing to do is probably to find the CreateTable statement for JobType, fix it, then re-create the database.

Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197
  • Cheers, but as said in OP, this happens when i delete and recreate the database too. – MrBliz Nov 11 '14 at 13:57
  • 1
    Can you post the `CreateTable` statement from your Up Migration? Is there an `ALTER TABLE` statement in a subsequent migration? – Colin Nov 11 '14 at 13:59
  • I'm using automatic migrations at the moment. – MrBliz Nov 11 '14 at 14:03
  • Think i've tracked down the problem. Updated the question but it's probably not of any use to anyone... Thanks for the help. – MrBliz Nov 11 '14 at 15:13