我正在尝试在空闲时间为一个简单的博客网站创建数据结构,但我似乎无法弄清楚为什么会出现此错误。我正在使用 EF5 和 MVC4。数据库上下文:
public class KWBlogContext : DbContext
{
static KWBlogContext()
{
Database.SetInitializer<KWBlogContext>(null);
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<BlogType> BlogTypes { get; set; }
etc...
我对 OnModelCreating 进行了一些实验,但无济于事。
DbContext 正在由 BaseController 初始化:
public class BaseController : Controller
{
public KWBlogContext db;
public BaseController()
{
Database.SetInitializer<KWBlogContext>(null);
db = new KWBlogContext();
}
}
我在 HomeController(implements BaseController) 的这一行上收到“序列包含多个元素”错误:
vm.Blogs = db.Blogs.Where(x => x.CreatedDate >= subtractDate).OrderBy(x => x.BlogId).ToList();
vm.BlogTypes = db.BlogTypes.ToList();
这是 StackTrace:
[InvalidOperationException: Sequence contains more than one element]
System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4078798
System.Data.Entity.ModelConfiguration.Conventions.<>c__DisplayClass3.<System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Entity.Edm.EdmEntityType>.Apply>b__0(PropertyInfo p) +187
System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each(IEnumerable`1 ts, Action`1 action) +194
System.Data.Entity.ModelConfiguration.Conventions.DeclaredPropertyOrderingConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Entity.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model) +185
System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.Dispatch(TEdmDataModelItem item) +181
System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item) +59
System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection(IEnumerable`1 collection, Action`1 visitMethod) +204
System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes) +89
System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item) +193
System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item) +53
System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection(IEnumerable`1 collection, Action`1 visitMethod) +204
System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces) +89
System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item) +130
System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.VisitEdmModel(EdmModel item) +79
System.Data.Entity.ModelConfiguration.Configuration.EdmConventionDispatcher.Dispatch() +36
System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model) +136
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +209
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +106
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +143
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +171
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +499
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +137
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +84
任何输入将不胜感激。一两天来,我一直在为此苦苦挣扎。提前致谢!
编辑:
我开始意识到这也可能是 SQL Server/config 问题。我正在查看我的 Management Studio 日志并遇到:
The SQL Server Network Interface library could not register the SPN for the SQL Server service. [ MSSQLSvc/MachineName:SQLEXPRESS] Windows return code: 0xffffffff, state 63
Dedicated administrator connection support was not started because it has been disabled on this edition of SQL Server. If you want to use dedicated administrator connection, restart SQL server using the trace flag 7806.
这是我的连接字符串:
<add name="KWBlogContext" connectionString="Data Source=MACHINENAME\SQLEXPRESS;Initial Catalog=KWBlog;Integrated Security=True" providerName="System.Data.SqlClient" />