0
我想在我的空閒時間創建一個簡單的博客網站的數據結構,我似乎無法弄清楚爲什麼我得到這個錯誤。我正在使用EF5和MVC4。該的DbContext:創建模型時無法使用數據上下文。實體框架5
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錯誤(實現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 /配置問題。我一直在尋找在我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.
這裏是我的connectionString:
<add name="KWBlogContext" connectionString="Data Source=MACHINENAME\SQLEXPRESS;Initial Catalog=KWBlog;Integrated Security=True" providerName="System.Data.SqlClient" />
我發佈的HomeController代碼是調用DbContext的唯一代碼,它使用ToList()方法。它在視圖模型中定義爲public IEnumerable Blogs {get;組; } –
kwinsor5
你可以請調試,看看當你點擊這條線'db.Blogs.Where(x => x.CreatedDate> = subtractDate).OrderBy(x => x.BlogId)時正在生成的EF查詢是什麼。 ToList();' – Saravanan