简体   繁体   中英

Entity Framework System.InvalidOperationException

I use entity framework container within 2 different classes.

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);

    }
}

And I got this exception:

 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`

I don't want to modify my class structure, can I use Entity Framework properly in this case? How?

Thanks for the answers.

You're mixing multiple data contexts: account is attached to databaseContainer ( databaseContainer.Transactions.AddObject(transaction); with account as a nested property from transaction ) but comes from Model1Container container = new Model1Container(); .

You can't use an instance of an entity in multiple contexts at the same time.


Also I see some kind of singleton pattern for a data container, which is generally a bad, bad, bad idea...

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