Code First Mapping with Data Annotations

Asked

Viewed 404 times

7

I am creating a project using mapping through DataAnnotations with Code First (similar to the Hibernate), without the use of FluentAPI.

It turns out that in implementing this process, some exceptions are being raised and I believe it is due to DataAnnotations I’m using in the wrong places.

So,

What is the function and use of DataAnnotations down below?

  • [ComplexType]

  • [InverseProperty]

  • [ForeignKey]

  • [ScaffoldColumn]

Obs: I’m having a problem with ForeignKeys, but I believe that if we understand well the DataAnnotations, I can solve the exception.

1 answer

7


[ComplexType]

Specifies that the class in question is a complex type, used for the construction of several others Models.

For example:

[ComplexType]
public class Address
{
    [Key]
    public int AddressId { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string PostalCode { get; set; }
}

public class Person
{
    [Key]
    public int PersonId { get; set; }
    ...
    public Address Address { get; set; }
}

public class Company
{
    [Key]
    public int CompanyId { get; set; }
    ...
    public Address Address { get; set; }
}

A ComplexType normally cannot be accessed directly by a data context.

[InverseProperty]

Serves to explicitly specify a relation N to 1. It is specified when the property name is not default.

For example:

public class Movimentacao
{
    [Key]
    public int MovimentacaoId { get; set; }
    public int ProdutoId { get; set; }
    public int UsuarioId { get; set; }

    [InverseProperty("UsuariosMovimentaramProdutos")]
    public virtual Produto Produto { get; set; }
    [InverseProperty("UsuariosMovimentaramProdutos")]
    public virtual Usuario Usuario { get; set; }
}

public class Usuario
{
    [Key]
    public int UsuarioId { get; set; }

    ...

    public virtual ICollection<Movimentacao> UsuariosMovimentaramProdutos { get; set; }
}

public class Produto
{
    [Key]
    public int ProdutoId { get; set; }

    ...

    public virtual ICollection<Movimentacao> UsuariosMovimentaramProdutos { get; set; }
}

[ForeignKey]

Serves to explicitly specify which foreign entity the foreign key property refers to. For example:

public class Usuario
{
    [Key]
    public int UsuarioId { get; set; }

    ...

    public virtual ICollection<Movimentacao> UsuariosMovimentaramProdutos { get; set; }
}

public class Produto
{
    [Key]
    public int ProdutoId { get; set; }
    [ForeignKey("Usuario")]
    public OperadorDeCadastroId { get; set; }

    ...

    public virtual ICollection<Movimentacao> Movimentacoes { get; set; }
    public virtual Usuario OperadorDeCadastro { get; set; }
}

[ScaffoldColumn]

Indicates for the engine of Scaffolding whether the column should be generated in its View.

It is usually used to nay place the property in View.

Browser other questions tagged

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