将Detached对象添加到EF 6

时间:2015-06-20 06:26:15

标签: c# asp.net-mvc entity-framework-6

我有一个具有许多不同属性的分离对象。其中一些属性需要在某些时候“附加”,以便EF不会尝试将它们插入到数据库中。

public partial class Load 
{
    public virtual int Id {get;set;}

    [Required]
    public virtual int CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public virtual Customer Customer { get; set; }

    [Required]
    public virtual long CreatedByApplicationUserId { get; set; }

    [ForeignKey("CreatedByApplicationUserId")]
    public virtual ApplicationUser CreatedByApplicationUser { get; set; }

    public virtual long? ModifiedByApplicationUserId { get; set; }

    [ForeignKey("ModifiedByApplicationUserId")]
    public virtual ApplicationUser ModifiedByApplicationUser { get; set; }

    public virtual long? CoveredByApplicationUserId { get; set; }

    [ForeignKey("CoveredByApplicationUserId")]
    public virtual ApplicationUser CoveredByApplicationUser { get; set; }

    public virtual bool IsNetworkLoad { get; set; }

    public virtual bool IsExport { get; set; }

    public virtual bool CanTrackLoad { get; set; }

    [Required]
    public virtual DateTime CreatedDateTime { get; set; }

    public virtual DateTime? ModifiedDateTime { get; set; }

    public virtual string BillingReferenceNumber { get; set; }

    [Required]
    public virtual int LoadStatusId { get; set; }

    [ForeignKey("LoadStatusId")]
    public virtual LoadStatus LoadStatus { get; set; }

    public virtual Freight Freight { get; set; }

    public virtual ICollection<LoadOrigin> LoadOrigins { get; set; }

    public virtual ICollection<LoadDestination> LoadDestinations { get; set; }

    public virtual ICollection<LoadNote> LoadNotes { get; set; }

    public virtual ICollection<LoadCarrier> LoadCarriers { get; set; }

}

以我的属性LoadCarries为例。 LoadCarries有一个属性Carrier,它从db中填充而没有跟踪。

public partial class LoadCarrier
{
    public virtual int Id {get;set;}

    public virtual bool IsDispatched { get; set; }

    public virtual bool IsPrimary { get; set; }

    [MaxLength(25)]
    public virtual string CarrierProNumber { get; set; }

    [MaxLength(1000)]
    public virtual string RCNotes { get; set; }

    [Required]
    public virtual int CarrierId { get; set; }

    [ForeignKey("CarrierId")]
    public virtual Carrier Carrier { get; set; }

    [MaxLength(50)]
    [Required]
    public virtual string Dispatcher { get; set; }

    public virtual long LoadId { get; set; }

    [ForeignKey("LoadId")]
    public virtual Load Load { get; set; }

    public virtual LoadDriver LoadDriver { get; set; }
}

一旦我正确填充了对象,我就会尝试保存到数据库。由于这些对象都已分离,因此保存失败,因为我对Carriers对象的成员有唯一约束,因为EF错误地尝试插入已存在于db中的对象。

    public virtual int Create(T entity, long userId)
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        dbSet.Add(entity);
        return context.SaveChanges(userId);
    }

我已经多次尝试将Carriers对象的状态更改为“Unchanged”,但是我得到了一个重复的主键异常。我还尝试将Load对象添加到上下文中,然后将Carrier对象设置为我从db中明确提取的对象。有没有办法可以正确地附加这个分离的对象?

1 个答案:

答案 0 :(得分:0)

不要设置LoadCarrier的Carrier属性,而只设置CarrierId。 这足以让EF知道它必须将外键设置为现有记录。

还有其他方法可以做到这一点,但是由于Carrier是分离的,所以最好保持连接,而不是首先将其拉入。我想你正在预装载体,因为它们有点固定。