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

Asked

Viewed 398 times

0

Views doesn’t have a primary key so it causes me to make a mistake:

Error: The number of Primary key values passed must match number of Primary key values defined on the Entity.

How could I avoid this mistake?

public ActionResult AtualizaCliente(int id)
{
    sistema_mobileEntities dao = new sistema_mobileEntities();
    return View(dao.vcliente.Find(id));
}
  • 3

    You have this PK set in your model ? Because what the error ta saying is that you don’t have the PK set...

  • 1

    Are you using Entityframework not? Which version?

  • @Érikthiago, yes, you’re right about the error message, more in my doubt I talk I’m working with a Views,so I don’t have a primary key.

  • If you don’t have a primary key, Find will not work. You can try to get otherwise: return View(dao.vcliente.FirstOrDefault(v => v.Id == id));

  • @itasouza Would the question not be How to map database views in Entityframework?

  • In ASP.NET MVC the term View is something else entirely different from View in the database.

Show 1 more comment

1 answer

4

Is totally wrong work on ASP.NET MVC and Entity Framework without using primary keys. Using Entity Framework presupposes the use of primary keys in all entities. It is a Framework design feature that simply cannot be circumvented.

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

Take as an example the supposed class used by the question, called Cliente:

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

    ...
}

Consider the above reticence 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 noted as [Key]) and the Find already works.

  • Do you know how it is possible to define a primary key in a Views? Entity Framework does not answer this?

  • 1

    You are misunderstanding the concept. In MVC, you do not define primary keys in Views. Views are part of the presentation of the system, that is, the place where it is defined how the information should appear to the user. The place where keys are defined is Model, which is where the entities and the relations between them are defined.

  • 1

    Don’t take this the wrong way, but your conceptual explanation is very complicated, both in this and in other questions that I was able to verify, because there are several ways to create the models is in my case was created by the Entity Framework , within the Model.tt is created the class of each table is there does not have a primary key definition. It would not be simpler in place of a conceptual answer an example in the code?

  • @itasouza Model.tt is an old method of creating a class through a modeling done through an EDMX file. This has nothing to do with not needing to create the primary key. This interpretation is totally yours. Let me give you an example to make this clear.

Browser other questions tagged

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