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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:27:34+00:00 2026-05-22T16:27:34+00:00

Not in SQL but in C# using Entity Framework: Is there a simple way

  • 0

Not in SQL but in C# using Entity Framework: Is there a simple way to copy a source record to another table? The column definitions are identical however the target table has no indices, keys or constraints. I have this code and the one line I am having trouble with is marked:

private void HandleAutos()
{
    // convert to List<auto>...
    List<Model.Auto> imports = ConvertAuto();

    // get all DB records in List<auto>...
    List<Model.Auto> current = _dbFeed.Autoes.Where(a => a.ClientID == _targetClientID && a.Active == true).ToList();

    // isolate all Inserts, Updates and Deletes...
    var existing = imports.Intersect(current, new AutoIsIn()).ToList();         // should be all autos with matching VIN & SKU  //
    var updates = existing.Intersect(current, new AutoHasChanged()).ToList();   // should be a subset of changed resords        //
    var inserts = imports.Except(current, new AutoIsIn()).ToList();             // should be all the imports not in the DB      //
    var deletes = current.Except(imports, new AutoIsIn()).ToList();             // should be all the DB records not in imports  //

    // THIS IS THE BAD LINE //
    deletes.ForEach(a => _dbFeed.AddToAutoArchives(a));  // <=======
    // -------------------- //
    deletes.ForEach(a => _dbFeed.DeleteObject(a));
    updates.ForEach(a => _dbFeed.ApplyCurrentValues("Autoes", a));
    inserts.ForEach(a => _dbFeed.AddToAutoes(a));

    _dbFeed.SaveChanges();
}

I know I can loop through my source records, create an Archive record, copy each property over and finally add the new entity to the ArchiveAutos collection and save it to the DB. Before I went down that path and wrote a method to copy a record from an Auto entity to an AutoArchive entity I though I would check to see if there is already some awesome sweetness already baked in.

Do you know if there is any?

TIA

  • 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-22T16:27:35+00:00Added an answer on May 22, 2026 at 4:27 pm

    Here is the solution I came up with:

    // Archive the records to be deleted...
    if (_deletes.Count > 0)
    {
        // ignore deletes less than 60 days old...
        var myDeletes = _deletes.Where(a => a.DateCreated <= DateTime.Now.AddDays(-60)).ToList();
    
        List<Model.AutoArchive> archiveList = ConvertAuto(myDeletes);
        archiveList.ForEach(a => _dbFeed.AutoArchives.Add(a));
        _dbFeed.SaveChanges();
    
        // Drop the records no longer in the feed...
        foreach (var d in myDeletes)
        {
            var myImages = _dbFeed.Images.Where(a => a.ParentRecordID == d.AutoID).ToList();
            myImages.ForEach(a => _dbFeed.Images.Remove(a));
        }
        myDeletes.ForEach(a => _dbFeed.Autoes.Remove(a));
        _dbFeed.SaveChanges();
    }
    
    private List<Model.AutoArchive> ConvertAuto(List<Model.Auto> deletes)
    {
        List<Model.AutoArchive> aa = new List<Model.AutoArchive>();
        foreach (var a in deletes)
        {
            aa.Add(new Model.AutoArchive
            {
                ID = Guid.NewGuid(),
                Active = a.Active,
                ....
                ImageList = a.ImageList
            });
        }
    
        return aa;
    }
    

    Ugly as hell but it works. 🙁

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

Sidebar

Related Questions

I have a simple app written using SQL Server, Entity Framework, C# and WCF.
Using NHibernate from C# and only HQL (not SQL) in a way that is
Are there any open-source examples of ASP.NET MVC applications that use the Entity Framework?
I'm using the Entity Framework to model a simple parent child relationship between a
I am using SQL Azure as database with code first technique of Entity framework
Initially I deployed packages on SQL server but since my machine is not having
SQL is not my forte, but I'm working on it - thank you for
Maybe I'm not looking in the right places but I'd like to use SQL
I have setup multiple SQL Service Broker Queues in a database but have not
I've seen somewhere but can not remember. How to get a SQL string from

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.