简体   繁体   English

实体框架4.3.1 - > 5.0异常:“EntityType没有定义键。 定义此EntityType的键。“

[英]Entity Framework 4.3.1 -> 5.0 Exception: “EntityType has no key defined. Define the key for this EntityType.”

In Entity Framework 4.3.1 I had the following Models, and they worked fine: 在Entity Framework 4.3.1中,我有以下模型,它们工作正常:

public class BaseUser
{
    [Key]
    [MaxLength(100)]
    public string Username { get; set; }
}

[Table("AppUser")] // Ensures EF Code First uses Table-per-Type
public class AppUser : BaseUser
{
    [MaxLength(200)]
    public string About { get; set; }
}

After upgrading to EF 5.0, I get the following exception when I try to run this application and access the associated DbContext: 升级到EF 5.0后,当我尝试运行此应用程序并访问关联的DbContext时,我收到以下异常:

EntityType 'AppUser' has no key defined. Define the key for this EntityType.

How do I resolve this issue? 我该如何解决这个问题? This appears to be a regression. 这似乎是一种回归。

This Exception is caused by mixing EF versions. 此异常是由EF版本混合引起的。

BaseUser was defined in a shared Project that uses EF 4.3.1. BaseUser是在使用EF 4.3.1的共享项目中定义的。 AppUser was in a Project using EF 5.0. AppUser在使用EF 5.0的项目中。 This confusing Exception is a result of trying to inherit from a model in a Project that has not been upgraded to EF 5.0. 这种令人困惑的异常是尝试从尚未升级到EF 5.0的项目中的模型继承的结果。 Splitting the shared project and upgrading the one referenced here resolved the issue. 拆分共享项目并升级此处引用的项目解决了该问题。

If others run into this it's worth pointing out that upgrading is a bit awkward. 如果其他人碰到这一点,值得指出升级有点尴尬。 Since NuGet doesn't seem to notice the need for an upgrade, it lets you install EF 5.0 on top of it, lists the new version as 4.4 after install, and requires you to include both of the following using statements in your Model classes since some of the EF data annotations were moved (redirected), and some were not: 由于NuGet似乎没有注意到升级的需要,它允许您在其上安装EF 5.0,在安装后将新版本列为4.4,并要求您在Model类中包含以下两个using语句一些EF数据注释被移动(重定向),有些则不是:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

暂无
暂无

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

相关问题 EntityType''没有定义键。 定义此EntityType的键。 -C#Web API实体框架 - EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity Framework EF 5.0 Fluent映射:“ EntityType'{ClassMap}'”未定义键。 定义此EntityType的密钥。” - EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.” EntityType没有定义键。 定义此EntityType的键 - EntityType has no key defined. Define the key for this EntityType EntityType X没有定义键。 定义此EntityType的键 - EntityType X has no key defined. Define the key for this EntityType EntityType“帖子”没有定义键。 定义此EntityType的键 - EntityType 'posts' has no key defined. Define the key for this EntityType EntityType'MyProfile'没有定义键。 定义此EntityType的键 - EntityType 'MyProfile' has no key defined. Define the key for this EntityType EntityType 'Worker' 没有定义键。 定义此 EntityType 的键 - EntityType 'Worker' has no key defined. Define the key for this EntityType 实体框架验证错误没有定义键。 定义此EntityType的键 - Entity framework Validation errors has no key defined. Define the key for this EntityType 模型验证错误:EntityType 未定义键。 定义此 EntityType 的键 - Model validation error: EntityType has no key defined. Define the key for this EntityType 数据库第一个EntityType'表'没有定义键。 定义此EntityType的键 - Database First EntityType 'table' has no key defined. Define the key for this EntityType
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM