Bet*_*rte 3 c# entity-framework entity-framework-5
我有这个模型(动物模型):
public int Id { get; set; }
public int AnimalSpecieId { get; set; }
public int AnimalBreedId { get; set; }
public Nullable<int> ProtectorId { get; set; }
public Nullable<int> OwnerId { get; set; }
public string Name { get; set; }
public virtual Owner Owner { get; set; }
public virtual Protector Protector { get; set; }
Run Code Online (Sandbox Code Playgroud)
保护器型号:
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string CellPhone { get; set; }
public string Email { get; set; }
public virtual ICollection<Animal> Animals { get; set; }
Run Code Online (Sandbox Code Playgroud)
车主型号:
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string CellPhone { get; set; }
public string Email { get; set; }
public virtual ICollection<Animal> Animals { get; set; }
Run Code Online (Sandbox Code Playgroud)
当我第一次插入此模型时,如果
ProtectorID = 1
和
OwnerID = null
可以,但是,我尝试更新此模型,更改为:
OwnerID = 1
和
ProtectorID = null
我遇到标题错误,有人可以帮助我吗?
小智 5
我不同意以上答案。我不确定它是否能永久解决您的问题,因为该问题与空值分配无关。实际原因与DBContext。当我们进行任何操作时SaveChanges,都需要正确调度上下文,以便继续进行下一个操作,SaveChanges以便使用不同的外键将另一个记录插入同一项目的DB中。您只需要在“ context.SaveChanges()” 之后添加以下行
context.Entry(your object).State = System.Data.Entity.EntityState.Detached;
Run Code Online (Sandbox Code Playgroud)
这样可以解决冲突。具有相同上下文的多次插入会导致冲突。
抱歉,如果我的评论以任何方式批评了您的答案。
| 归档时间: |
|
| 查看次数: |
10390 次 |
| 最近记录: |