Zazaeil
Zazaeil

Reputation: 4109

Convert existing value from one type to another during EntityFramework migration?

I have:

    [Required]
    public string Proportion { get; set; }

which is a string property. I have to apply some simple logic to convert it to the integer. So my goal is to 1) save existing proportions, 2) drop string-column and create new int column with saved and converted to int values instead. Is it possible?

Upvotes: 2

Views: 435

Answers (1)

Shlo
Shlo

Reputation: 1046

If I get your intention correctly, you could delete your Proportion entity from the model than migrate your database with update-database -force which will delete the column from your db. than re-create the Proportion entity to int and migrate the db again.

Now you will have the Model and db to be corresponding to int type for the Proportion entity. Of course, you will have to use another variable to store the string value and than to parse it to int and store it to Proportion.

In your question you talk about values in plural. In my answer I referred to int property which get only one integer. If you have some thing like width*height i would suggest parsing into two separate properties.

Upvotes: 1

Related Questions