1

I choose Database First: Imgur

Here is an example table that is experiencing this issue. As you can see the EntityId column is the Primary Key:

Imgur

The imported table in the model browser shows that it has the Primary Key:

Imgur

But the code for the generated class does not have the EntityId column decorated with a Key attribute:

Imgur

At run time I get this error:

Additional information: One or more validation errors were detected during model generation: EntityType 'Entity' has no key defined. Define the key for this EntityType.

Why do I have to manually decorate the EntityId column with the Key Attribtue? Shouldnt EntityFramework take care of all that considering it is Database first?

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24
party_Rob
  • 77
  • 7
  • You seem to be using a regular connection string, not an entity connection string (with metadata paths). – Gert Arnold Feb 08 '17 at 16:19
  • @Gert Arnold - You were correct. The connection string used did not have the metadata part. Once i added it, I was able to perform operations on the Entity collection on the Context. – party_Rob Feb 15 '17 at 13:58
  • The images included have since broken. This question relies upon them and is now useless. If you can fix the images, please do. – bendl Apr 05 '21 at 13:13

2 Answers2

0

Typically speaking I have experience with EF4 through EF 6.1.3 and a teeny bit with Entity Core (was EF7 and then MS fun with naming). Typically if you are doing database first you do not get an adornment from your t4 template. I just looked at mine just now and I have no adornment for the key, just for constructor and the reference to the navigation of teOrder back to it.

enter image description here

I can save an Entity just fine and my code runs with this:

  using (var context = new EntityTesting.TesterEntities())
  {
    var nPerson = new tePerson { FirstName = "Test", LastName = "Tester" };

    context.tePerson.Add(nPerson);
    context.SaveChanges();
  }

What I would suggest is:

  1. Go to your (name).edmx Entity File and on the design surface wipe out the object and then replace it on the surface. In countless times this has fixed issues with the generated objects. When you SAVE it should auto run the T4 templates (*.tt files). If not you can select them, right click and select 'Run Custom Tool'. This just generates the POCO objects or the Context objects.

  2. If this is really a fault with the table it is typically with it NOT having a key. You are showing it does though. Is there anyway to mirror the exact same table logic and confirm the key has nothing out of the ordinary and is just a plain old key Primary Key?

  3. Create a brand new table with similar structure but not an exact copy and a new Entity File and confirm you can create it.

Tables are pretty straight forward with EF, or as straight forward as EF can be. You create them in SQL, ensure you have a key, add it to a design surface, save, it generates the objects for you. If you have other things under the hood like custom procs hooked to it or other out of the ordinary nav items that would be one thing. The only other thing would be if it has a very old SQL Type as the key. I know that the 'Text' type and EF do not play nice together. There may be other types that behave the same way.

djangojazz
  • 14,131
  • 10
  • 56
  • 94
0

This issue was fixed by including the "metadata" part of the connection string. At first my connection string looked like this:

data source=.;initial catalog=TestDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework;

Which produced the error.

Changing my connection string to this:

metadata=res://*/DbContexts.TestContext.csdl|res://*/DbContexts.TestContext.ssdl|res://*/DbContexts.TestContext.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=TestDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"

Allowed operations on the Context to be performed with no error encountered

party_Rob
  • 77
  • 7