Error inserting into the BD using EF

0

I have this model generated by EF

 public partial class Ventas
{
    public Ventas()
    {
        this.Caja = new HashSet<Caja>();
        this.VentasLinias = new HashSet<VentasLinias>();
    }

    public string weblogin { get; set; }
    ...
    ...

}

But I use this other one by means of automapper to do the operations of insertion in the BD.

public class VentasModel
{
    public string weblogin { get; set; }
    ...
    public List<Caja> caja { get; set; }
    public List<VentasLinias> linias { get; set; }
}

When trying to save in the BD, this error gives me the code I use is this

 try
        {
            var destino = mapper.Map<VentasModel, Ventas>(venta);
            db.Ventas.Add(destino);
            db.SaveChanges();
        }
    catch (Exception ex)
        {
            string error = ex.Message;
        }

The error I receive is this

  

Only keys generated by storage for identity columns are supported. The key column 'weblogin' has the type 'SqlServer.varchar', which is not a valid type for an identity column.

In the database I have this table where I consider a key composed of these three fields.

Thank you,

    
asked by ilernet 27.05.2017 в 17:51
source

1 answer

0

Add a new column CajaId to your table Caja , then update your class to note that your primary key will be CajaId .

weblogin , id , tiendaventa can be used as a natural key to your application.

    
answered by 08.06.2017 в 16:43