Use an SQL Query as the source of a Model in the Entity Framework

0

Good morning!

I comment, I'm having the need to add a Model in EF, but with the peculiarity that you use a Query SQL as a source of information instead of a table.

I tried to do it by the normal ways in the following way

[Table("[dbo].[ViewCuboVenta]")]
public class CuboVentas
{
    [Key]
    [Column("[ClienteRazon]",Order = 2)]
    public string Cliente { get; set; }

    [Key]
    [Column("[NumeroCotizacion]",Order = 0)]
    public string Nota_de_Venta { get; set; }

    [Key]
    [Column("[ArtiCodiCompuesto]",Order = 1)]
    public string Cod_Fabricacion { get; set; }
}

The issue is that it throws an exception saying that none of my columns exist.

  

SqlException: Invalid column name '[CallNumber]'.   Invalid column name '[NumeroCotizacion]'.   Invalid column name '[ArtiCodiCompuesto]'.   Invalid column name '[ClientRazon]'.

The issue of consulting a query is an obligation, since I do not have access to the complete database, but only some Query, and I would need this to use it as a Repository, (have the client's name, quote and article purchased)

I clarify that being a Query does not really have primary Keys in the database, but if I do not assemble it as a composite key I will have repetition of primary keys in the model

Is there a way to do it with a DBContext? or directly the solution is to perform the query in a conventional way?

Thank you very much for your time!

    
asked by Juan Salvador Portugal 09.04.2018 в 14:39
source

1 answer

0

Analyzing the description of your question I understand that you were given access to several views within the database and you want to map them in a DBContext, since the database is already created it is good that you use the tool Entity Data Model Tools so you can map those views to entities and have a DBContext that accesses that data.

This answer is based on what I can understand about your question. If you can be more specific, it would be much better.

    
answered by 19.04.2018 в 19:55