简体   繁体   English

实体框架5 MVC 4 - 脚手架错误 - 没有密钥

[英]Entity Framework 5 MVC 4 - Scaffolding Error - no key

Firstly i have to say i am a complete novice with MVC and EF. 首先,我不得不说我是MVC和EF的新手。 I have created Model using Database First, and selected two table Users ---> User Detail (related) 我使用Database First创建了Model,并选择了两个表用户---> User Detail(相关)

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. 现在当我尝试将它与MVC 4脚手架和选择的新控制器/ EF框架添加/更新/删除等结合起来时,我放入User Model + UserContext它抱怨自动生成的模型没有密钥。

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. 所以我打开模型,看到实际上有一个键,它有复选框Entity键,以及该属性旁边的小“关键”符号。

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? 我到处寻找唯一可行的方法是手动打开User.cs并将[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: 从来没有我离题,现在我已经为User和UserDetails cs文件添加了密钥,它停止抱怨,现在开始抱怨:

Unable to retrieve MetaData: for User. 无法检索MetaData:for User。 Unable to determine the principle end of an association between User and UserDetail. 无法确定User和UserDetail之间关联的主要结束。 The principle end of this association must be explicitly configured using either the relationship fluent API or data annotations. 必须使用关系流畅API或数据注释显式配置此关联的主要结束。

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? 2 /我如何解决第二个问题?

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: 假设(如果有)您正确实现了UsersUserDetails之间的关系,您可以尝试以下操作,这就是我解决错误的方法:

In your Web.config file (the root one, at the very end of solution explorer) change name in your connection string from this... 在您的Web.config文件(根目录,在解决方案资源管理器的最后)更改连接字符串中的name ...

<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. 只需更改name ,别的。 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. 密钥用于告诉实体这是带有数据注释的主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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