Converting data using C # during Entity Framework migration

Is it possible to convert the data stored in a column using the calculation performed in C # during the migration of the Entity First database?

I currently have a column called Content type nvarchar . I would like to replace it with a column named ContentBinary type varbinary , copying the contents of each row in the process, but also converting the contents.

In particular, I want to convert a string to UTF-8 encoding, and then compress it.

I know that the DbMigration class allows you to convert / move data using Sql * () methods, but these methods require that all the conversion logic be in SQL. I think this would require that the compression logic be duplicated as a stored procedure in SQL Server, which would double the required effort and lead to potential inconsistency simply by using the normal C # compression procedure.

I would like to be able to iterate through all the lines, read each Content value, apply the conversion in C #, and then write it to the ContentBinary .

I think this can happen as part of a migration transaction for consistency, but also because only before Content is transferred, only ContentBinary will exist. I suppose that precludes opening a separate database connection during the migration. However, if there is a way to access the connection used for the migration transaction, this may be sufficient.

+1
source share
1 answer

you can use the AddOrUpdate method as shown below

 context.TableName.AddOrUpdate(x=> x.ID, collection); 

where the collection is the table itself after applying the transform

If you provided some code in your question, perhaps I would be more specific.

0
source

Source: https://habr.com/ru/post/1244499/


All Articles