使用Entity Framework创建新记录

4 c# sql-server entity-framework

我有一个表,其主键id是一个标识列.我认为在创建新记录时,id将增加1.

但是我发现在调试时它是0.为什么?

必要的代码:

  public DbSet<testDetails> testDetailRecords { get; set; }
  testDetails test = testContext.testDetailRecords.Create();
  // test.id is 0 when hover over it
  public class testDetails : IEntityWithRelationships
  {
      [Key]
       [Required]
       [Display(Name = "Primary Key", Description = "Primary Key")]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public long id { get; set; }
Run Code Online (Sandbox Code Playgroud)

pod*_*ska 9

在使用a提交记录之前,不会创建记录SaveChanges().


Jos*_*osh 5

它是 0,因为它还没有创建。

        TestContext context = new TestContext();

        var increment = context.IncrementTest.Create();
        increment.name = "testbyme";

        context.IncrementTest.Add(increment);


        context.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

这对我来说没有任何异常。