Error using Code First From Database

Asked

Viewed 128 times

2

I’m trying to use the Entity Framework with a Sqlserver database that I created outside of Visual Studio. The problem is that when I add the ADO.NET Entity data Model to the project to do the relation, it is failing to create one of the classes to reference with the table in the database.

My bank has these tables:

inserir a descrição da imagem aqui

After choosing ADO.NET Entity data Model, I choose Code First From Database and select to import all tables: inserir a descrição da imagem aqui

However the Productovenda table is not imported along with the others. It is only imported if I select just it. What may be happening?

  • The model class is my context, should I leave it with the name of the class I want to import from the bank? I don’t understand

  • @Vanderson, if I understand your question... it will not really be created the entity Productovenda, because, if it is a Relationship N to N, that is, the Product Entity has a Sales Collection and the Sale entity has a Product Collection, this is an EF convention, so it’s correct.

  • Personal really is a relationship from N to N. I thought the Entity would create the Product entity because when I go to create a table by Entity I have to create the Product class in visual studio.

1 answer

2

If your relationship entity has only the references to the primary tables, then the RU will only treat it as a collection:

Note that the properties are virtual (virtual), that is, the RU maps them as navigation property (recommended reading).

In this case, the navigation property only signals to the EF that a related record can be found in the specified entity. In the examples below it has been indicated that a Product may (or may not) be associated with several SALES or a Sale may (or may not) have several PRODUCTS.

Product
See in this template there is a collection of SALE.

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("Produto")]
public partial class Produto
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Produto()
    {
        Venda = new HashSet<Venda>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int idProduto { get; set; }

    [StringLength(10)]
    public string descricao { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Venda> Venda { get; set; }
}

Sale
And in this model, a collection of PRODUCT.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("Venda")]
public partial class Venda
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Venda()
    {
        Produto = new HashSet<Produto>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int idVenda { get; set; }

    public DateTime? referencia { get; set; }

    public int? qtd { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Produto> Produto { get; set; }
}

To create the model - A justification

If you notice, there is no reason for EF to create a template since all fields will be automatically filled in when you link a product to sale or vice versa.

If at random, your Sales Tableproduct has other fields (besides foreign fields), EF will create a template for it, so you can design your application and provide the user with a means to fill these fields.

Note that I simply created the Discount field and the template was created:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("ProdutoVenda")]
public partial class ProdutoVenda
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int idProduto { get; set; }

    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int idVenda { get; set; }

    public double? desconto { get; set; }

    public virtual Produto Produto { get; set; }

    public virtual Venda Venda { get; set; }
}

Other readings
Many Relationship for Many Entity Framework 6
List, virtual and Entity Framework

Browser other questions tagged

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