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 507329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:47:35+00:00 2026-05-13T06:47:35+00:00

If I have a LINQ to SQL table that has a field called say

  • 0

If I have a LINQ to SQL table that has a field called say Alias.

There is then a method stub called OnAliasChanging(string value);

What I want to do is to grab the value, check the database whether the value already exists and then set the value to the already entered value.

So I may be changing my alias from “griegs” to “slappy” and if slappy exists then I want to revert to the already existing value of “griegs”.

So I have;

    partial void OnaliasChanging(string value)
    {
        string prevValue = this.alias;
        this.Changed = true;
    }

When I check the value of prevValue it’s always null.

How can I get the current value of a field?

Update

If I implement something like;

    partial void OnaliasChanging(string value)
    {
        if (this.alias != null)
            this.alias = "TEST VALUE";
    }

it goes into an infinte loop which is unhealthy.

If I include a check to see whether alias already == “TEST VALUE” the infinate loop still remains as the value is always the original value.

Is there a way to do this?

  • 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-13T06:47:35+00:00Added an answer on May 13, 2026 at 6:47 am

    The code snippets you’ve posted don’t lend themselves to any plausible explanation of why you’d end up with an infinite loop. I’m thinking that this.alias might be a property, as opposed to a field as the character casing would imply, but would need to see more. If it is a property, then you are invoking the OnAliasChanging method before the property is ever set; therefore, trying to set it again in the same method will always cause an infinite loop. Normally the way to design this scenario is to either implement a Cancel property in your OnXyzChanging EventArgs derivative, or save the old value in the OnXyzChanging method and subsequently perform the check/rollback in the OnXyzChanged method if you can’t use the first (better) option.

    Fundamentally, though, what you’re trying to do is not very good design in general and goes against the principles of Linq to SQL specifically. A Linq to SQL entity is supposed to be a POCO with no awareness of sibling entities or the underlying database at all. To perform a dupe-check on every property change not only requires access to the DataContext or SqlConnection, but also causes what could technically be called a side-effect (opening up a new database connection and/or silently discarding the property change). This kind of design just screams for mysterious crashes down the road.

    In fact, your particular scenario is one of the main reasons why the DataContext class was made extensible in the first place. This type of operation belongs in there. Let’s say that the entity here is called User with table Users.

    partial class MyDataContext
    {
        public bool ChangeAlias(Guid userID, string newAlias)
        {
            User userToChange = Users.FirstOrDefault(u => u.ID == userID);
            if ((userToChange == null) || Users.Any(u => u.Alias == newAlias))
            {
                return false;
            }
            userToChange.Alias = newAlias;
    
            // Optional - remove if consumer will make additional changes
            SubmitChanges();
    
            return true;
        }
    }
    

    This encapsulates the operation you want to perform, but doesn’t prevent consumers from changing the Alias property directly. If you can live with this, I would stop right there – you should still have a UNIQUE constraint in your database itself, so this method can simply be documented and used as a safe way to attempt a name-change without risking a constraint violation later on (although there is always some risk – you can still have a race condition unless you put this all into a transaction or stored procedure).

    If you absolutely must limit access to the underlying property, one way to do this is to hide the original property and make a read-only wrapper. In the Linq designer, click on the Alias property, and on the property sheet, change the Access to Internal and the Name to AliasInternal (but don’t touch the Source!). Finally, create a partial class for the entity (I would do this in the same file as the MyDataContext partial class) and write a read-only wrapper for the property:

    partial class User
    {
        public string Alias
        {
            get { return AliasInternal; }
        }
    }
    

    You’ll also have to update the Alias references in our ChangeAlias method to AliasInternal.

    Be aware that this may break queries that try to filter/group on the new Alias wrapper (I believe Linq will complain that it can’t find a SQL mapping). The property itself will work fine as an accessor, but if you need to perform lookups on the Alias then you will likely need another GetUserByAlias helper method in MyDataContext, one which can perform the “real” query on AliasInternal.

    Things start to get a little dicey when you decide you want to mess with the data-access logic of Linq in addition to the domain logic, which is why I recommend above that you just leave the Alias property alone and document its usage appropriately. Linq is designed around optimistic concurrency; typically when you need to enforce a UNIQUE constraint in your application, you wait until the changes are actually saved and then handle the constraint violation if it happens. If you want to do it immediately your task becomes harder, which is the reason for this verbosity and general kludginess.

    One more time – I’m recommending against the additional step of creating the read-only wrapper; I’ve put up some code anyway in case your spec requires it for some reason.

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

Sidebar

Related Questions

I have a table in the database that I'm retrieving using LINQ to SQL,
I have 62 columns in a table under SQL 2005 and LINQ to SQL
While trying to use LINQ to SQL I encountered several problems. I have table
I have a LINQ-to-SQL DataContext that represents a parent-child relationship of Products to Prices.
I have a Linq to SQL query that was working just fine with SQL
I have a linq to sql database. Very simplified we have 3 tables, Projects
What I have: LINQ to SQL auto generated class, which corresponds to the shape
I have a LINQ to SQL generated class with a readonly property: <Column(Name:=totalLogins, Storage:=_TotalLogins,
I have used LINQ to SQL with a update stored procedure to update databases
I'm using LINQ to SQL and C#. I have two LINQ classes: User and

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.