简体   繁体   中英

MySQL to MSSQL keep foreign key relationships

I am currently trying to migrate from an old MySQL (5.0) to MSSQL. Because I must keep the primary key relationships, I am now facing a problem. Some data inside a table begin with the id of 6102 instead of one. I can solve this by increasing the seed, which works. Now, after several thousand of data sets, I have some leaps eg from id 22569 to 22597. This occurres multiple times.

What I basically do at the moment is, select all data from the source db (MySQL), map them into a generated model und try to map this model to my target model (MSSQL). (I do this because the target, new structure differs a little from the existing one.) When I ignore those leaps, I am getting later on several other tables a foreign key violation.

So my solution currently would be, to count from the beginning each mapping and when the id of the current model differs from the counter, to reset manually the seed in the database.

DBCC CHECKIDENT (mytable, RESEED, idFromCurrentModel);

Is there a possibility to force entity framework, respectively SQL Server to accept the id from my model instead of ignoring it and use the ident value?

Thanks for reading and best regards

EDIT

Just if anybody is wondering how I solved this, here it is:

        var context = new TestEntities();

        // map mysql data to mssql model and convert data
        // let's assume I did this
        var mapped = new List<Test>()
        {
            new Test() {id= 42, bar = "foo", created = DateTime.Now},
            new Test() {id= 1337, bar = "bar", created = DateTime.Now}
        };

        var transaction = context.Database.BeginTransaction();
        context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [SeedingTest].[dbo].[Test] ON");
        context.Test.AddRange(mapped);
        context.SaveChanges();
        context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [SeedingTest].[dbo].[Test] OFF");
        transaction.Commit();

        context.Dispose();

This only works when I do this:

Right click on my *.edmx file, open, and remove the

StoreGeneratedPattern="Identity"

in your identity column. In my case this looked like this:

...
<Property Name="id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
...

After removing this, EF was no longer ignoring my set id's.

Source

Additional information:

Adding this attribute

[DatabaseGenerated(DatabaseGeneratedOption.None)]

to my id in my generated model, did not work.

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int foo { get; set; }
public string bar { get; set; }
...

The easiest way is to set the seed initially to the current max Id value + 1. Then, when inserting the converted rows do the following

set identity_insert on tablename -- stops generation of IDENTITY, requires user to supply it

insert into tablename values (Id....) -- supply value of Id

set identity_insert_off tablename -- turn inedtity generation back on

This does it quite nicely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM