How to work with Database Views in ASP.NET MVC?

0

Views does not have a primary key then it generates the error:

  

Error: The number of primary key values passed must match number of   primary key values defined on the entity.

How could you do to avoid this error?

public ActionResult AtualizaCliente(int id)
{
    sistema_mobileEntities dao = new sistema_mobileEntities();
    return View(dao.vcliente.Find(id));
}
    
asked by anonymous 08.04.2015 / 03:41

1 answer

4

It's totally wrong to work in ASP.NET MVC and Entity Framework without using primary keys. Using Entity Framework assumes the use of primary keys in all entities. It is a design feature of the Framework that simply can not be fooled.

In the question below the OS the answers are consensual . Therefore, set a primary key to be able to use Find .

Let's take the supposed class used by the question, called Cliente :

public class Cliente 
{
    [Key]
    public int ClienteId { get; set; }

    ...
}

Consider the above ellipsis as the other fields that are part of your Model .

The way it is, it's already right. Your Model already has a primary key (I wrote it as [Key] ) and Find already works.

    
08.04.2015 / 15:57