MVC3 - Error configuring a controller with Entity infrastructure

The steps that I follow ...

  • Add a new ADO.NET entity data model> Create from database> Install a new connection string in adventureworks db> Next> Select the "DatabaseLog" table> Done. Verify that the DatabaseLog is displayed in the edmx view.

  • Right Click Controller> Add Controller

  • Template
    Controller with read / write actions and views using Entity

  • Model class
    AdventureWorksDWEntities

  • Context
    New Data Context> Accept Default Name

  • View
    Razor

  • Click Add.

To produce an error:
"Failed to get metadata for" DatabaseDocumentor.models.AdventureWorksDWEntities ".

  • System.Data.Edm.EdmEntityeType: EntityType "AdventureWorksDWEntities" does not have a specific key. Define a key for this entity type.

  • System.Data.Edm.EdmEntitySet: EntityType: EntitySet "AdventureWorksDWEntities" is based on the type "AdventureWorksDWEntities", which does not have specific keys.


I tried again using AdventureWorks (not AdventureWorksDW), and this time it worked. But I still do not understand what to choose when creating the controller. I have 3 options:

Template
Here I chose Controller with read / write actions and views using Entity. This is easy enough to understand. I want my tables to be created for me, so I select this option.

Model
This is what I want to simulate. In this case, I want to simulate the Department table. Therefore, I choose the Department.

Context
This is completely fuzzy for me. I selected * Name * Entities. This value is in the web.config connection string area. Why do I need to select a connection string as context? I only know the context as "the object that I use to access my entities" in C #. So, it's hard for me to imagine here. Should I always select a connection string for context?

+4
source share
4 answers

This problem may occur if the context is not selected correctly from the drop-down list. The context should be the value stored in the web.config file

<add name="NamedEntitiesCs1" 

which also contains the model you want to generate.

+3
source

I found that the problem ...

I have a three-tier architecture that I use with each of the following projects in one solution.

1.YeagerTech 2.YeagerTechWcfService 3.YeagerTechModel

Regardless, even though my wcf service refers to my model, the launch project (1) is not smart enough to recognize metadata to create a controller. In this case, you should include a link to your project, which includes your edmx model.

You should also make sure that the connection string is also in the startup project (1) via the web.config file in order to get the connection for metadata.

+2
source

I found the answer, the model class must have a key, that is, the ID property ie public int ID {get; ask;} save changes and build or rebuild the solution. It should be able to work.

+1
source

your property in your model for the identifier must be declared public. rebuild and try again, it should work

0
source

All Articles