Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3941332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:31:43+00:00 2026-05-20T00:31:43+00:00

In my application I instantiate a new Linq-to-SQL object and pass it (with some

  • 0

In my application I instantiate a new Linq-to-SQL object and pass it (with some value assigned) to a WinForms window so the user can fill out the rest.

If the user cancels the form I don’t want the data to be inserted.

The problem is that when SubmitChanges is called the next time, the record is still inserted.

Is there an easy way to prevent that? I don’t want to create a separate class with the same fields as the Linq-to-SQL class when I already have all the fields I need.

I found this article http://www.codeproject.com/KB/linq/linq-to-sql-detach.aspx, but it seems to solve a different problem and doesn’t seem like an easy solution.

Update:
I was able to reproduce the behavior in a console app.
Here is the code I used (with different names like Customer and Country):

MyDatabase db = new MyDataBase(@"Data Source:d:\test.sdf");
Customer customer = new Customer();
customer.Name = "customer 1";
customer.Country = db.Country.FirstOrDefault();

db.SubmitChanges();

The insert happened as soon as I added the “Country” assigment line. Country data in this example is stored in a different table.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:31:43+00:00Added an answer on May 20, 2026 at 12:31 am

    The record won’t be inserted unless you either call InsertOnSubmit, or it is otherwise attached to your datacontext.

    Are you inserting it to your database before you send it to the form? I assume that if your user cancels the form then the object is thrown away?

    Perhaps you could post some code, specifically around where you create the object.

    edit: it appears that your customer is being attached to the datacontext when you assign it the db.Country.FirstOrDefault(). These EntityRef / EntitySet relationships are two way – when you assign customer.Country = db.Country.FirstOrDefault() you are also effectively saying db.Country.FirstOrDefault().Customers.Add(customer), which adds the customer to the datacontext as a new object. Then when you call SubmitChanges, the customer will be inserted.

    The best approach is to either (a) keep the customer detached from the Country until you go to submit then record, or (b) to dispose of the datacontext if the user cancels / fails to submit – and then instantiate a new context. These approaches will definitely work and are a better pattern than you currently have.

    However an even simpler way to fix the problem – without any code changes – might be to change the properties of the relationship in the LINQ-to-SQL designer. This is a workaround for your immediate problem, it’s not a real fix for the underlying issue of keeping the datacontext around – it’s likely you’ll run into more problems. However give it a go because it’s a very simple fix.

    • Open the dbml in Visual Studio
    • Select the relationship between Customer and Country, and then view Properties
    • You’ll see a property for the Child and Parent of the relationship. In the Child section you want to set ‘ChildProperty’ (or it might be ‘IsChildProperty’) to false.

    Doing this removes the Customers property on the Country object, and will mean that you can set the country in the manner that you are doing without any side-effects; i.e. it won’t implicitly associate the Customer with the Country. It means that the Country object no longer ‘has’ Customers, and so you can’t accidentally attach a Customer to a Country. Let me know if this works for you!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.