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?
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
Make it like this
and
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.
In Your case o1 is inside of contextMySql.MyEntitys and test2 is o2.
PS: plural from entity is entities.