实体框架System.InvalidOperationException

时间:2013-01-11 17:10:48

标签: c# entity-framework

我在2个不同的类中使用实体框架容器。

private static DataManager instance;
    private Model1Container databaseContainer;
    private DataManager()
    {
        databaseContainer = new Model1Container();
    }

    public static IDataManager Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new DataManager();
            }
            return instance;
        }
    }

    public void OpenNewAccount(int amount)
    {

        Account account = new Account();

        Transaction transaction = new Transaction();
        transaction.Account = account;
        transaction.Amount = amount;
        transaction.Date = DateTime.Now;

        if (TransactionExecutor.Instance.ExecuteTransaction(transaction, account))
        {
            //EXCEPTION An unhandled exception of type                   'System.InvalidOperationException' occurred in System.Data.Entity.dll

            //Additional information: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
            databaseContainer.Transactions.AddObject(transaction);
            databaseContainer.Accounts.AddObject(account);
            databaseContainer.SaveChanges();
        }
    }

class AccountStateTransactionValidator : AbstractTransactionValidator
{
    override
    public bool HandleTransaction(Transaction transaction, Account account)
    {
        if (account.AccountState == null || (account.Amount < account.AccountState.LeftBound || account.Amount >= account.AccountState.RightBound))
        {
            Model1Container container = new Model1Container();
            AccountState properState = (from st in container.AccountStates
                              where st.LeftBound <= account.Amount &&
                              st.RightBound >= account.Amount
                              select st).FirstOrDefault();
            if (properState != null)
            {
               account.AccountState = properState;
            }
            else
            {
                return false;
            }
        }

        return base.nextTransaction(transaction, account);

    }
}

我得到了这个例外:

 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll`

我不想修改我的类结构,在这种情况下我可以正确使用Entity Framework吗?怎么样?

感谢您的回答。

1 个答案:

答案 0 :(得分:2)

您正在混合多个数据上下文:account附加到databaseContainerdatabaseContainer.Transactions.AddObject(transaction); account作为来自transaction的嵌套属性,但来自Model1Container container = new Model1Container();

您不能同时在多个上下文中使用实体实例。


我也看到了数据容器的某种单例模式,这通常是一个坏的,坏的,坏主意......