实体框架更新多对多

时间:2015-01-21 15:45:46

标签: .net entity-framework

我有多对多关系目录< => LOBBid。将新目录添加到出价时,不会更新数据库。目前这是我正在尝试的不起作用。任何方向将不胜感激。

public bool UpdateBid(LOBBid bid, User user)
    {

        if (_context.LOBBids.Any(dbl => dbl.Id == bid.Id))
        {

            _context.LOBBids.Attach(bid);
            var entry = _context.Entry(bid);

            List<Catalog> catalogs = new List<Catalog>();
            bid.Catalogs.ToList().ForEach(cat => catalogs.Add(cat));

            var dbAction = _context.LOBBids.Include("Catalogs").First(bd => bd.Id == bid.Id);
            dbAction.Catalogs.Clear();

            foreach (var cat in catalogs)
            {
                var dbCatalog = _context.Catalogs.Find(cat.ID);
                dbAction.Catalogs.Add(dbCatalog);
            }

            var actionEntry = _context.Entry(dbAction);

            actionEntry.State = EntityState.Modified;

            _context.SaveChanges();

            return true;
        }
        return false;
    }

LOBBid类定义:

public partial class LOBBid
    {
        public LOBBid()
        {
            this.ChildBids = new HashSet<LOBBid>();
            this.Notes = new HashSet<Note>();
            this.LOBItems = new HashSet<LOBItem>();
            this.Catalogs = new HashSet<Catalog>();
        }

        public int Id { get; set; }
        public Nullable<int> ParentBidId { get; set; }
        public int ApprovalStatusId { get; set; }
        public string Program { get; set; }
        public Nullable<int> BusinessUnitId { get; set; }
        public int StateId { get; set; }
        public Nullable<bool> EvalPackageException { get; set; }
        public int BidYear { get; set; }
        public Nullable<int> SalesYear { get; set; }
        public int InventoryTeamId { get; set; }
        public int MarketingTeamId { get; set; }
        public int SalesTeamId { get; set; }
        public Nullable<System.DateTime> StateBidSampleDate { get; set; }
        public Nullable<System.DateTime> EvalPackageDate { get; set; }
        public Nullable<System.DateTime> SASDate { get; set; }
        public Nullable<System.DateTime> EarlySASDate { get; set; }
        public Nullable<System.DateTime> BidDueDate { get; set; }
        public Nullable<bool> Locked { get; set; }
        public string Catalog { get; set; }
        public string OptionalTitle { get; set; }
        public Nullable<int> RevisionNumber { get; set; }
        public Nullable<bool> Enabled { get; set; }
        public string CreateUser { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }

        public virtual ApprovalStatus ApprovalStatus { get; set; }
        public virtual BusinessUnit BusinessUnit { get; set; }
        public virtual Team InventoryTeam { get; set; }
        public virtual Team MarketingTeam { get; set; }
        public virtual ICollection<LOBBid> ChildBids { get; set; }
        public virtual LOBBid ParentBid { get; set; }
        public virtual Team SalesTeam { get; set; }
        public virtual ICollection<Note> Notes { get; set; }
        public virtual State State { get; set; }
        public virtual ICollection<LOBItem> LOBItems { get; set; }
        public virtual ICollection<Catalog> Catalogs { get; set; }
    }

2 个答案:

答案 0 :(得分:0)

这就是我要做的事情(但我可能会错过积分):

public bool UpdateBid(LOBBid bid, User user)
{

    var dbBid = _context.LOBBids.Include("Catalogs").FirstOrDefault(
         bd => bd.Id == bid.Id);
    if ( dbBid != null )
    {
        //clear catalog
        dbBid.Catalogs.Clear();
        //select only existing catalogs. Here optimisation can be done preventing to get the catalogs from the db.
        var l = bid.Catalogs.Select(x => x.Id);
        foreach(var cDb in _context.Catalogs.Where(x => l.Contains(x.Id)) ) {
            //inserting
            dbBid.Catalogs.Add(cDb);
        }
        //updating the context
        _context.SaveChanges();

        return true;
    }
    return false;
}

答案 1 :(得分:0)

所以为了解决这个问题,基本上我一直在尝试不同的事情,直到点击某些东西......出于某种原因,这有效:

 public bool UpdateBid(LOBBid bid, User user)
        {
            if (_context.LOBBids.Any(dbl => dbl.Id == bid.Id))
            {
                var catIds = bid.Catalogs.Select(cat => cat.ID).ToList();                
                bid.Catalogs.Clear();//so new ones can be added

                var bidEntry = _context.Entry(bid);
                _context.LOBBids.Attach(bid);

                bidEntry.Collection(x => x.Catalogs).Load();       
                bid.Catalogs.Clear();//so old ones are deleted

                var catalogs = _context.Catalogs.Where(x => catIds.Contains(x.ID)).ToList();                
                bid.Catalogs = catalogs;

                _context.SaveChanges();

                return true;
            }
            return false;
        }