实体框架自动递增字段,这不是Id [英] Entity Framework auto incrementing field, that isn't the Id
本文介绍了实体框架自动递增字段,这不是Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有没有在代码中定义自动递增字段,还是自己创建列,并定义数据库的自动递增工作?
解决方案
您可以使用 DatabaseGenerated(DatabaseGeneratedOption.Identity )
。 EF只允许每个表单个身份列。
public class Foo
{
[Key]
public Guid Id {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Bar {get;组; }
}
I know this isn't the most ideal solution, but I need to add an auto incrementing field to one of my EF Code First objects. This column id NOT the Id, which is a guid.
Is there anyway for me to define the auto incrementing field in code, or would creating the column myself and defining in the DB that its auto incrementing work?
解决方案
You can annotate that property with DatabaseGenerated(DatabaseGeneratedOption.Identity)
. EF allows only single identity column per table.
public class Foo
{
[Key]
public Guid Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Bar { get; set; }
}
这篇关于实体框架自动递增字段,这不是Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文