Error using Code First From Database

2

I'm trying to use the Entity Framework with a SQLServer database that I created out of Visual Studio. The problem is that when I add the ADO.NET Entity data model in the project to make the relation, it is failing to create one of the classes to reference with the table in the database.

My bank has these tables:

AfterchoosingtheADO.NETEntitydataModel,IchoosetheCodeFirstFromDatabaseoptionandselecttoimportalltables:

However, the SalesProfile table is not imported from the others. It is imported only if I select only it. What can be happening?

    
asked by anonymous 15.10.2017 / 20:49

1 answer

2

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

It should be noted that the properties are virtual ( virtual ), ie the EF maps them as navigation property (read recommended)

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

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

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 PRODUCT collection.

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 template - A justification

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

If at random, your Product Sales table 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 of filling in 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
Relationship Many To Many Entity Framework 6 List, Virtual and Entity Framework

    
01.11.2017 / 13:23