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 を適切に使用できますか? どのように?
答えてくれてありがとう。