score:2
You could do something to the effect of
db.MatchUpdateQueues.DeleteOnSubmit(db.MatchUpdateQueues.Single(theItem => theItem == item));
Just a note as other answers hinted towards Attach.. you will not be able to use attach on a context other then the original context the item was received on unless the entity has been serialized.
score:0
Use:
aDataContext db = new aDataContext();
item = new MatchUpdateQueue { id=item.id }; // <- updated
db.MatchUpdateQueues.Attach(item);
db.MatchUpdateQueues.DeleteOnSubmit(item);
db.SubmitChanges();
Since you are using a new datacontext it doesn't know that the object is already in the db.
score:0
Try wrapping the entire inside of the while loop in a using statement for a single data context:
Queue<MatchUpdateQueue> waiting = new Queue<MatchUpdateQueue>();
events.WriteEntry("matchqueue worker thread started");
while (!stop)
{
using (var db = new aDataContext())
{
if (waiting.Count == 0)
{
/* grab any new items available */
List<MatchUpdateQueue> freshitems = db.MatchUpdateQueues
.OrderBy(item => item.id)
.ToList();
foreach (MatchUpdateQueue item in freshitems)
waiting.Enqueue(item);
}
...
}
}
score:0
Remove the first db.Dispose() dispose. It can be the problem code because the entities keep a reference to their data context, so you don't want to dispose it while you are still be working with the instances. This won't affect connections, as they are open/closed only when doing operations that need them.
Also don't dispose the second data context like that, since an exception won't call that dispose code anyway. Use the using keyword, which will make sure to call dispose whether or not an exception occurs. Also grab the item from the db to delete it (to avoid having to serialize/deserialize/attach).
using (aDataContext db = new aDataContext())
{
var dbItem = db.MatchUpdateQueues.Single(i => i.Id == item.Id);
db.MatchUpdateQueues.DeleteOnSubmit(dbItem);
db.SubmitChanges();
}
score:0
try this if your TEntity's (here Area) Primary Key is of type Identity column; Just it, without any change in your SP or Model:
public void InitForm()
{
'bnsEntity is a BindingSource and cachedAreas is a List<Area> created from dataContext.Areas.ToList()
bnsEntity.DataSource = cachedAreas;
'A nominal ID
newID = cachedAreas.LastOrDefault().areaID + 1;
'grdEntity is a GridView
grdEntity.DataSource = bnsEntity;
}
private void tsbNew_Click(object sender, EventArgs e)
{
var newArea = new Area();
newArea.areaID = newID++;
dataContext.GetTable<Area>().InsertOnSubmit(newArea);
bnsEntity.Add(newArea);
grdEntity.MoveToNewRecord();
}
Source: stackoverflow.com
Related Articles
- DeleteOnSubmit LINQ exception "Cannot add an entity with a key that is already in use"
- "Cannot add an entity that already exists" while deleting and recreating record with LINQ
- Cannot add an entity with a key that is already in use (LINQ)
- Exception raised when using a Linq query with Entity Framework
- Using LINQ with stored procedure that returns multiple instances of the same entity per row
- C# linq to entity An item with the same key has already been added
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- C# Linq to SQL:cannot add an entity that already exists
- Problem with a Linq to Entity query that contains a passed list of entities
- LINQ NotSupportedOperation exception with Code First
- Converting a Linq expression tree that relies on SqlMethods.Like() for use with the Entity Framework
- How to select elements from a table that are present in a junction table with LINQ - Entity Framework
- Exception: Cannot add an entity that already exists using linqtosql
- Proper Linq Query for objects with many to many relation ship generated with code first entity framework
- Entity Framework dynamic linq where from generic source with dynamic where clause
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query
- NullReference exception when using LINQ Contains with Entity Framework
- Cannot add an entity that already exists in foreach
- Entity Framework: There is already an open DataReader associated with this Command
- Error: "The specified LINQ expression contains references to queries that are associated with different contexts"
- How are people unit testing code that uses Linq to SQL
- How to count the number of elements that match a condition with LINQ
- Cleanest Way To Map Entity To DTO With Linq Select?
- Best practices for dealing with LINQ statements that result in empty sequences and the like?
- Entity Framework with LINQ aggregate to concatenate string?
- Create Dictionary with LINQ and avoid "item with the same key has already been added" error
- How to insert a record with LINQ and C# and return the Primary Key of that record
- LINQ To SQL: Delete entity (by ID) with one query
- Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name
- how to get this data via linq
- Serialization of EntityFramework LINQ Objects
- How to write a join var datacontext?
- LINQ LEFT JOIN on Nullable<int>
- Multiple where conditions in EF using Lambda expressions
- LambdaExpression to Expression via Extensions Method
- Linq Compiled Queries and int[] as parameter
- Linq query sum up the amount column
- LINQ GroupBy into a new object
- Does Linq's IEnumerable.Select return a reference to the original IEnumerable?
- Extract Items from IEnumerable<ObservableCollection<T>> C#
- Remove Object From Hierarchical Collection
- Retrieve index in LinQ Lambda Join
- How to set order by descending in Linq query based on aggregated column?
- Duplicate record cause no record insert
- Cast IEnumerable<PageData> to Episerver PageDataCollection
- Selecting column from LINQ query where cells equals value from a list
- Searching a DataGridView for a match or partial match
- Linq "join" with a IList<T> getting "Error Unable to create a constant value.."
- Tuples vs. Anonymous Types vs. Expando object. (in regards to LINQ queries)