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

  • SEARCH
  • Home
  • 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 8069549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:05:14+00:00 2026-06-05T13:05:14+00:00

I’m coding an app that uses an existing database, that database has some tables

  • 0

I’m coding an app that uses an existing database, that database has some tables I cannot modify at all, I can nevertheless add new tables.
So, let’s say I have an old table, Cities, to which I can read, create, update and delete but I cannot modify the schema.
I then add a new table, Clients, which has a foreign key to Cities, but I cannot add a relation on the database server (SQL SERVER 2008) as it also modifies the Cities table, adding a relation.
So I thought maybe if I add them both to my linq-to-sql context, and add the relation there, linq-to-sql would be clever enough to check for referential integrity for me, even though the relation is not established on the database, it’s on the context.

I created a new project to try that out, I added two simple tables without a relation, then added the relation on the linq-to-sql designer, and tried to force an exception on referential integrity, but looks like it’s not working.

DBContextDataContext db = new DBContextDataContext();

City l = new City();
l.name= "Buenos Aires";
db.Cities.InsertOnSubmit(l);

Client c = new Client();
c.name = "Mike";
c.City = l;
db.Clients.InsertOnSubmit(c);

db.SubmitChanges(); // This works

db.Cities.DeleteOnSubmit(l); 
db.SubmitChanges(); // This shouldn't work, but it works

Any idea if it’s possible to force the referential integrity? Or adding the relation on the DB is the only way?

  • 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-06-05T13:05:15+00:00Added an answer on June 5, 2026 at 1:05 pm

    You can override the SubmitChanges method on the DataContex, and verify the changes made.

        public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
        {
            bool everythingIsOK = true;
    
            var changes = GetChangeSet();
            var inserts = changes.Inserts;
            var deletes = changes.Deletes;
            var updates = changes.Updates;
    
            //verify everything is valid
            //...
    
            //if you need to, you can get the original state of the updated objects like this:
            foreach(object x in updates) {
                var original = this.GetTable(x.GetType()).GetOriginalEntityState(x);
                //verify the change doesn't break anything
                //...
            }
    
            if(everythingIsOK){ base.SubmitChanges(failureMode); }
        }
    

    But feels painful. Are you absolutely sure, that you can’t talk to the DBA to make the required changes? You can (i guess you are allowed to too) add a table, but can’t add a foreign key, so the database may contain invalid data?

    Also, if you have to verify changes this way, that means you have to make more queries to the db to verify all keys, etc is valid.

    Or this IS definitely a hack:

    • Script the db schema for yourself
    • Recreate the db on your machine, or wherever you want to
    • Add the new tables, and add foreign keys to the db
    • Generate the linq schema from your local modified db
    • Change the connection string to the real one

    And very fragile: if anything changes in the original db, you have to change it in your “dummy” db…

    So still, try to reason with your boss, the DBA, or who made this decision, because you can’t prevent a delete by someone else in the referenced table.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.