Property containing only the Keyattribute attribute resulting in auto-increment column in the database

Asked

Viewed 109 times

2

If I’m not mistaken, this must have come in version 6.1.1 of Entityframework and it wasn’t like this in previous versions.

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

I have a simple class, 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; }
}

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

Amostragem da coluna sendo gerada como Identity

How to stop it from happening?

1 answer

1


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

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

How to stop it from happening?

Using [DatabaseGenerated(DatabaseGeneratedOption.None)].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.