0

我使用 EF 创建了一个与另一个表没有任何关系的模型。当我在上下文中添加它并为创建数据库运行应用程序时,我在应用程序启动中遇到错误:

在模型生成期间检测到一个或多个验证错误:System.Data.Edm.EdmEntityType: : EntityType 'UrlHelper' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntityType: : EntityType 'RequestContext' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntityType: : EntityType 'HttpContextBase' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntityType: : EntityType 'Exception' 没有定义键。定义此 EntityType 的键。System.Data.Edm.EdmEntityType: : EntityType 'Type' 没有定义键。定义此 EntityType 的键。

我的 global.asax 中的代码如下所示:

    public class MyInitializer
: DropCreateDatabaseIfModelChanges<DBTa>
    {
    }
    protected void Application_Start()
    {
        //for creating and initializing database 
        System.Data.Entity.Database.SetInitializer(new MyInitializer());
        **DBTa db = new DBTa();**//Error shows here
        db.Database.Initialize(true);
        //

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

但是当我从应用程序中删除模型时,我的问题解决了,但我需要这个模型。

请帮助非常感谢

4

1 回答 1

0

您应该在'UrlHelper'模型中设置“key”属性。如果您首先使用 EF 代码 - 在您的模型中,您应该将属性添加[Key]到其属性之一。如果您使用模型优先方法 - 在您的模型中,您应该将其中一个属性标记为主键。如果您使用数据库优先方法 - 您应该在表中创建主键。

于 2013-08-24T12:35:20.493 回答