【问题标题】:Entity Type 'AstNode' has no key defined实体类型“AstNode”没有定义键
【发布时间】:2015-08-29 00:08:13
【问题描述】:

我正在将数据模型从 EF4 移植到 EF6 Code First。尝试创建数据库时收到以下消息。我不知道是什么原因造成的。我没有任何 Context、AstNode 或 JSParser 实体。它也没有在 Models 命名空间中查找:

var context = QPDataContext.Create();
var session = context.DataSessions.FirstOrDefault(ds => ds.DataSessionId = sessionId); 

抛出此异常:

{"One or more validation errors were detected during model generation:

   QPWebRater.DAL.Context: : EntityType 'Context' has no key defined. Define the key for this EntityType. 
   QPWebRater.DAL.AstNode: : EntityType 'AstNode' has no key defined. Define the key for this EntityType.
   QPWebRater.DAL.JSParser: : EntityType 'JSParser' has no key defined. Define the key for this EntityType.
   (many more similar errors snipped). 
"}

这是我的数据库上下文(我已经简化了一点):

QPWebRater.DAL.QPDataContext.cs:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Ajax.Utilities;
using QPWebRater.Models;
using QPWebRater.Utilities;

namespace QPWebRater.DAL
{
    public class QPDataContext : DbContext
    {
        public QPDataContext()
            : base("DefaultConnection")
        {
            Database.SetInitializer<QPDataContext>(new CreateDatabaseIfNotExists<QPDataContext>());
        }

        public static QPDataContext Create()
        {
            return new QPDataContext();
        }

        public DbSet<DataSession> DataSession { get; set; }
        public DbSet<Document> Documents { get; set; }
        public DbSet<Driver> Drivers { get; set; }
        public DbSet<Location> Locations { get; set; }
        public DbSet<Lookup> Lookups { get; set; }
        public DbSet<Quote> Quotes { get; set; }
        public DbSet<Vehicle> Vehicles { get; set; }
        public DbSet<Violation> Violations { get; set; }
     }
}

QPWebRater.Models.DatabaseModels.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QPWebRater.Models
{

    public partial class DataSession
    {
        public DataSession()
        {
            this.Vehicles = new HashSet<Vehicle>();
            this.Drivers = new HashSet<Driver>();
            ...
        }

        public string DataSessionId { get; set; }
        public System.DateTime Timestamp { get; set; }

        ...
    }

    public partial class Document
    {
        public int DocumentId { get; set; }
        public int QuoteId { get; set; }
        public string DocumentType { get; set; }
        public string Url { get; set; }
        public string Description { get; set; }

        public virtual Quote Quote { get; set; }
    }

    public partial class Driver
    {
        public Driver()
        {
            this.Violations = new HashSet<Violation>();
        }

        public int DriverId { get; set; }
        public string DataSessionId { get; set; }
        ...
    }
}

【问题讨论】:

    标签: asp.net-mvc-5 ef-code-first entity-framework-6


    【解决方案1】:

    我通过检查所有 DbSet 定义解决了这个问题。我在升级数据模型的同时缩减了数据模型。我已经删除了 Lookup 模型类,但忽略了也删除了 DbSet&lt;Lookup&gt; Lookups { get; set; } 属性。

    这导致类被解析为Microsoft.Ajax.Utilities.Lookup。在运行时,EntityFramework 尝试添加相应的数据库表,但失败得很惨。如果您遇到类似问题,请仔细检查 DbSet 属性中的泛型类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多