要在 linqToSql C# 中分配 FK 字段和条目,异常:"An attempt has been made to Attach or Add an entity that is not new,...."
To assign FK field of and entry in linqToSql C#, Exception: "An attempt has been made to Attach or Add an entity that is not new,...."
我正在将一个对象分配给来自 dbContext 实例的外键字段的引用,然后我提交更改并收到此异常
"An attempt has been made to Attach or Add an entity that is not new,
perhaps having been loaded from another DataContext. This is not
supported."
Table schema is like the following,
__________________________
|tbOrder | tbPharma |
|------------ |----------|
|Id | Id |
|.. | ... |
|.. | ... |
|PharmaId(FK) | ... |
|.. | |
|.. | |
public FormOrder()
{
InitializeComponent();
currOrder= new TBL_DEMO();
dbContext = DBContext.db; // dbContex is received from a static class
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = new VerticalProgressBar.MyDBDataContext().tbOrders;
}
...
currOrder = gridViewOrderPreview.GetFocusedRow() as tbOrder;
...
...
Pharma pharma = db.tbPharmas.Where(i => i.ID == pharmaId).FirstOrDefault();
tbOrder currOrder = new tbOrder();
currOrder.tbPharma = pharma;
...
...
if (currOrder.Id == null || currOrder.Id == 0)
db.tbOrder.InsertOnSubmit(currOrder);
else
{
targetOrder = db.tbOrders.SingleOrDefault(x => x.Id == currOrder.Id) as tbOrder;
// targetOrder's fields are syncronized with currOrder's fields.
applyChanges(currOrder, targetOrder);
}
db.SubmitChanges();
...
我已经探索了这个问题。
它源于使用不同的DBContext。
我意识到我从另一个 DBContext 提供了与我的 currOrder 相关的 gridControl,并且我用另一个 dbContext 提交了 Changes()。
dbContext = DBContext.db; // dbContex is received from a static class
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = new VerticalProgressBar.MyDBDataContext().tbOrders;
我像下面这样更改了自动生成的代码,问题消失了。
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = dbContext.tbOrders;
我正在将一个对象分配给来自 dbContext 实例的外键字段的引用,然后我提交更改并收到此异常
"An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported."
Table schema is like the following,
__________________________
|tbOrder | tbPharma |
|------------ |----------|
|Id | Id |
|.. | ... |
|.. | ... |
|PharmaId(FK) | ... |
|.. | |
|.. | |
public FormOrder()
{
InitializeComponent();
currOrder= new TBL_DEMO();
dbContext = DBContext.db; // dbContex is received from a static class
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = new VerticalProgressBar.MyDBDataContext().tbOrders;
}
...
currOrder = gridViewOrderPreview.GetFocusedRow() as tbOrder;
...
...
Pharma pharma = db.tbPharmas.Where(i => i.ID == pharmaId).FirstOrDefault();
tbOrder currOrder = new tbOrder();
currOrder.tbPharma = pharma;
...
...
if (currOrder.Id == null || currOrder.Id == 0)
db.tbOrder.InsertOnSubmit(currOrder);
else
{
targetOrder = db.tbOrders.SingleOrDefault(x => x.Id == currOrder.Id) as tbOrder;
// targetOrder's fields are syncronized with currOrder's fields.
applyChanges(currOrder, targetOrder);
}
db.SubmitChanges();
...
我已经探索了这个问题。 它源于使用不同的DBContext。
我意识到我从另一个 DBContext 提供了与我的 currOrder 相关的 gridControl,并且我用另一个 dbContext 提交了 Changes()。
dbContext = DBContext.db; // dbContex is received from a static class
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = new VerticalProgressBar.MyDBDataContext().tbOrders;
我像下面这样更改了自动生成的代码,问题消失了。
// This line of code is generated by Data Source Configuration Wizard
gridControlOrderPreview.DataSource = dbContext.tbOrders;