Linq to Sql Delete (Disconnected Object)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daokfella

    Linq to Sql Delete (Disconnected Object)

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

    MyObjectType myobject = MyDataContext.M yObjects.Single (o =>
    o.MyObjectId == id);
    MyDataContext.M yObjects.Delete OnSubmit(o);
    MyDataContext.S ubmitChanges();

    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.MyObje ctId = id;
    MyDataContext.M yObjects.Attach (c);
    MyDataContext.M yObjects.Delete OnSubmit(c);
    MyDataContext.S ubmitChanges();

    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_MyObject s] AS [t0]
    WHERE [t0].[MyObjectId] = @p0',N'@p0 int',@p0=16

    Am I missing something or just completely misunderstandin g 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
Working...