ADO.NET Entity Framework and identity columns

前端 未结 10 1480
甜味超标
甜味超标 2020-12-29 18:06

Is the Entity Framework aware of identity columns?

I am using SQL Server 2005 Express Edition and have several tables where the primary key is an identity column. w

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 18:38

    In C# you can do something like this to make it aware:

    In your FooConfiguration.cs : EntityTypeConfiguration:

    this.Property(x => x.foo).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    

    Then to use it, just be sure to insert the item into the context and call context.SaveChanges() before using x.foo to get the updated auto-incremented value. Otherwise x.foo will just be 0 or null.

提交回复
热议问题