实体框架6异常:"在创建模型时不能使用上下文"

Ste*_*her 8 .net c# entity-framework-6

当它从旧式的ADO.NET进入实体框架6时,我有点新鲜,所以请耐心等待.

我正在尝试创建一个新的WPF MVVM项目以及一些WinForms,它们将直接使用DBContext而不与EF 6进行数据绑定.

使用Visual Studio 2013实体框架向导,我通过逆向工程创建了代码优先于业务服务器上的当前数据库.然后我将数据模型类与上下文分开

这是DbContext代码:

namespace EFModels
{
    public partial class MyDataContext : DbContext
    {
        public MyDataContext () : base("name=MyDataContext")
        {
        }

        public virtual DbSet<Calibration> Calibrations { get; set; }
        public virtual DbSet<OrderDetail> OrderDetails { get; set; }
        public virtual DbSet<OrderHistory> OrderHistories { get; set; } 
        public virtual DbSet<WorkDetail> WorkDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {   
            modelBuilder.Entity<WorkDetail>()
                    .HasMany(e => e.Calibrations)
                    .WithOptional(e => e.WorkDetail)
                    .HasForeignKey(e => e.WorkID);  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在单独的命名空间中分隔了数据类,例如:

namespace MyDataDomain
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class OrderDetail
    {
        public OrderDetail()
        {
            Calibrations = new HashSet<Calibration>();
            JournalEntryDatas = new HashSet<JournalEntryData>();
            OrderHistories = new HashSet<OrderHistory>();
            WorkDetails = new HashSet<WorkDetail>();
        }

        [Key]
        public long OrderID { get; set; }

        [StringLength(50)]  
        public string PONumber { get; set; }

        [Column(TypeName = "date")]
        public DateTime? Due { get; set; }

        [Column(TypeName = "date")]
        public DateTime? OrderDate { get; set; }

        [Column(TypeName = "date")]
        public DateTime? ShipDate { get; set; }

        [Column(TypeName = "text")]
        public string Comment { get; set; }

        public int? EnterByID { get; set; }

        public virtual ICollection<Calibration> Calibrations { get; set; }

        public virtual ICollection<JournalEntryData> JournalEntryDatas { get; set; }
        public virtual ICollection<OrderHistory> OrderHistories { get; set; }
        public virtual ICollection<WorkDetail> WorkDetails { get; set;      }
    }
}
Run Code Online (Sandbox Code Playgroud)

其余的类都是类似的样式,但是当使用外键约束时,它有类似这样的东西:

public virtual OrderDetail OrderDetail { get; set; }
Run Code Online (Sandbox Code Playgroud)

因为在我们这个小小的世界里,它将围绕着秩序.

和app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0,         Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
  <connectionStrings>
    <add name="MyDataContext" connectionString="data source=BIZSERVER\SQL2008R2DB;initial catalog=Company;persist security info=True;user id=DaBossMan;password=none_of_your_damn_business;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>
Run Code Online (Sandbox Code Playgroud)

所以当我这样做时:

var context = New MyDataContext();
var list1 = context.JournalEntryDatas.ToList();
var list2 = context.OrderHistories.ToList();
Run Code Online (Sandbox Code Playgroud)

抛出异常:

在创建模型时不能使用上下文.如果在OnModelCreating方法内部使用上下文,或者同时由多个线程访问相同的上下文实例,则可能抛出此异常 .请注意,DbContext 不保证实例成员和相关类是线程安全的.

我在这里试图弄清楚我能做些什么,我一直在读,也许正在做一个任务工厂可以提供帮助,那么我怎么能用它来从每个表中获取数据以便我可以填充这些列表?

或者我可以使用另一种替代方案或解决方法吗?

编辑:这是所要求的完整堆栈跟踪(由Andez提供):

System.InvalidOperationException was caught
HResult=-2146233079
Message=The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
Source=EntityFramework
StackTrace:
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at MichellControls.JournalDataListView.JournalDataListView_Load(Object sender, EventArgs e) in c:\Users\Steve\Projects\Visual Studio 2013\Projects\MyControls\WinFormControls\JournalDataListView.cs:line 35
InnerException: 
    // (there wasn't any InnerException)
Run Code Online (Sandbox Code Playgroud)

Iva*_*eng 0

我认为这可能与 onModelCreating 中的外键创建有关。根据文档, onMethodCreating 方法“在派生上下文的模型已初始化时但在模型被锁定并用于初始化上下文之前调用”。

此外,如果您使用 EF6,您可以在域对象中声明关系,而不必自己手动声明关系。我发现您已经在模型中声明了关系;删除它,看看会发生什么。有关配置一对多关系的更多信息,请参阅此处。