Property containing only the KeyAttribute attribute resulting in a self-incrementing column in the database

2

If I'm not mistaken, this must have come in version 6.1.1 of EntityFramework and it was not like that in previous versions.

I believe that a column only became auto-increment when the DatabaseGenerated attribute was declared with the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] signature.

I have a simple classes, like this:

public class Municipio
{
    [Key]
    public int Id { get; set; }

    [StringLength(50)]
    [Required(AllowEmptyStrings = false)]
    public string Descricao { get; set; }

    [StringLength(2)]
    [Required(AllowEmptyStrings = false)]
    public string Uf { get; set; }
}

Note that the Id property is declared with the KeyAttribute attribute only. However, an Autoincrement column is being generated.

How to prevent this from happening?

    
asked by anonymous 13.10.2014 / 21:53

1 answer

1

I believe a column only became auto-increment when the DatabaseGenerated attribute was declared with the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] signature.

Not necessarily. Since the Entity Framework 5 the use of the attribute is optional.

How do you prevent this from happening?

Using [DatabaseGenerated(DatabaseGeneratedOption.None)] .

    
13.10.2014 / 22:25