[{"data":1,"prerenderedAt":617},["ShallowReactive",2],{"blog:2007:discarding-new-entity-objects-in-linqtosql-beta-1":3,"blogMore-Development":558,"comments-discarding-new-entity-objects-in-linqtosql-beta-1":571},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":15,"body":30,"_type":550,"_id":551,"_source":552,"_file":553,"_stem":554,"_extension":555,"url":556,"wordCount":557,"minutes":207,"commentCount":207},"/blog/2007/discarding-new-entity-objects-in-linqtosql-beta-1","2007",false,"en","Discarding new entity objects in LINQ to SQL beta 1","An unusual bug in .NET Framework 3.5 beta 1 that relates to creating a new entity object in LINQ to SQL has been getting in my way.","2007-06-20T12:27:08+00:00","Development",[13,14],".NET","LINQ",{"type":16,"children":17},"root",[18,25],{"type":19,"tag":20,"props":21,"children":22},"element","p",{},[23],{"type":24,"value":9},"text",{"type":19,"tag":20,"props":26,"children":27},{},[28],{"type":24,"value":29},"When you create a new entity object it is not automatically added to the database unless you either attach it to the table or create an association to another existing entity.",{"type":16,"children":31,"toc":545},[32,36,43,47,52,66,72,154,396,402,419,424,493,505,526,531,539],{"type":19,"tag":20,"props":33,"children":34},{},[35],{"type":24,"value":9},{"type":19,"tag":37,"props":38,"children":40},"h2",{"id":39},"bug-description",[41],{"type":24,"value":42},"Bug description",{"type":19,"tag":20,"props":44,"children":45},{},[46],{"type":24,"value":29},{"type":19,"tag":20,"props":48,"children":49},{},[50],{"type":24,"value":51},"The problem arises when you decide to discard the new object instead of persisting it. Setting the property of the association(s) to null should achieve this and the LINQ to SQL designer code shows that it removes the new object from the association.",{"type":19,"tag":20,"props":53,"children":54},{},[55,57,64],{"type":24,"value":56},"When ",{"type":19,"tag":58,"props":59,"children":61},"code",{"className":60},[],[62],{"type":24,"value":63},"SubmitChanges",{"type":24,"value":65}," is called it is apparent that the new object has not been discarded but is in fact attempting to persist to the database.",{"type":19,"tag":37,"props":67,"children":69},{"id":68},"reproduction",[70],{"type":24,"value":71},"Reproduction",{"type":19,"tag":73,"props":74,"children":75},"ol",{},[76,90,107,125,137,142],{"type":19,"tag":77,"props":78,"children":79},"li",{},[80,82,88],{"type":24,"value":81},"Create a new ",{"type":19,"tag":83,"props":84,"children":85},"em",{},[86],{"type":24,"value":87},"C# Console",{"type":24,"value":89}," project",{"type":19,"tag":77,"props":91,"children":92},{},[93,95,100,102],{"type":24,"value":94},"Add a new a ",{"type":19,"tag":83,"props":96,"children":97},{},[98],{"type":24,"value":99},"LINQ to SQL file",{"type":24,"value":101}," and name it ",{"type":19,"tag":83,"props":103,"children":104},{},[105],{"type":24,"value":106},"Northwind",{"type":19,"tag":77,"props":108,"children":109},{},[110,112,117,119,123],{"type":24,"value":111},"Use ",{"type":19,"tag":83,"props":113,"children":114},{},[115],{"type":24,"value":116},"Server Explorer",{"type":24,"value":118}," to drag Orders and Customers tables from a ",{"type":19,"tag":83,"props":120,"children":121},{},[122],{"type":24,"value":106},{"type":24,"value":124}," database connection",{"type":19,"tag":77,"props":126,"children":127},{},[128,130,135],{"type":24,"value":129},"Paste the following code snippet into the ",{"type":19,"tag":83,"props":131,"children":132},{},[133],{"type":24,"value":134},"static main",{"type":24,"value":136}," method",{"type":19,"tag":77,"props":138,"children":139},{},[140],{"type":24,"value":141},"Run the program",{"type":19,"tag":77,"props":143,"children":144},{},[145,147,152],{"type":24,"value":146},"Examine the ",{"type":19,"tag":83,"props":148,"children":149},{},[150],{"type":24,"value":151},"Orders",{"type":24,"value":153}," table and notice a new a record consisting of null fields",{"type":19,"tag":155,"props":156,"children":161},"pre",{"className":157,"code":158,"language":159,"meta":160,"style":160},"language-csharp shiki shiki-themes everforest-light dracula","NorthwindDataContext dc = new NorthwindDataContext();\nCustomer alfredCustomer = dc.Customers.Single(c => c.CustomerID == \"ALFKI\");\nOrder newOrder = new Order();\nnewOrder.Customer = alfredCustomer;\nnewOrder.Customer = null;\ndc.SubmitChanges();\n","csharp","",[162],{"type":19,"tag":58,"props":163,"children":164},{"__ignoreMap":160},[165,205,298,329,352,379],{"type":19,"tag":166,"props":167,"children":170},"span",{"class":168,"line":169},"line",1,[171,177,183,189,195,200],{"type":19,"tag":166,"props":172,"children":174},{"style":173},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[175],{"type":24,"value":176},"NorthwindDataContext",{"type":19,"tag":166,"props":178,"children":180},{"style":179},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[181],{"type":24,"value":182}," dc ",{"type":19,"tag":166,"props":184,"children":186},{"style":185},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[187],{"type":24,"value":188},"=",{"type":19,"tag":166,"props":190,"children":192},{"style":191},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[193],{"type":24,"value":194}," new",{"type":19,"tag":166,"props":196,"children":197},{"style":173},[198],{"type":24,"value":199}," NorthwindDataContext",{"type":19,"tag":166,"props":201,"children":202},{"style":179},[203],{"type":24,"value":204},"();\n",{"type":19,"tag":166,"props":206,"children":208},{"class":168,"line":207},2,[209,214,219,223,228,234,239,245,250,256,261,266,271,276,282,288,293],{"type":19,"tag":166,"props":210,"children":211},{"style":173},[212],{"type":24,"value":213},"Customer",{"type":19,"tag":166,"props":215,"children":216},{"style":179},[217],{"type":24,"value":218}," alfredCustomer ",{"type":19,"tag":166,"props":220,"children":221},{"style":185},[222],{"type":24,"value":188},{"type":19,"tag":166,"props":224,"children":225},{"style":179},[226],{"type":24,"value":227}," dc.",{"type":19,"tag":166,"props":229,"children":231},{"style":230},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[232],{"type":24,"value":233},"Customers",{"type":19,"tag":166,"props":235,"children":236},{"style":179},[237],{"type":24,"value":238},".",{"type":19,"tag":166,"props":240,"children":242},{"style":241},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[243],{"type":24,"value":244},"Single",{"type":19,"tag":166,"props":246,"children":247},{"style":179},[248],{"type":24,"value":249},"(",{"type":19,"tag":166,"props":251,"children":253},{"style":252},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[254],{"type":24,"value":255},"c",{"type":19,"tag":166,"props":257,"children":258},{"style":185},[259],{"type":24,"value":260}," =>",{"type":19,"tag":166,"props":262,"children":263},{"style":179},[264],{"type":24,"value":265}," c.",{"type":19,"tag":166,"props":267,"children":268},{"style":230},[269],{"type":24,"value":270},"CustomerID",{"type":19,"tag":166,"props":272,"children":273},{"style":185},[274],{"type":24,"value":275}," ==",{"type":19,"tag":166,"props":277,"children":279},{"style":278},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[280],{"type":24,"value":281}," \"",{"type":19,"tag":166,"props":283,"children":285},{"style":284},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[286],{"type":24,"value":287},"ALFKI",{"type":19,"tag":166,"props":289,"children":290},{"style":278},[291],{"type":24,"value":292},"\"",{"type":19,"tag":166,"props":294,"children":295},{"style":179},[296],{"type":24,"value":297},");\n",{"type":19,"tag":166,"props":299,"children":301},{"class":168,"line":300},3,[302,307,312,316,320,325],{"type":19,"tag":166,"props":303,"children":304},{"style":173},[305],{"type":24,"value":306},"Order",{"type":19,"tag":166,"props":308,"children":309},{"style":179},[310],{"type":24,"value":311}," newOrder ",{"type":19,"tag":166,"props":313,"children":314},{"style":185},[315],{"type":24,"value":188},{"type":19,"tag":166,"props":317,"children":318},{"style":191},[319],{"type":24,"value":194},{"type":19,"tag":166,"props":321,"children":322},{"style":173},[323],{"type":24,"value":324}," Order",{"type":19,"tag":166,"props":326,"children":327},{"style":179},[328],{"type":24,"value":204},{"type":19,"tag":166,"props":330,"children":332},{"class":168,"line":331},4,[333,338,342,347],{"type":19,"tag":166,"props":334,"children":335},{"style":179},[336],{"type":24,"value":337},"newOrder.",{"type":19,"tag":166,"props":339,"children":340},{"style":230},[341],{"type":24,"value":213},{"type":19,"tag":166,"props":343,"children":344},{"style":185},[345],{"type":24,"value":346}," =",{"type":19,"tag":166,"props":348,"children":349},{"style":179},[350],{"type":24,"value":351}," alfredCustomer;\n",{"type":19,"tag":166,"props":353,"children":355},{"class":168,"line":354},5,[356,360,364,368,374],{"type":19,"tag":166,"props":357,"children":358},{"style":179},[359],{"type":24,"value":337},{"type":19,"tag":166,"props":361,"children":362},{"style":230},[363],{"type":24,"value":213},{"type":19,"tag":166,"props":365,"children":366},{"style":185},[367],{"type":24,"value":346},{"type":19,"tag":166,"props":369,"children":371},{"style":370},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[372],{"type":24,"value":373}," null",{"type":19,"tag":166,"props":375,"children":376},{"style":179},[377],{"type":24,"value":378},";\n",{"type":19,"tag":166,"props":380,"children":382},{"class":168,"line":381},6,[383,388,392],{"type":19,"tag":166,"props":384,"children":385},{"style":179},[386],{"type":24,"value":387},"dc.",{"type":19,"tag":166,"props":389,"children":390},{"style":241},[391],{"type":24,"value":63},{"type":19,"tag":166,"props":393,"children":394},{"style":179},[395],{"type":24,"value":204},{"type":19,"tag":37,"props":397,"children":399},{"id":398},"workaround",[400],{"type":24,"value":401},"Workaround",{"type":19,"tag":20,"props":403,"children":404},{},[405,407,412,414,418],{"type":24,"value":406},"It would appear there is at least a temporary workaround which is to force the association that was modified to reload. You can do this by calling the associations ",{"type":19,"tag":83,"props":408,"children":409},{},[410],{"type":24,"value":411},"Load",{"type":24,"value":413}," method after the entity has been de-associated but before ",{"type":19,"tag":83,"props":415,"children":416},{},[417],{"type":24,"value":63},{"type":24,"value":238},{"type":19,"tag":20,"props":420,"children":421},{},[422],{"type":24,"value":423},"In our reproduction this would therefore be:",{"type":19,"tag":155,"props":425,"children":427},{"className":157,"code":426,"language":159,"meta":160,"style":160},"newOrder.Customer = null;\nalfredCustomer.Orders.Load();\ndc.SubmitChanges();\n",[428],{"type":19,"tag":58,"props":429,"children":430},{"__ignoreMap":160},[431,454,478],{"type":19,"tag":166,"props":432,"children":433},{"class":168,"line":169},[434,438,442,446,450],{"type":19,"tag":166,"props":435,"children":436},{"style":179},[437],{"type":24,"value":337},{"type":19,"tag":166,"props":439,"children":440},{"style":230},[441],{"type":24,"value":213},{"type":19,"tag":166,"props":443,"children":444},{"style":185},[445],{"type":24,"value":346},{"type":19,"tag":166,"props":447,"children":448},{"style":370},[449],{"type":24,"value":373},{"type":19,"tag":166,"props":451,"children":452},{"style":179},[453],{"type":24,"value":378},{"type":19,"tag":166,"props":455,"children":456},{"class":168,"line":207},[457,462,466,470,474],{"type":19,"tag":166,"props":458,"children":459},{"style":179},[460],{"type":24,"value":461},"alfredCustomer.",{"type":19,"tag":166,"props":463,"children":464},{"style":230},[465],{"type":24,"value":151},{"type":19,"tag":166,"props":467,"children":468},{"style":179},[469],{"type":24,"value":238},{"type":19,"tag":166,"props":471,"children":472},{"style":241},[473],{"type":24,"value":411},{"type":19,"tag":166,"props":475,"children":476},{"style":179},[477],{"type":24,"value":204},{"type":19,"tag":166,"props":479,"children":480},{"class":168,"line":300},[481,485,489],{"type":19,"tag":166,"props":482,"children":483},{"style":179},[484],{"type":24,"value":387},{"type":19,"tag":166,"props":486,"children":487},{"style":241},[488],{"type":24,"value":63},{"type":19,"tag":166,"props":490,"children":491},{"style":179},[492],{"type":24,"value":204},{"type":19,"tag":20,"props":494,"children":495},{},[496,498,503],{"type":24,"value":497},"Calling ",{"type":19,"tag":58,"props":499,"children":501},{"className":500},[],[502],{"type":24,"value":411},{"type":24,"value":504}," does not disrupt other still-valid associations that have not yet persisted.",{"type":19,"tag":20,"props":506,"children":507},{},[508,510,516,518,524],{"type":24,"value":509},"Oddly, accessing the ",{"type":19,"tag":58,"props":511,"children":513},{"className":512},[],[514],{"type":24,"value":515},"Count",{"type":24,"value":517}," property of the association also causes the problem to disappear because the ",{"type":19,"tag":58,"props":519,"children":521},{"className":520},[],[522],{"type":24,"value":523},"EntitySet\u003CT>",{"type":24,"value":525}," class that is responsible for representing these one-to-many associations calls Load every time you access Count. This is a potential performance hit as accessing a Count` property is considered to be a fast operation that does not go off and rebuild collections.",{"type":19,"tag":20,"props":527,"children":528},{},[529],{"type":24,"value":530},"Here’s hoping that Microsoft fix those two bugs before release and ideally before beta 2.",{"type":19,"tag":20,"props":532,"children":533},{},[534],{"type":19,"tag":83,"props":535,"children":536},{},[537],{"type":24,"value":538},"[)amien",{"type":19,"tag":540,"props":541,"children":542},"style",{},[543],{"type":24,"value":544},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":160,"searchDepth":207,"depth":207,"links":546},[547,548,549],{"id":39,"depth":207,"text":42},{"id":68,"depth":207,"text":71},{"id":398,"depth":207,"text":401},"markdown","content:blog:2007:discarding-new-entity-objects-in-linqtosql-beta-1.md","content","blog/2007/discarding-new-entity-objects-in-linqtosql-beta-1.md","blog/2007/discarding-new-entity-objects-in-linqtosql-beta-1","md","/blog/2007/discarding-new-entity-objects-in-linqtosql-beta-1/",414,[559,563,567],{"title":560,"date":561,"url":562},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":564,"date":565,"url":566},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":568,"date":569,"url":570},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[572,599],{"_path":573,"_dir":574,"_draft":6,"_partial":6,"_locale":7,"title":575,"description":576,"id":577,"name":578,"email":579,"avatar":580,"url":581,"date":582,"body":583,"_type":550,"_id":596,"_source":552,"_file":597,"_stem":598,"_extension":555},"/comments/discarding-new-entity-objects-in-linqtosql-beta-1/1928","discarding-new-entity-objects-in-linqtosql-beta-1","1928","Great to hear Matt.",1928,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2007-06-24T23:12:25",{"type":16,"children":584,"toc":594},[585,589],{"type":19,"tag":20,"props":586,"children":587},{},[588],{"type":24,"value":576},{"type":19,"tag":20,"props":590,"children":591},{},[592],{"type":24,"value":593},"Looking forward to beta 2 :)",{"title":160,"searchDepth":207,"depth":207,"links":595},[],"content:comments:discarding-new-entity-objects-in-linqtosql-beta-1:1928.md","comments/discarding-new-entity-objects-in-linqtosql-beta-1/1928.md","comments/discarding-new-entity-objects-in-linqtosql-beta-1/1928",{"_path":600,"_dir":574,"_draft":6,"_partial":6,"_locale":7,"title":601,"description":602,"id":603,"name":604,"date":605,"body":606,"_type":550,"_id":614,"_source":552,"_file":615,"_stem":616,"_extension":555},"/comments/discarding-new-entity-objects-in-linqtosql-beta-1/1927","1927","Fortunately, since I fixed those myself, I can honestly say they are fixed in Beta 2.",1927,"Matt Warren","2007-06-24T19:31:31",{"type":16,"children":607,"toc":612},[608],{"type":19,"tag":20,"props":609,"children":610},{},[611],{"type":24,"value":602},{"title":160,"searchDepth":207,"depth":207,"links":613},[],"content:comments:discarding-new-entity-objects-in-linqtosql-beta-1:1927.md","comments/discarding-new-entity-objects-in-linqtosql-beta-1/1927.md","comments/discarding-new-entity-objects-in-linqtosql-beta-1/1927",1779264593072]