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

3 Answers 3

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); } 
Sign up to request clarification or add additional context in comments.

Comments

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; } 

1 Comment

Just for clarification: it should be [DatabaseGenerated(DatabaseGeneratedOption.None)], not [DatabaseGenerated(DatabaseGenerationOption.None)]
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.

5 Comments

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)
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.
Separate table, nothing changed, also just re-seeded it and still getting this error.
Separate table is different then change one table so that a bigint is not an identity.
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 :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.