我有一个奇怪的情况,我无法弄清楚原因。在启动 Windows Phone 应用程序时,我有以下代码,其中我只是创建了一些Group
s,然后创建了一些Account
s,其中先前创建Group
的 s 是与 s 相关的对象Account
。
public async static void SampleData()
{
var grp1 = await Manager.GroupManager.SaveGroupAsync(new Group { GroupName = "Wells Fargo" });
var grp2 = await Manager.GroupManager.SaveGroupAsync(new Group { GroupName = "Chase" });
var grp3 = await Manager.GroupManager.SaveGroupAsync(new Group { GroupName = "Citi" });
var acct1 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Spending", AccountType = (int)Enums.AccountType.Banking, Group = grp1 }); //0
var acct2 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Credit Card", AccountType = (int)Enums.AccountType.Credit, Group = grp1 }); //1
var acct3 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Mortgage", AccountType = (int)Enums.AccountType.Banking, Group = grp2 }); //2
var acct4 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Savings", AccountType = (int)Enums.AccountType.Banking, Group = grp2 }); //3
var acct5 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Spending", AccountType = (int)Enums.AccountType.Banking, Group = grp2 }); //4
var acct6 = await Manager.AccountManager.SaveAccountAsync(new Account { AccountName = "Credit Card", AccountType = (int)Enums.AccountType.Credit, Group = grp3 }); //5
}
相应的方法:
经理人
public async static Task<Group> SaveGroupAsync(Group group)
{
await DataAccess.GroupData.SaveGroupAsync(group);
return group;
}
public async static Task<Account> SaveAccountAsync(Account account)
{
await DataAccess.AccountData.SaveAccountAsync(account);
return account;
}
数据访问
public async static Task<Group> SaveGroupAsync(Group group)
{
using (var dc = new CbmDataContext(DBHelper.LocalConnectionString))
{
if (group.GroupId == 0)
dc.Groups.InsertOnSubmit(group);
dc.SubmitChanges();
return group;
}
}
public async static Task<Account> SaveAccountAsync(Account account)
{
using (var dc = new CbmDataContext(DBHelper.LocalConnectionString))
{
if (account.AccountId == 0)
dc.Accounts.InsertOnSubmit(account);
dc.SubmitChanges();
return account;
}
}
当组的前 3 个保存运行时,我看到插入了 3 个新对象。这是我的预期行为。当代码为第一个新帐户运行时,我看到了新帐户,但我还在组表中看到了引用帐户的新条目。我最终得到了 2 个同名的组。
我做错了什么导致这种情况发生?我可以看到 Account.GroupId 设置为下面设置的组的 ID。