2

Using Entity Framework SQL-Server-CE I have created a table like this:

class Era {
    public long Id { get; set; }
    [Index(IsUnique=true), Required]
    public long Code { get; set; }
    public string Name { get; set; }
    public long StartDate { get; set; }
    public long EndDate { get; set; }
}

Whenever I try to insert into it using Entity Framework, the Id field of the Era object is ignored and an auto incremented Id is inserted.

How can I make the Id field not to use the auto incremented value, and use the given value instead, using annotations preferably.

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
raven
  • 775
  • 6
  • 22

3 Answers3

6
[DatabaseGenerated(DatabaseGeneratedOption.None)] 

This ensures that EF will not try and change the values and will simply insert them with the rest of the data.

here is detail description about it link

sm.abdullah
  • 1,777
  • 1
  • 17
  • 34
  • The link is about switching identity insert on. You don't need to do that if you create a primary key that is not database generated. – Colin Jun 08 '15 at 13:16
2

I think you need:

[DatabaseGenerated(DatabaseGeneratedOption.None)]

The default is that the database generates a value, so you need to explicitly turn that off.

Dennis_E
  • 8,751
  • 23
  • 29
  • adding that annotation and trying to insert results in this exception: System.Data.SqlServerCe.SqlCeException: The column cannot be modified. [ Column name = Id ] – raven Jun 08 '15 at 09:41
  • @raven as I mention see this link http://blog.robertobonini.com/2014/10/09/entity-framework-with-identity-insert/ – sm.abdullah Jun 08 '15 at 09:59
  • 1
    @raven After adding that annotation you must recreate the database – ErikEJ Jun 08 '15 at 10:15
  • @sm.abdullah i'm trying that out right now, but in that example an sql connection string is required, but i'm using sql compact and i have no idea what to use there. – raven Jun 08 '15 at 10:20
  • @ErikEJ yes i have updated the database using package manager console – raven Jun 08 '15 at 10:21
  • @raven updating the database does not necessarily drop and re-create the table or the database http://stackoverflow.com/a/18917348 – Colin Jun 08 '15 at 13:19
  • @Colin i did not know that. are you saying that if you recreate the table you wont need to do the stuff i've done in my answer? – raven Jun 09 '15 at 13:52
  • Yes, and that is why @ErikEJ said you should recreate the database. If the table is empty that's not a problem. If you need to fix up data you could use a custom migration: http://romiller.com/2013/04/30/ef6-switching-identity-onoff-with-a-custom-migration-operation/ – Colin Jun 09 '15 at 16:14
0

so this is the answer that i was looking for:

if you want to have a table with non-auto increment id in entity framework; you need to first backup the data, drop the table(this can be done by commenting the relevant DbSet in your DbContext class), adding the annotation [DatabaseGenerated(DatabaseGeneratedOption.None)] then migrating and updating database.

raven
  • 775
  • 6
  • 22
  • Your root problem is that the column is IDENTITY when it should be `DatabaseGeneratedOption.None`. You have found an interesting workaround, but really you should change the column. You are correct about needing to do more than add a data annotation if you have already created the table....http://stackoverflow.com/a/18917348 – Colin Jun 08 '15 at 13:13