0

I have a code first EF model class like this:

public class ContentEntry
{
    [Key, Column(Order = 0)]
    public int Id { get; set; }

    [Key, Column(Order = 1)]
    public string Language { get; set; }
}

Is it possible to auto increment Id if it is 0?

For example: I want to add a dataset like

Id = 1, Language = "En"

It works fine. Then, I want to add the following:

Id = 1, Language = "De"

It works fine too. But yet, I want to add this one:

Id = 0, Language = "En"

Id is 0, so the DB should generate an Id for me:

Id = 2, Language = "En"

Is there no way, I know I can generate an Id my self. But what happen if two threads try to add an element with the same Id? Can I prevent this?

Koopakiller
  • 2,679
  • 3
  • 27
  • 44
  • @Claies I changed my model's `Id` property to `int?`, but it does not work too. I get an `DbEntityValidationException` if I set `Id` to `null` and `Language` to a valid value. I get the message _The field Id is required_ as an additionbtal Information. `Language` will be a foreign key later, so every item can be accessed by the specific `Id` in the specific `Language` during every Item can be exists in every language. I think a splitted key is the best way to implement this. – Koopakiller May 11 '15 at 23:07

1 Answers1

0

Class

    [Table("Test")]
    class Test
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key, Column(Order = 0)]
        public int Id { get; set; }

        [Key, Column(Order = 1)]
        public string Language { get; set; }      

    }

Insert Rec

using (var db = new TestDbContext())
{
      db.Test.Add(new Test { Id = 0, Language = "En" });
      db.Test.Add(new Test { Id = 0, Language = "Sb" });

      db.SaveChanges();    
 }

Result

Id Language
1 En
2 Sb
3 En
4 Sb

enter image description here

Rrerence: Entity Framework: how to generate an auto-increment ID field

MKEF
  • 141
  • 2
  • 11
  • I tested your solution but it does not work for me. I get always the message, that I can not insert NULL to the Column `Id`... I think the problem in this case is the splitted key. – Koopakiller May 12 '15 at 16:23
  • I tried `code { Id = 1, Language = "En" }; { Id = 1, Language = "De" }; { Id = 0, Language = "En" }; ` They worked fine for me. FYI: I'm using EF = 6.1.3, SQL = 2014 – MKEF May 13 '15 at 03:54
  • I tried it with an empty table with insert `{Id=0,Language="En"}` Than I got the message _Cannot insert the value NULL into column 'Id', table Namespace.dbo.ContentEntries'; column does not allow nulls. INSERT fails.The statement has been terminated._ I use EF 6.1.3 and SQL Server 2014 too – Koopakiller May 13 '15 at 15:40
  • Better check the table Design in SQL Server Management Studio. Open your table in `Design` Select your column and go to `Column Properties` Under `code Indentity Specification` , Check if, `Is Identity=Yes and Indentity Increment=1` – MKEF May 14 '15 at 03:34
  • That is pretty strange...my DB has not updated correctly :/ I can now insert data sets. But If I set `id=1` it should be insert a data set with the `id=1` and the given language. Any idea? – Koopakiller May 14 '15 at 13:21
  • The reason why the table didn't get updated my be you don't have the `Migration Code` [link](http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application). `EF` creates the `Database` at the first run. It uses existing table model to create the tables and If you don't have `Migration Code`, later changes in the table model would not be applied. Now on the `id` column is auto incrementing so the `id` will automatically generated. – MKEF May 14 '15 at 14:15
  • I know the update concept of EF, so I used the PMC to update the DB. It worked always except the ID. But the main problem is still, that the ID should not be genarated if I pass a value not equal to 0. – Koopakiller May 14 '15 at 15:20
  • I believe that you are trying to insert `id` value explicitly (and also you want to db to auto generate ids ). As the current set up `IDENTITY_INSERT` is set to `OFF` in the table, db will always generate an id no matter what id value you pass(if you try it in SQL you will get an error). If you want to insert value to `id` you need to set `IDENTITY_INSERT OFF`. Reference: [Insert explicit value](http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity) ,[link](https://technet.microsoft.com/en-us/library/aa259221(v=sql.80).aspx) – MKEF May 15 '15 at 04:18