I'm trying to update existing entities in my database. I want to add a m:n
relationship.
This is how I try to do it:
Save new artists
Get all genres
Update artists
public void SyncArtists(ICollection<FullArtistWrapper> fullArtists) { using (var unitOfWork = UnitOfWorkFactory.CreateUnitOfWork()) { var dbArtists = unitOfWork.Repository<IArtistRepository>().GetArtists().ToList(); var newArtists = fullArtists.Where(s => dbArtists.All(d => d.ArtistId != s.Id)).ToList(); var artistsToInsert = newArtists.Select(artist => new Artist { ArtistId = artist.Id, Name = artist.Name }).ToList(); unitOfWork.Repository<IArtistRepository>().InsertEntities(artistsToInsert); unitOfWork.Commit(); // dbArtists.AddRange(artistsToInsert); var allArtists = unitOfWork.Repository<IArtistRepository>().GetArtists().ToList(); var allGenres = unitOfWork.Repository<IGenreRepository>().GetGenres(); foreach (var artist in allArtists) { var fullArtist = fullArtists.Single(f => f.Id == artist.ArtistId); var assignedDbGenres = allGenres.Where(g => fullArtist.Genres.Any(f => f == g.Name)); artist.Genres.AddRange(assignedDbGenres); } unitOfWork.Repository<IArtistRepository>().UpdateEntities(allArtists); unitOfWork.Commit(); } }
My Entities look like this:
public class Artist : PersistenceEntity
{
public Artist()
{
Genres = new List<Genre>();
}
[Key]
public string ArtistId { get; set; }
public string Name { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
}
public class Genre : PersistenceEntity
{
[Key]
public int GenreId { get; set; }
public string Name { get; set; }
public virtual ICollection<Artist> Artist { get; set; }
}
The problem is the following:
Why is this the case? My UpdateEntities
method looks like this:
public void UpdateEntities<TPersistentEntity>(ICollection<TPersistentEntity> persistentEntitiesToBeUpdated) where TPersistentEntity : PersistenceEntity
{
persistentEntitiesToBeUpdated.ForEach(UpdateAndSave);
}
public void Update(PersistenceEntity entity)
{
Context.Entry(entity).State = EntityState.Modified;
}
public void UpdateAndSave(PersistenceEntity entity)
{
Update(entity);
Context.SaveChanges();
}
Why aren't my relationships inserted?
Thanks in advance
What is not very well visible in your code is that you cache the artists in a private member in your class. This means that EF will now know about these entities. The proper implementation of the unit of work pattern is the following:
If you manage instances outside, EF will have a different instance associated, and it will not find any changes. the code you write marking the entity manually as modified is not needed, as EF does its own change tracking. Even if you put the state manually to modified, EF will just rescan the entity it has associated (not equal to the one you have actually modified) and will not find any changes.
The main question is why you cache these artists outside of EF. This is not good practice, in general you have more than one server / client in use, and the DB can be updated by anyone. These cached instances would never know about this. I would completely get rid of such a situation and reload entities always from EF. Then you ensure that you always get the latest state no matter who has updated the DB.
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.