我通过创建连接并将其传递给DB上下文以常规方式使用Effort:
public class InMemoryContextInitializer
{
public void BeforeAllTests()
{
Effort.Provider.EffortProviderConfiguration.RegisterProvider();
}
public DbConnection BeforeEachTest()
{
return Effort.DbConnectionFactory.CreateTransient();
}
public void AfterEachTest(DbConnection prevUsedConnection)
{
prevUsedConnection.Dispose();
}
}
等等:
private static InMemoryContextInitializer _testHarness;
private DbConnection _conn;
private MyDbContext _myContext;
[ClassInitialize]
public static void BeforeAllTests(TestContext testCtx)
{
_testHarness = new InMemoryContextInitializer();
_testHarness.BeforeAllTests(); // registers provider
}
[TestInitialize]
public void BeforeTest()
{
_conn = _testHarness.BeforeEachTest();
_myContext = new MyDbContext(_conn);
}
[TestCleanup]
public void AfterTest()
{
_testHarness.AfterEachTest(_conn);
}
我的所有努力测试都通过了,除了抛出这个测试的那个:
System.Data.DataException: An exception occurred while initializing the database. See the InnerException for details. --->
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. --->
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. --->
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->
NMemory.Exceptions.NMemoryException: Error code: GenericError --->
System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source, Func`2 predicate)
at NMemory.Tables.TableCollection.FindTable(Type entityType)
at NMemory.Utilities.TableCollectionExtensions.FindTable(TableCollection tableCollection)
at NMemory.Execution.CommandExecutor.ExecuteInsert(T entity, IExecutionContext context)
at NMemory.Tables.DefaultTable`2.InsertCore(TEntity entity, Transaction transaction)
at Effort.Internal.DbManagement.Engine.ExtendedTable`2.InsertCore(TEntity entity, Transaction transaction)
at NMemory.Tables.Table`2.Insert(TEntity entity, Transaction transaction)
--- End of inner exception stack trace ---
at NMemory.Tables.Table`2.Insert(TEntity entity, Transaction transaction)
at Effort.Internal.Common.DatabaseReflectionHelper.WrapperMethods.InsertEntity(ITable`1 table, TEntity entity, Transaction transaction)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Effort.Internal.Common.DatabaseReflectionHelper.InsertEntity(ITable table, Object entity, Transaction transaction)
at Effort.Internal.CommandActions.InsertCommandAction.CreateAndInsertEntity(ITable table, IList`1 memberBindings, Transaction transaction)
at Effort.Internal.CommandActions.InsertCommandAction.ExecuteNonQuery(ActionContext context)
at Effort.Provider.EffortEntityCommand.ExecuteNonQuery()
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<NonQuery>b__0(DbCommand t, DbCommandInterceptionContext`1 c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext)
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery()
at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
--- End of inner exception stack trace ---
at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(T noChangesResult, Func`2 updateFunction)
at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute(Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()
at System.Data.Entity.Migrations.History.HistoryRepository.BootstrapUsingEFProviderDdl(VersionedModel versionedModel)
at System.Data.Entity.Internal.InternalContext.<SaveMetadataToDatabase>b__7()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.SaveMetadataToDatabase()
at System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
at System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState)
at System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
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.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Count(IQueryable`1 source)
at Model.MyProj... my test method call stack here
我在这些测试中使用了多个数据库上下文,除了这种情况外它都可以工作:
var query = db.MyEntities.Count(); // will throw the exception above ONLY FOR SOME DB CONTEXTS
事实上,像Add
这样的上下文的任何操作都会抛出。
我在Effort网站上或通过Google搜索找不到任何人遇到同样的问题。也许这是一个错误?我使用的是最新版本的Effort - 1.1.4和Entity Framework - 6.1.3。
我检查了有关DbContext或实体类的任何特殊内容,但没有找到任何内容。
答案 0 :(得分:1)
我遇到了这个问题,发现每个上下文使用单独的连接(<form method="post" id="onlineorder" action="sendform.php">
<fieldset id="listproducts">
<legend>PRODUCTS</legend>
<div id='forclone'>
<select name="product" id="product">
<optgroup label="PRODUCT 1">
<option>PRODUCT 1 - BLUE</option>
<option>PRODUCT 1 - RED</option>
<option>PRODUCT 1 - YELLOW</option>
</optgroup>
<optgroup label="PRODUCT 2">
<option>PRODUCT 2 - WHITE</option>
<option>PRODUCT 2 - BLACK</option>
<option>PRODUCT 2 - GRAY</option>
</optgroup>
</select>
<label for="quantity">Quantity:<span style="color:red">*</span>
</label>
<input type="number" name="quantity" id="quantity" placeholder="Qnt" min="0" max="9999" required/>
</div>
<div id="dynamicinput"></div>
<input type="button" value="New Product" onclick="addInput('dynamicinput');" />
</fieldset>
<p></p>
<input type="submit" value="SUBMIT">
</form>
)解决了这个问题。
在我的情况下,每个上下文都在完全不同的表上运行,所以在Effort / NMemory中不应该发生冲突 - 所以我认为错误报告是合理的 - 但是这个信息可能会在此期间解锁人。
权衡的是,这可能会警告你,你确实有两个在同一张桌子上运行的情境,这很糟糕 - 所以你也要注意这一点。