相关疑难解决方法(0)

实体框架代码 - 首先是null外键

我有一个User< Country模特.用户属于某个国家/地区,但可能不属于任何(空外键).

我该如何设置?当我尝试插入具有空国家/地区的用户时,它告诉我它不能为空.

模型如下:

 public class User{
    public int CountryId { get; set; }
    public Country Country { get; set; }
}

public class Country{
    public List<User> Users {get; set;}
    public int CountryId {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

错误: A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = Country_Users ]"}

mapping entity-framework foreign-key-relationship ef-code-first entity-framework-4.1

98
推荐指数
3
解决办法
8万
查看次数