简体   繁体   中英

Entity Framework 5 MVC 4 - Scaffolding Error - no key

Firstly i have to say i am a complete novice with MVC and EF. I have created Model using Database First, and selected two table Users ---> User Detail (related)

The models are generated fine, they validate fine and if i try do simple data access this works fine too.

Now when i try combine this with MVC 4 scaffolding and selected new controllers/EF framework add/update/delete etc, I put in User Model + UserContext it complains that the auto-generated Model has no key.

Entity Type - "User" has not key. So i open the Model and see that in fact there is a key, and it has the check box Entity key ticked, as well as the little "key" sign next to the property.

I have looked everywhere the only solution that works is to manually open up the User.cs and put the [Key] attribute next to the key - this to me is scary because this file is clearly autogenerated and i would not want to have to do this for every key? am i doing something wrong?

Never the less i digress, now that i have added the key to both User and UserDetails cs files, it stops complaining and now starts complaining about:

Unable to retrieve MetaData: for User. Unable to determine the principle end of an association between User and UserDetail. The principle end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I have two questions now:

  1. should i be messing with the autogenerated files like this? or am i doing something wrong? 2/ How do i solve this second issue?

I feel like something is wrong because i shouldn't be hitting problem after problem like this?

I had a similar "unable to retrieve meta data for type..." error. Assuming (if there is one) you have the relationship between Users and UserDetails implemented correctly, you could try the following, which is what I did to resolve the error:

In your Web.config file (the root one, at the very end of solution explorer) change name in your connection string from this...

<connectionStrings>
 <add name="YourContext" connectionString="Data Source=|DataDirectory|YourNamespace.Models.YourDBName.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

...to this:

<connectionStrings>
 <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|YourNamespace.Models.YourDBName.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

Just change name , nothing else. Then build your solution and try run it. Could be a long shot, but it worked for me. More info from here , where I asked a similar question.

The key is used as follows:

public class Item()
{
    [Key]
    public int Id {get;set;}
    public string Name {get;set;}
}

The key is used to tell Entity that this is the primary key with Data Annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM