简体   繁体   中英

The data context cannot be used while the model is being created. Entity Framework 5

I am trying to create the data structure for a simple blog website in my free time, and I cannot seem to figure out why I'm getting this error. I am using EF5 and MVC4. The DbContext:

public class KWBlogContext : DbContext
{
    static KWBlogContext()
    {
        Database.SetInitializer<KWBlogContext>(null);
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<BlogType> BlogTypes { get; set; }

etc...

I experimented a little with OnModelCreating, but to no avail.

The DbContext is being initialized by BaseController:

public class BaseController : Controller
{

    public KWBlogContext db;
    public BaseController()
    {
        Database.SetInitializer<KWBlogContext>(null);
        db = new KWBlogContext();
    }

}

I get the "Sequence contains more than one element" error on this line in HomeController(implements BaseController):

vm.Blogs = db.Blogs.Where(x => x.CreatedDate >= subtractDate).OrderBy(x => x.BlogId).ToList();
vm.BlogTypes = db.BlogTypes.ToList();

And here is the 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

Any input would be appreciated. I have been beating my head against this for a day or two. Thanks in advance!

EDIT:

I have come to realize that it also may be a SQL Server/config issue. I was looking in my Management Studio logs and came across:

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.

Here is my connectionString:

<add name="KWBlogContext" connectionString="Data Source=MACHINENAME\SQLEXPRESS;Initial Catalog=KWBlog;Integrated Security=True" providerName="System.Data.SqlClient" />

您在代码中的某处使用了SingleOrDefault ,条件或表达式返回了多个元素。请发布您的控制器代码以进行验证。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM