473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Linq to Sql Delete (Disconnected Object)

I want to delete on object from my datacontext. I know that the
following works:

MyObjectType myobject = MyDataContext.MyObjects.Single(o =>
o.MyObjectId == id);
MyDataContext.MyObjects.DeleteOnSubmit(o);
MyDataContext.SubmitChanges();

However, this hits the database twice (once for fetch, once for
delete). I thought I was able to delete an object with a single hit by
attaching an entity simply setting the primary key like this:

MyObjectType myobject = new MyObjectType ();
myobject.MyObjectId = id;
MyDataContext.MyObjects.Attach(c);
MyDataContext.MyObjects.DeleteOnSubmit(c);
MyDataContext.SubmitChanges();

However, this doesn't work. I profiled SQL and the statements run are
this:
DELETE FROM [dbo].[MyObjects] WHERE 0 = 1 <--- STRANGE!
SELECT NULL AS [EMPTY]
FROM [dbo].[INcite_MyObjects] AS [t0]
WHERE [t0].[MyObjectId] = @p0',N'@p0 int',@p0=16

Am I missing something or just completely misunderstanding how it's
supposed to work? I understand perhaps why it wants the complete
record (for concurrency), but it seems silly to fetch just to delete.

Thanks,

Jason
Jul 16 '08 #1
0 1024

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shapper | last post by:
Hello, I have two tables: TagId, PostId TagId, PostId How can I delete, given a TagId, the record from Tags and all records associated with it in PostTags.
15
by: EDBrian | last post by:
My problem is this. Our clients create different fields they want to collect and we allow them build dynamic filters, reports etc... We run some TSQL to actually create the column and all works...
3
by: shapper | last post by:
Hello, I have 3 tables, Tags, ArticlesTags and FilesTags, with the following columns: Tags TagID, Text ArticlesTags TagID, ArticleID FilesTags TagID, FileID
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
15
by: shapper | last post by:
Hello, I have two Lists: A = {ID, Name} = { (Null, John), (Null, Mary), (Null, Andrew), (Null, Peter) } B = {ID, Name} = { (1, John), (2, Robert), (3, Angela), (4, Andrew) } I want to...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
1
by: Mike Gleason jr Couturier | last post by:
Hi, Is there any benefit to store a datacontext singleton in an application variable? Should I bother? Should I just create an instance of my datacontext every time my business objects...
0
by: Big Daddy | last post by:
I am trying to do all my DB access through LINQ. For example, I am trying to delete a record from the JobQueue table. There’s a couple ways I could do this: 1. Method 1 – The disadvantage is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.