开发者

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

开发者 https://www.devze.com 2023-02-10 06:44 出处:网络
Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

Using Entity Framework Code First CTP5, how do I create a primary key column that are INTs and are not identity columns

Preferably not using att开发者_如何学Pythonributes.


That answer didn't work for me (in EF 4.1).

To make this work, I added DatabaseGenerated attribute to my key column:

public class FacebookUser
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  public long FacebookId { get; set; }

  // ...
}

Hope this helps someone.


If you're building your entities using the fluent interface rather than using attributes over the properties, you may be able to use the DatabaseGenerationOption class (from EntityFramework.dll, in the System.ComponentModel.DataAnnotations namespace). I've not tested it, but this is what it would look like:

modelBuilder
    .Entity<Foo>()
    .Property(f => f.Id)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);


Small Change

public class User
{

  [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

  public int ID { get; set; }

  // Rest of the code
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号