List of Entity Framework attributes for entity mapping

4
For my Database to be outside the EF convention, and as another system already developed uses it, it is out of the question to change table / column names.

I would like to know the available attributes (ex: [Key] , [ForeignKey] ) to "dribble" possible problems that may occur.

Thank you in advance!

    
asked by anonymous 19.10.2015 / 17:46

1 answer

6

Most attributes are here and here , but I'll make one quick guide that can be useful for your conversion.

Attributes of fields (properties)

[Key]

Specifies when a property is or is part of a key. Can be used in more than one parameter.

It also has a parameter called Column , where you can specify the order of each key.

[Column]

Specifies the name of the column when it is different from the name of the property.

[ForeignKey]

Specifies which property represents foreign key binding in a Model . Can be used in two cases:

  • In a data property, to specify which navigation property refers to:

    [ForeignKey("MinhaPropriedadeDeNavegacao")]
    public int MinhaChaveEstrangeira { get; set; }
    
    public virtual TabelaEstrangeira MinhaPropriedadeDeNavegacao { get; set; }
    
  • In a navigation property, to specify which data property it refers to:

    public int MinhaChaveEstrangeira { get; set; }
    
    [ForeignKey("MinhaChaveEstrangeira")]
    public virtual TabelaEstrangeira MinhaPropriedadeDeNavegacao { get; set; }
    

[InverseProperty]

Undoes ambiguities when a table has multiple ratios 1 to N for another table.

    public class Orientador
    {
        public int OrientadorId { get; set; }

        [InverseProperty("OrientadorAntigo")]
        public virtual ICollection<Aluno> AlunosAntigos { get; set; }
        [InverseProperty("OrientadorNovo")]
        public virtual ICollection<Aluno> AlunosNovos { get; set; }
    }

    public class Aluno
    {
        public int AlunoId { get; set; }
        public int OrientadorAntigoId { get; set; }
        public int OrientadorNovoId { get; set; }

        public virtual Orientador OrientadorAntigo { get; set; }
        public virtual Orientador OrientadorNovo { get; set; }
    }

[NotMapped]

Indicates to the Entity Framework that a property does not exist in the database. It can be an auxiliary field that only appears on screen or is used for some function in Controller .

[DatabaseGenerated]

Indicates that the field will not be filled in the application, but in the database, by some specific rule.

Parameterized , which has the following values:

  • Identity : sequential generation made by the bank;
  • None : is not explicitly generated by the bank;
  • Computed : generation by some database calculation, such as a Trigger , for example.

Class Attributes

[Table]

Specifies the table name if it is different from the Model class name.

  

I want to enrich this response as questions arise and need examples, but it is a good starting point.

    
19.10.2015 / 18:03