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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:01:42+00:00 2026-06-15T17:01:42+00:00

I have the same entity in a context with SQL Server and in a

  • 0

I have the same entity in a context with SQL Server and in a context with MySql, and I need add the same entity from SQL server to MySql, but I cant.

I have this code:

    var test1 = contextSQL.MyEntitys.Where(m => m.Id == 1000).FirstOrDefault();
    var test2 = contextMySql.MyEntitys.Where(m => m.Id == 1000).FirstOrDefault();

    if(test2 != null)
       test2 = test1;
    else
       contextMySql.MyEntitys.Add(test1);

    contextMySql.SaveChanges();

And this doesn’t work.

I tried a lot of things and sample code from others with apparently the same problem, but nothing works. Any idea?

  • 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-15T17:01:43+00:00Added an answer on June 15, 2026 at 5:01 pm
    test2 = test1;
    

    here You just make test2 to reference test1 but You don’t change it, implement method getValues and setValues for Your entity and do like this

    test2.setValues(test1.getValues());
    

    Make it like this

    public void setValues(MyEntity entity) {
    this.Name = entity.getName();
    }
    

    and

    test2.setValues(test1);
    

    The point is that
    contextMySql.MyEntitys holds reference to same object that test2 points at. It is thay point at same object but they are not the same, they are two copies of same address. If You put new address into one it doesn’t affect other, but if You put something inside one it will be inside both of them.

    Object o1 = new Object();
    Object o2 = o1;
    
    o1.setId(1);
    //both have id 1
    
    o2.setId(2);
    //both have id 2
    
    o2 = new Object();
    o2.setId(3);
    //o1 still have id 2 but o2 has id 3
    

    In Your case o1 is inside of contextMySql.MyEntitys and test2 is o2.

    PS: plural from entity is entities.

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

Sidebar

Related Questions

Why does the Entity Framework generate nested SQL queries? I have this code var
I have an entity framework data context and a SQL command that both need
I recently had to switch from using Microsofts SQL server to using MySQL. The
I'm using Entity Framework 5 on .NET 4.5 with SQL Server 2012. I have
I'm using Entity Framework (4.3) and code first (along with SQL server 2012 localdb).
I have 4 entities in my core data with the same parent entity. They
I recently installed xampp, followed the installation. MySQL/apache. (I have same website running on
I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0
Is it possible to extend LINQ-to-SQL entity-classes with constructor-methods and in the same go;
See the code below. If I initialize more than one entity context, then I

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.