4
votes

The steps I go through...

  • Add new ADO.NET Entity Data Model > Generate from DB > Setup new connection string to adventureworks db > Next > Select table "DatabaseLog" > Finish. Verify DatabaseLog is visible 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.

Produce Error:
"Unable to retrieve metadata for 'DatabaseDocumentor.models.AdventureWorksDWEntities'.

  • System.Data.Edm.EdmEntityeType: EntityType 'AdventureWorksDWEntities' has no key defined. Define the key for this entitytype.

  • System.Data.Edm.EdmEntitySet: EntityType: EntitySet 'AdventureWorksDWEntities' is based on type 'AdventureWorksDWEntities' that has no keys defined.


I tried again using AdventureWorks (not AdventureWorksDW) and this time it worked. But, I still don't understand what to pick when generating a controller. I have 3 options:

Template
Here I picked Controller with read/write actions and views, using Entity. This is easy enough to understand. I want to have my tables generated for me so I pick this option.

Model
This is what I want to model. In this case I want to model the "Department" table. So I choose Department.

Context
This one is real fuzzy to me. I chose *Name*Entities. This is the value in the web.config connection strings area. Why do I need to choose my connection string as the context? I only know context as "an object that I use to get to my entities" in C#. So, here it's hard for me to visualize. Do I need to always choose my connection string for the context?

4
@Eranga - That answer does not correct my issue nor does it address my updated question. I know keys are required on tables in some ORM's. Its the same in SubSonic. That is not my question. Not a dup.P.Brian.Mackey
Did you specify a PK? if you dont give a PK how can EF or nHibernate uniquely identify entity? true it does not give answer to your second questionEranga
The DB is AdventureWorks created by Microsoft for testing. The tables are already keyed. I can now generate items properly without error. The only question I have left is the update regarding explanation of the Context. The original problem was that I did not choose the correct Context.P.Brian.Mackey

4 Answers

3
votes

This issue can occur when the Context is not correctly chosen from the dropdown. The context should be the value stored in the web.config

<add name="NamedEntitiesCs1"

that also contains the Model you want to generate.

2
votes

I found what the issue is...

I have a 3 tiered architiecture I'm using with each of the below projects in one solution.

1.YeagerTech 2.YeagerTechWcfService 3.YeagerTechModel

No matter what, even though my wcf service references my model, the startup project (1) is not "smart" enough to recognize the metadata to create the Controller. In this case, you must include a reference to your project that includes your edmx model.

You must also ensure that the connectionstring also resides in the startup project (1) via the web.config file, in order to get the connection for the metadata.

1
votes

I found the answer, the model class should have a key, that is an ID property i.e public int ID { get; set;} save the changes and the build or rebuild the solution. That should be able to work out.

0
votes

your property in your Model for the ID must be declared as public. rebuild and try again, it should work