EntityType在MVC 4中没有定义关键字 [英] EntityType has no key defined in MVC 4

查看:105
本文介绍了EntityType在MVC 4中没有定义关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习MVC在ASP.net网站上编写教程,但我无法理解一件事。

I have started to learn MVC making the tutorial on ASP.net website but I cannot understand one thing.

当我创建控制器和它给我的模型一个错误

When I create the controller and the model it gives me an error


System.Data.Entity.Edm.EdmEntityType::EntityType'TusofonaFields'
没有定义键。定义此EntityType的密钥。

System.Data.Entity.Edm.EdmEntityType: : EntityType 'TusofonaFields' has no key defined. Define the key for this EntityType.

我尝试在网络上搜索,他们总是表示它缺少[Key]但不幸的是,我的视觉工作室给了我一个错误:

I try to search on web, and they always says its missing the [Key] on Id, but unfortenatly my visual studio gives me an error:


找不到类型或命名空间名称Key(您是否缺少使用指令或装配参考?)

The type or namespace name 'Key' could not be found (are you missing a using directive or an assembly reference?)

我的代码:

public ActionResult Index()
{
    return View(db.site_membros.ToList());
}

模型:

namespace MvcApplication2
{
    public class TusofonaFields
    {
      public int IDMembro { get; set; }
      public string Alcunha { get; set; }        
      public DateTime Aniversario { get; set; }        
      public string Foto { get; set; }
    }

public class TusofonaTable : DbContext
{        
    public DbSet<TusofonaFields> site_membros { get; set; }
}   

}

推荐答案

嗯,错误告诉你发生了什么:你是缺少的参考。

Well, the error tells you what's going on: you are the missing reference.

在EntityFramework 4中:
检查项目的引用中是否有System.ComponentModel.DataAnnotations。
然后使用System.ComponentModel.DataAnnotations; 命名空间在cs文件的顶部添加或在上使用mouse2 [Key]> Resolve> ;使用System.ComponentModel.DataAnnotations;

In EntityFramework 4: Check that you have System.ComponentModel.DataAnnotations in project's references. Then add using System.ComponentModel.DataAnnotations; namespace at the top of the cs file or use mouse2 on [Key]>Resolve>using System.ComponentModel.DataAnnotations;

如果您使用EntityFramework 5,您需要的命名空间是 System.ComponentModel .DataAnnotations.Schema

If you are using EntityFramework 5 the namespace you need is System.ComponentModel.DataAnnotations.Schema

这篇关于EntityType在MVC 4中没有定义关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆