adding a foreign key with Code First migration

前端 未结 6 1790
花落未央
花落未央 2020-12-24 02:15

I have an error when trying to update my database after adding a migration.

Here are my classes before add-migration

public class Product
{
    publi         


        
6条回答
  •  执念已碎
    2020-12-24 02:50

    The quick answer is - firstly add a nullable column for ProductId, then set some default value in all existing rows, then set the column to non null (if you need that) for future inserts, add finally add the foreign key constraint.

    I wrote a long blog post on just this with full source code included - http://nodogmablog.bryanhogan.net/2015/04/entity-framework-non-null-foreign-key-migration/

提交回复
热议问题