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

喜欢而已 提交于 2019-12-29 06:34:07

问题


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 attributes.


回答1:


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);



回答2:


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.




回答3:


Small Change

public class User
{

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

  public int ID { get; set; }

  // Rest of the code
}


来源:https://stackoverflow.com/questions/4999853/using-entity-framework-code-first-ctp5-how-do-i-create-a-primary-key-column-tha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!