实体框架不保存记录到连接表

时间:2014-07-24 21:21:26

标签: c# entity-framework

我的实体模型。它的DB第一种方法。

public partial class Provider
    {
        public Provider()
        {  
            this.ProviderSubCatetogryOnes = new HashSet<ProviderSubCatetogryOne>();
        }
         public int Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<ProviderSubCatetogryOne> ProviderSubCatetogryOnes { get; set; }
    }

    public partial class SubCategoryOne
    {
        public SubCategoryOne()
        {

        }

        public int Id { get; set; }
        public string Name { get; set; }

    }

    public partial class ProviderSubCatetogryOne
    {
        public int Id { get; set; }
        public int ProviderId { get; set; }
        public int SubCategoryOneId { get; set; }

        public virtual Provider Provider { get; set; }
        public virtual SubCategoryOne SubCategoryOne { get; set; }
    }

我有提供商和类别。我必须通过选择一些类别来更新我的提供商。

我试图通过以下方式做到这一点。

var provider = GetProvider(); // Returns a Provider Object.
provider.SubCategoryOnes.Add(new ProviderSubCategoryOne()
                    {
                        SubCategoryOneId = childCategoryId // existing category id.
                    });

但是当我尝试保存提供者时,我的连接表没有填充任何记录。

以下是我用于保存的代码。

    public virtual void Update(TDomainModel item)
            {
                _logger.Info("In Update.");
                if (item == null)
                    throw new ArgumentNullException("item");
                try
                {
                    var entity = ToDataEntity(item);
                    DbEntityEntry dbEntityEntry = Context.Entry(entity);
                    try
                    {
                        if (dbEntityEntry.State == EntityState.Detached)
                        {
                            Context.Set<TEntityModel>().Attach(entity);

                            dbEntityEntry.State = EntityState.Modified;
                        }
                    }
                    catch (InvalidOperationException ex)
                    {
                        _logger.Error(ex);
                        _logger.Info("Trying to update by setting the values to CurrentEntity");

                        if (item as IUniqueId != null)
                        {
                            _logger.Warn("Model is an IUniqueId object. Trying to update by using SetValues.");
                            TEntityModel oldEntity = CurrentDbSet.Find((item as IUniqueId).Id);
                            Context.Entry(oldEntity).CurrentValues.SetValues(entity);
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex);
                    }

                    _savedRecord = entity;
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }
            }

public void Save()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                _logger.Error(ex.Message);
                foreach (DbEntityValidationResult validationResult in ex.EntityValidationErrors)
                {
                    foreach (DbValidationError validationError in validationResult.ValidationErrors)
                    {
                       _logger.Error(validationError.PropertyName+": "+validationError.ErrorMessage); 
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
        }

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试添加以下内容:

Provider.SaveChanges();

如果我没记错,将更改保存到模型中,我也使用了Database First aproach,因此可能会也可能不会。