12

I've got a problem with a code-first model I've got. Data is now in the database so I can't re-seed the database using a DropCreateDatabaseIfModelChanges class, but I need to change one table so that a bigint column is not an IDENTITY(1,1). I've managed to do this using SSMS but now my EF code is saying it's out of date. This is the code for the table in question:

public class Vote {
    [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long FacebookUserId { get; set; }
    [Required]
    public Entity Entity { get; set; }
}

So I've changed my table schema, and my model (which I thought was the reflection of it, but I'm obviously wrong), but EF is still saying my model is out of date, and I can't re-seed the database to get it "perfect".

Any help would be much appreciated.

Thanks,

Benjamin

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
Jamie Howarth
  • 3,273
  • 3
  • 20
  • 26

3 Answers3

9

Using data annotation:

public class Customer
{
[Key]

    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CustomerID { get; set; }
}

Using fluent API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Customer>().Property(c => c.CustomerID).HasDatabaseGeneratedOption(null);
    base.OnModelCreating(modelBuilder);
}
Shehab Fawzy
  • 7,148
  • 1
  • 25
  • 18
4

referring to this post ...

It seems that entity framework expects by default that you insert into identity column.

to solve this try

    modelBuilder.Entity<BOB>()
    .HasKey(p => p.Id)
        .Property(p => p.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.None;

builder.Entity<BOB>().MapSingleType().ToTable("BOB");

or decorate your key in the POCO with ...

 [Key]
 [DatabaseGenerated(DatabaseGeneratedOption.None)]
 public Int64 PolicyID { get; set; }
Community
  • 1
  • 1
  • 1
    Just for clarification: it should be `[DatabaseGenerated(DatabaseGeneratedOption.None)]`, not `[DatabaseGenerated(DatabaseGenerationOption.None)]` – z00l Aug 18 '15 at 08:48
2

Try add this to your OnModelCreating:

modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

That should remove the exception that model is out of date but till this time you must always synchronize model and database manually.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • That removes the error, but now I get a fresh error, as I have "Entity" mapped in the table and it's telling me that only primitive types are allowed. Do I need to re-seed the database at some point, and if so, how do I do it without destroying my data? (if the answer to the first part is yes I'll spawn this into a new question) – Jamie Howarth Apr 28 '11 at 15:29
  • What exactly did you changed in your code. I thought you only added `DatabaseGenerated` attribute but this complains about unmapped entity. You don't need to re-seed DB. – Ladislav Mrnka Apr 28 '11 at 18:18
  • Separate table, nothing changed, also just re-seeded it and still getting this error. – Jamie Howarth Apr 28 '11 at 18:45
  • Separate table is different then change one table so that a bigint is not an identity. – Ladislav Mrnka Apr 28 '11 at 19:15
  • Sorry, I mean the Entity is mapped. I ended up having to back up my data to a second database, re-gen'ing the model using a DropCreateDatabaseIfModelChanges policy, and then porting the data back. Thanks for all your help though :-) – Jamie Howarth May 05 '11 at 08:53