How to add computed column using migrations in code first?

邮差的信 提交于 2019-12-06 06:21:20

Entity Framework doesn't know how to properly handle migrations for computed columns, so you need to help it out.

Firstly, delete the computed column from the table in the database.

Then create a new migration in the package manager console:

add-migration FullNameComputed

Replace the body of the Up() method in the new migration with the following:

Sql("ALTER TABLE [TableName] ADD [FullName] AS ([FirstName] + ' ' + [LastName])");

Finally, run the migration from the package manager console:

update-database
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!