views:

36

answers:

1

Here's the table and code

public class Person {
    [Key] public int ID {get;set;}
    public string Name {get;set;}
}

var owner = new Person() { ID = 1, Name = "Owner" };
db.People.Add(Person);
db.SaveChanges();

The SaveChanges method does not add ID to the sql so I end up with the following sql query

exec sp_executesql N'insert [dbo].[People]([Name])
values (@0)
select [ID]
from [dbo].[People]
where @@ROWCOUNT > 0 and [ID] = scope_identity()',N'@0 varchar(50),@0='Owner'

Rather than the expected insert statement with ID passed in as well.

Is there another attribute I need to make sure the ID is passed as part of the query?

I'm sure it will be asked but it's a non-autoincrement on purpose.

A: 

Oddly enough, here's the answer for anyone else who might come across this problem.

modelBuilder.Entity<Person>()
            .HasKey(p => p.ID)
            .Property(p => p.ID)
            .StoreGeneratedPattern = StoreGeneratedPattern.Identity;

Hope this helps someone.

BuildStarted