简体   繁体   中英

Entity framework not saving record to join table

My Entity Models. Its DB first approach.

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; }
    }

I have Providers and Categories. I have to update my Provider by selecting some of the categories.

I am trying to do this by the following.

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

But when I try to save provider, my join table is not populating with any records.

Following is the code I am using for save.

    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);
            }
        }

Any suggestions?

Try adding the following:

Provider.SaveChanges();

If I remember correctly that saves the changes to the model, also, I used Database First aproach so it might or might not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM