I'm facing a problem which one I tried some solutions to solve, but I couldn't. Basically, I have a method which I receive as parameter one string and one serialized Map from ajax post. Everything regarding this is working really well, except the fact when I have for example the same FileId with different UseCaseId, because then just one information is being saved, not both.
**UseCaseId and FileId ** are foreign keys from other tables.
The relationship:
modelBuilder.Entity<Content>()
.HasIndex(ct => ct.FileId)
.IsUnique(false);
modelBuilder.Entity<Content>()
.HasIndex(ct => ct.UseCaseId)
.IsUnique(false);
modelBuilder.Entity<Content>()
.HasKey(cn => cn.ContentId);
modelBuilder.Entity<Content>()
.HasOne(cn => cn.File)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Content>()
.HasOne(cn => cn.UseCase)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
In the follow example, two elements, exactly the same with just a different UseCase , need to be saved, but only the last one is saved in the database.
Parameter received:
This is the code where the problem occurs:
[HttpPost]
public JsonResult Create([FromBody] ContentJson data)
{
List<Content> contents = new List<Content>();
var contentName = string.Empty;
var array = data.content;
int order;
for (int i = 0; i < array.Count; i++)
{
var files = JsonConvert.DeserializeObject<string[]>(array[i][1].ToString());
if (files == null || files.Length == 0)
continue;
contentName = array[i][0].ToString();
order = 1;
foreach(var fileName in files)
{
Content content = new Content();
content.Order = order;
content.UseCaseId = long.Parse(contentName);
content.FileId = unitOfWork.FileRepository.Get(f => f.FileName.Equals(fileName)).First().FileId;
contents.Add(content);
order++;
}
}
unitOfWork.ContentRepository.AddRange(contents);
unitOfWork.Save();
return Json(Ok());
}
- The ContentId is automatically generated.
- EntityFramework and Dotnet in the latest version
- I've already tried to save on each iteration.
Tried to implement Equals and HashCode,
public void Save() { context.SaveChanges(); } private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { context.Dispose(); } } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public Repository<Content> ContentRepository { get { if(contentRepository == null) { contentRepository = new Repository<Content>(context); } return contentRepository; } }
Could anyone help me, please ? I got stuck on this problem.
unitOfWork.Save();
code. – Hadi Samadzad Jan 21 '20 at 12:44Repository
class whoseAddRange
orAdd
methods are missing. – Panagiotis Kanavos Jan 21 '20 at 13:31