简体   繁体   中英

Transform data using c# during Entity Framework migration

Is it possible to transform data stored in a column using a calculation performed in c# during an Entity First database migration?

Currently, I have a column named Content of type nvarchar . I'd like to replace it with a column named ContentBinary of type varbinary , copying the content of each row in the process, but also transforming the content.

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

I know that the DbMigration class allows for transformation / data motion using the Sql*() methods, but those methods appear to require all the transformation logic to be in SQL. I think that would require the compression logic to be duplicated as a stored procedure in SQL Server, which would double the effort required and lead to the potential for inconsistencies over just using the custom c# compression routine directly.

I'd like to be able to iterate through all the rows, read each Content value, apply the transformation in C#, and then write it to ContentBinary .

I think this may need to happen as part of the migration transaction for consistency but also because only Content will exist before the migration and only ContentBinary will exist afterward. I assume that rules out opening a separate database connection during the migration. However, if there is a way to access the connection being used for the migration transaction, perhaps that would be enough.

you can use the AddOrUpdate method like the following

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

where the collection is the table itself after apply transforming

if you provided some code in your question may be I could be more specific.

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