Entity Framework attribute list for entity mapping

Asked

Viewed 937 times

4

For my sake Database 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 (e.g.: [Key], [ForeignKey]) to "dribble" possible problems that may occur.

Thank you in advance!

  • 1

    https://msdn.microsoft.com/en-us/data/jj591583.aspx

  • 2

    I was going to put in an answer, but I’m sure the Gypsy will be finishing one :p

  • 2

    Hello. Stand by.

  • Awesome guys! Why didn’t I discover this community before?! haha

  • 1

    Welcome and enjoy =D

1 answer

6


Most of the attributes are here and here, but I will make a quick guide that can be useful for your conversion.

Field attributes (properties)

[Key]

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

It also has a parameter called Column, where it is possible to specify the order of each key.

[Column]

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

[ForeignKey]

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

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

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

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

[InverseProperty]

Blurs ambiguities when a table has multiple relations 1 to N to 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]

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

[DatabaseGenerated]

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

Accepts an enumeration as a parameter, which has the following values:

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

Class attributes

[Table]

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

I intend to enrich this answer as questions and examples appear, but it is a good starting point.

  • What if I want a specific field of another class with a different name? For example: public partial class User { public int id{ get; set; } public string name { get; set; } public active bool { get; set; } [This property comes from another "Nivel" model and its name is "name"] public string Nivelname { get; set; }

  • There your modeling is incorrect, because properties of other classes should be in other classes.

  • So there is no possibility to put fields from another table (levels) directly in the user model? As in the cited example, the name of the direct level in the user model?

  • 1

    It doesn’t exist. You can refer to another class instead. The Entity Framework loads it itself for you.

Browser other questions tagged

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