Asp.net mvc 5 实体类型';AstNode';没有定义键
我首先将数据模型从EF4移植到EF6代码。当尝试创建数据库时,我收到以下消息。我不知道这是什么原因造成的。我没有任何上下文、AstNode或JSParser实体。它也不在模型名称空间中查找:Asp.net mvc 5 实体类型';AstNode';没有定义键,asp.net-mvc-5,ef-code-first,entity-framework-6,Asp.net Mvc 5,Ef Code First,Entity Framework 6,我首先将数据模型从EF4移植到EF6代码。当尝试创建数据库时,我收到以下消息。我不知道这是什么原因造成的。我没有任何上下文、AstNode或JSParser实体。它也不在模型名称空间中查找: var context = QPDataContext.Create(); var session = context.DataSessions.FirstOrDefault(ds => ds.DataSessionId = sessionId); 引发此异常: {"One or more val
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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Data.Entity.Core;
使用System.Data.Entity.Validation;
使用系统诊断;
使用System.Linq;
使用System.Text.RegularExpressions;
使用System.Web;
使用Microsoft.Ajax.Utilities;
使用QPWebRater.Models;
使用QPWebRater.Utilities;
命名空间QPWebRater.DAL
{
公共类QPDataContext:DbContext
{
公共QPDataContext()
:base(“默认连接”)
{
SetInitializer(新的CreateDatabaseIfNotExists());
}
公共静态QPDataContext创建()
{
返回新的QPDataContext();
}
公共数据库集数据会话{get;set;}
公共数据库集文档{get;set;}
公共数据库集驱动程序{get;set;}
公共数据库集位置{get;set;}
公共数据库集查找{get;set;}
公共数据库集引号{get;set;}
公共数据库集车辆{get;set;}
公共数据库集冲突{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; }
...
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间QPWebRater.Models
{
公共部分类数据会话
{
公共数据会话()
{
this.Vehicles=new HashSet();
this.Drivers=newhashset();
...
}
公共字符串DataSessionId{get;set;}
public System.DateTime时间戳{get;set;}
...
}
公共部分类文档
{
public int DocumentId{get;set;}
公共int QuoteId{get;set;}
公共字符串DocumentType{get;set;}
公共字符串Url{get;set;}
公共字符串说明{get;set;}
公共虚拟报价{get;set;}
}
公共部分类驱动程序
{
公共司机()
{
this.involutions=new HashSet();
}
public int DriverId{get;set;}
公共字符串DataSessionId{get;set;}
...
}
}
我通过检查所有DbSet定义解决了这个问题。我削减了数据模型,同时也对其进行了升级。我删除了Lookup
模型类,但忽略了删除DbSet Lookups{get;set;}
属性
这导致类被解析为Microsoft.Ajax.Utilities.Lookup
。在运行时,EntityFramework尝试添加相应的数据库表,但失败惨重。如果遇到类似的问题,请仔细检查DbSet属性中的泛型类型