Entity framework code-first null foreign key

前端 未结 4 1418
梦如初夏
梦如初夏 2020-11-28 05:34

I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key).

How do I set this up? When I t

4条回答
  •  旧时难觅i
    2020-11-28 06:13

    I have the same problem now , I have foreign key and i need put it as nullable, to solve this problem you should put

        modelBuilder.Entity()
            .HasMany(c => c.Users)
            .WithOptional(c => c.Country)
            .HasForeignKey(c => c.CountryId)
            .WillCascadeOnDelete(false);
    

    in DBContext class I am sorry for answer you very late :)

提交回复
热议问题