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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:31:13+00:00 2026-05-11T18:31:13+00:00

I have implemented a sort of Repository class and it has has GetByID ,

  • 0

I have implemented a sort of Repository class and it has has GetByID, DeleteByID methods and so on, but I’m having trouble implementing the UpdateByID method.

I did something like this:

public virtual void UpdateByID(int id, T entity)
{
        var dbcontext = DB;
        var item = GetByID(dbcontext, id);
        item = entity; 
        dbcontext.SubmitChanges();
}

protected MusicRepo_DBDataContext DB
{
    get
    {
        return new MusicRepo_DBDataContext();
    }
}

But it’s not updating the passed entity.

Has anyone implemented such a method ?


For reference, here is the GetByID method


[Update]

As Marc correctly suggested, I am merely changing the values of the local variable. So how do you think I should go about this method? Use reflection and copy the properties from entity to item ?

  • 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-11T18:31:13+00:00Added an answer on May 11, 2026 at 6:31 pm

    All you have updated is a local variable; for that to work you would have to copy the member values from entity to item – not quite so simple.


    Something like below; the only reason I used TKey was that I tested on Northwind.Customer, which has a string key ;-p

    The advantage of using the meta-model is that it works even if you are using POCO classes (and the xml-based mapping), and it doesn’t try to update anything unrelated to the model.

    For the purposes of the example, I have passed in the data-context, and you need to add a SubmitChanges at some point, but the rest should be directly comparable.

    BTW – if you are happy to take the ID from the passed in object, that would be easy too – and then you could support composite identity tables.

        static void Update<TEntity>(DataContext dataContext, int id, TEntity obj)
            where TEntity : class
        {
            Update<TEntity, int>(dataContext, id, obj);
        }
        static void Update<TEntity, TKey>(DataContext dataContext, TKey id, TEntity obj)
            where TEntity : class
        {
            // get the row from the database using the meta-model
            MetaType meta = dataContext.Mapping.GetTable(typeof(TEntity)).RowType;
            if(meta.IdentityMembers.Count != 1) throw new InvalidOperationException("Composite identity not supported");
            string idName = meta.IdentityMembers[0].Member.Name;
    
            var param = Expression.Parameter(typeof(TEntity), "row");
            var lambda = Expression.Lambda<Func<TEntity,bool>>(
                Expression.Equal(
                    Expression.PropertyOrField(param, idName),
                    Expression.Constant(id, typeof(TKey))), param);
    
            object dbRow = dataContext.GetTable<TEntity>().Single(lambda);
    
            foreach (MetaDataMember member in meta.DataMembers)
            {
                // don't copy ID
                if (member.IsPrimaryKey) continue; // removed: || member.IsVersion
                // (perhaps exclude associations and timestamp/rowversion? too)
    
                // if you get problems, try using StorageAccessor instead -
                // this will typically skip validation, etc
                member.MemberAccessor.SetBoxedValue(
                    ref dbRow, member.MemberAccessor.GetBoxedValue(obj));
            }
            // submit changes here?
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a quick sort algorithm but when I tested it I noticed
I have implemented the SortableSearchableList class found at http://msdn.microsoft.com/en-us/library/aa480736.aspx and have added a Sort
I have a buffered grid on which I've implemented a local sort function (client
I have implemented pagination to my data, but the problem is I only have
I have implemented a test method with Jersey to run on my Google AppEngine
I have implemented Facebook into my app but now I find that whenever I
I have implemented an insertion sort in a double link list (highest to lowest)
I have implemented the sort function in my codebehind, it works fine with words
I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I have a repository method that accepts an order by parameter in the form:

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.