0

我有,errr 有一个操作数据库信息的工作 wpf 应用程序(使用实体框架,首先使用数据库)。

数据的结构是 4 个财务信息表(全部 1:1 映射到 5 个主表),主表中有几个带有外键引用的查找表。

我在 SqlServer 中添加了一个表(另一个 1:1 映射到主表),然后运行“从数据库更新模型...”向导将新表添加到模型中。.edmx 文件中的一切看起来都很好,包括“0..1”关系链接。

但是,当我尝试保存时,我收到“违反唯一约束”错误。

我的创建代码:

private void AddNewStatementsQuery(LGFinanceEntities lGFinanceEntities)
{
  StatementsMain newStatement = StatementsMain.CreateStatementsMain(9999, this.LocalGovt.StakeholderID, 161, this.Year.FinancialYearID);
  StatementsIncome newInc = StatementsIncome.CreateStatementsIncome(newStatement.StatementsMainID);
  StatementsNote newNote = StatementsNote.CreateStatementsNote(newStatement.StatementsMainID);
  StatementsRSSFinPos newRSSFinPos = StatementsRSSFinPos.CreateStatementsRSSFinPos(newStatement.StatementsMainID);
  StatementsSurplusDeficit newSurplusDeficit = StatementsSurplusDeficit.CreateStatementsSurplusDeficit(newStatement.StatementsMainID);
  lGFinanceEntities.StatementsMains.Context.AddObject("StatementsMains", newStatement);
  lGFinanceEntities.StatementsMains.Context.AddObject("StatementsIncomes", newInc);
  lGFinanceEntities.StatementsMains.Context.AddObject("StatementsNotes", newNote);
  lGFinanceEntities.StatementsMains.Context.AddObject("StatementsRSSFinPos", newRSSFinPos);
  lGFinanceEntities.StatementsMains.Context.AddObject("StatementsSurplusDeficit", newSurplusDeficit);
  if (lGFinanceEntities.SaveChanges() != 1)  // this is causing the exception
  {
    MessageBox.Show("Error. New Statements not created", "Database Error");
  }
}

在添加新表之前,上面的代码是有效的。唯一的变化是添加了以下行:

StatementsSurplusDeficit newSurplusDeficit = 
    StatementsSurplusDeficit.CreateStatementsSurplusDeficit(newStatement.StatementsMainID);
...
lGFinanceEntities.StatementsMains.Context.AddObject("StatementsSurplusDeficit", 
    newSurplusDeficit);

有趣的,某处正在创建一条记录,因为当我检查 SqlServer 时,我确实有 5 个表的新记录。同样有趣的是,每次我尝试某些东西并运行该方法时,主键都会增加 2。看起来同一条记录被添加了两次,但我不知道如何添加。

编辑: 根据评论建议,我更改了“AddNewStatementsQuery”,如下所示:

lGFinanceEntities.StatementsMains.Context.AddObject("StatementsMains", newStatement);

改为:

lGFinanceEntities.StatementsMains.AddObject(newStatement);

然后到:

lGFinanceEntities.AddObject("StatementsMains", newStatement);

这并没有解决密钥冲突错误。

我如何找出数据被保存两次的位置/方式(即,除了lGFinanceEntities.SaveChanges()在 if 语句中)?

4

1 回答 1

0

唔。查看您的代码,我可以看到它被简化为:

// Create the new objects    
var statement = new StatementsMain()
{
    this.LocalGovt.StakeholderID, 161, this.Year.FinancialYearID
};

var income = new StatementsIncome()
{
    StatementsMain = statement
};

var note = new StatementsNote()
{
    StatementsMain = statement
};

var rss = new StatementsRSSFinPos()
{
    StatementsMain = statement
};

var surplus = new StatementsSurplusDeficit()
{
    StatementsMain = statement
};


// Add the objects into the context 
lGFinancialEntities.AddObject(statement);
lGFinancialEntities.AddObject(income);
lGFinancialEntities.AddObject(note);
lGFinancialEntities.AddObject(rss);
lGFinancialEntities.AddObject(surplus);

// Persist the objects to the data storage
lGFinancialEntities.SaveChanges();

或者,甚至更好:

// Create the main object
var statement = new StatementsMain()
{
    this.LocalGovt.StakeholderID, 161, this.Year.FinancialYearID
};

// Add the objects into the context
lGFinancialEntities.AddObject(statement);
lGFinancialEntities.AddObject(new StatementsIncome() { StatementsMain = statement });
lGFinancialEntities.AddObject(new StatementsNote() { StatementsMain = statement });
lGFinancialEntities.AddObject(new StatementsRSSFinPos() { StatementsMain = statement });
lGFinancialEntities.AddObject(new StatementsSurplusDeficit() { StatementsMain = statement });

// Persist the objects to the data storage
lGFinancialEntities.SaveChanges();

但是,这告诉我有很多关于您的数据模式的内容在这里并不明显。例如,对象中的值161引用了StatementsMain什么?

仅供参考,将主对象分配给它是外键的对象让 EF 完成将新 ID 分配给其他对象的工作,因为它们被持久化。

于 2013-01-22T22:55:56.870 回答