EntityFrameworkを使用してデータベースにレコードを追加する必要があります。私はこの構文を使用するのは初めてなので、コードを正しく記述する方法がわかりません(以下が私の最善の推測です)。
まず、エージェントは自分の情報をテーブルに挿入する必要がありAgent
ます。このテーブルは、として知られる自己インクリメント主キーを生成しますSymNumber
。SymNumber
次に、それを取得して、テーブルに挿入するための主キーとして使用する必要がありAgentIdentification
ます。
このコードを数回実行しましたが、エラーは発生しませんが、単体テストを使用してコードをテストしているため、エージェントが適切に追加されているかどうかを確認できません。第二に、私は最初の挿入後にテーブルSymNumber
によって生成されたものを正しく取得していないという事実を知っています。Agent
これは、0に設定されたLinqコードのint値であり、挿入 SymNumber
中に変更されません。AgentIdentification
どんな助けでも大歓迎です!
AgentResourcesEntities _db = new AgentResourcesEntities();
try
{
Agent agent = new Agent();
agent.EntityType = "P";
agent.FirstName = agentNewTraining.FirstName;
agent.LastName = agentNewTraining.LastName;
agent.LastChangeOperator = agentNewTraining.Requestor;
agent.LastChangeDate = DateTime.Now;
if (!String.IsNullOrEmpty(agentNewTraining.NameSuffix)) agent.NameSuffix = agentNewTraining.NameSuffix;
_db.Agent.AddObject(agent);
AgentIdentification agentIdentification = new AgentIdentification();
agentIdentification.SymNumber = agent.SymNumber;
agentIdentification.ReferenceType = "S";
agentIdentification.DummyReference = 0;
agentIdentification.LastChangeOperator = agentNewTraining.Requestor;
agentIdentification.LastChangeDate = DateTime.Now;
_db.AgentIdentification.AddObject(agentIdentification);
return true;
}
catch (Exception)
{
return false;
}