Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Return descriptions to the interface
Message
 
 
À
23/10/2015 17:27:52
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01626353
Message ID:
01626520
Vues:
47
Hi Rob,

I went with my original solution (using NotMapped GroupDescription) as I needed to show them in the interface and add/delete.

Now I am not sure how to Save entity with related entities. I did some searches online, but still not clear.

My current API call is:
 [Route("")]
        [HttpPost]
        public IHttpActionResult CreateSChannels(EditSChannelsViewModel sChannelsViewModel)
        {
            SChannels sChannels = AutoMapper.Mapper.Map<EditSChannelsViewModel, SChannels>(sChannelsViewModel);
            _sChannelsAdapter.Add(sChannels);
            sChannelsViewModel = AutoMapper.Mapper.Map<SChannels, EditSChannelsViewModel>(sChannels);
            return Ok(sChannelsViewModel);
        }
      
        [Route("")]
        [HttpPut]
        public IHttpActionResult UpdateSChannels(EditSChannelsViewModel sChannelsViewModel)
        {
            SChannels sChannels = AutoMapper.Mapper.Map<EditSChannelsViewModel, SChannels>(sChannelsViewModel);
            _sChannelsAdapter.Update(sChannels);
            sChannelsViewModel = AutoMapper.Mapper.Map<SChannels, EditSChannelsViewModel>(sChannels);
            return Ok(sChannelsViewModel);
        }
where Add or Update methods eventually lead to our generic RepositoryBase Add / Update methods:
  public virtual void Add(T entity)
        {
            EntityState entityState = _siriusContext.GetEntityState(entity);
            if (entityState != EntityState.Detached)
            {               
                    _siriusContext.SetAdd(entity);
            }
            else
            {
                _dbSet.Add(entity);
            }
            try
            {
                SaveChanges();
            }
            catch (DbUpdateException ex)
            {
                _exceptionParser.ThrowUniqueConstraintException(ex);
            }
        }
        
        public virtual void Update(T entity)
        {
            try
            {
                EntityState entityState = _siriusContext.GetEntityState(entity);
                if (entityState == EntityState.Detached)
                {
                    _dbSet.Attach(entity);
                    _siriusContext.SetModified(entity);
                }               

                SaveChanges();
            }
            //catch (DbEntityValidationException dbex)
            //{
            //    var msg = dbex.EntityValidationErrors.ToList();
            //}
            catch (DbUpdateException ex)
            {
                _exceptionParser.ThrowUniqueConstraintException(ex);
            }

            catch (AmbiguousMatchException ex)
            {
                Log.Error("Ambiguous Match Exception", ex);
                throw ex;
            }
        }
So, I get my entities to the client application (browser), using JavaScript I add or delete some links in the Sales Channel scspLink related entities. Now I need to save all my changes to the database. Right now I found that only the Sales Channel changes are saved but not the related entities. Do you have a simple method of dealing with the problem?

I looked into this link http://www.codeproject.com/Articles/576330/Attaching-detached-POCO-to-EF-DbContext-simple-and but don't know if that's how I should proceed for related entities (and the id column in my case is named ChannelId). It also doesn't handle deletes and assumes a separate deleted object.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform