EF 5.0 Code First adding new children to existing parent - c#

Overview:
I am working on an MVC ASP.Net app using Code First and EF 5.0. I have two tables: Scripts and ScriptItems. A Script can have multiple ScriptItems. ScriptItems are hierarchical as well. ScriptItems can optionally belong to each other, however this relationship is only 1 level deep thankfully. This relationship is indicated by ScriptItem.ParentId.
The problem:
Creating a new Script entry with ScriptItems works just fine. The problem arises when I try to add ScriptItems to an existing Script. If I try to add ScriptItems that do NOT have a ParentId, everything works fine. As soon as I try to add ScriptItems that do have a ParentId, I receive a FK violation exception.
Details:
Script class:
public class Script
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
[ForeignKey("ProcessorId")]
public Processor Processor { get; set; }
public int ProcessorId { get; set; }
public string Owner { get; set; }
public DateTime Created { get; set; }
public bool Public { get; set; }
public List<ScriptItem> Items { get; set; }
public List<ScriptRun> Runs { get; set; }
public Script()
{
Items = new List<ScriptItem>();
Created = DateTime.Now;
}
}
ScriptItem class: (Truncated for brevity)
public class ScriptItem
{
[Key]
public int Id { get; set; }
[ForeignKey("ParentId")]
public ScriptItem Parent { get; set; }
public int? ParentId { get; set; }
public Script Script { get; set; }
[ForeignKey("Script")]
public int ScriptId { get; set; }
The function that adds script items:
private void addToScript(ScriptModel model, List<int> ids)
{
Script script = scriptRepository.GetScriptWithItems(model.ScriptId);
List<History> historyItems = historyRespository.History.Where(h => ids.Contains(h.Id)).ToList();
ScriptItem lastScriptItem = script.Items.OrderByDescending(item => item.SortIndex).FirstOrDefault();
int topSortIndex = lastScriptItem == null ? 0 : lastScriptItem.SortIndex;
if (script != null)
{
List<ScriptItem> newItems = new List<ScriptItem>();
Mapper.CreateMap<History, ScriptItem>();
foreach (History h in historyItems)
{
ScriptItem scriptItem = new ScriptItem();
Mapper.Map(h, scriptItem); //Populate new ScriptItem from History entry
scriptItem.SortIndex = ++topSortIndex;
scriptItem.ScriptId = model.ScriptId;
scriptItem.Script = script;
//Only add an entry if it is NOT the parent of another entry. Otherwise, EF will duplicate the Parent entries
if (!historyItems.Any(his => his.ParentId == h.Id))
newItems.Add(scriptItem);
}
scriptRepository.AddScriptItems(newItems);
}
}
And finally the scriptRepository.AddScripItems():
public void AddScriptItems(List<ScriptItem> items)
{
items.ForEach(item => context.Entry(item).State = System.Data.EntityState.Added);
context.SaveChanges();
}
Consider the scenario where I add two ScriptItems A and B to an existing script. A is the parent of B. When I run a SQL Server trace, I see that an attempt is made to insert the parent record A, but with a ScriptId of 0 hence the FK violation exception. No clue why ScriptId is 0. ScriptId is set properly on the ScriptItems, I verified this with the debugger.
I did not include the function that inserts new Scripts and Items, because it is very similar to the addToScript function above. And it works fine. But if someone wants to see it, I can add that too.
Someone smarter than me have any ideas? Thanks!

Not sure what causes this. But I think it may help to add new ScriptItem to script.Items in stead of setting their owner script, i.e. replace
scriptItem.ScriptId = model.ScriptId;
scriptItem.Script = script;
by
script.Items.Add(scriptItem);
Another advantage is that you don't have to change their state manually anymore: the change tracker knows enough when new items are added to a tracked collection. I even wonder if doing this was necessary in your script, because setting Script should also have been enough afaik.
Maybe setting script and ScriptId and changing the state interfered too much with EF's own logic and put it off track.

Thanks to Gert Arnold for pointing me in the right direction. The issue ultimately stemmed from how I was building my ScriptItems via AutoMapper. I cant explain what the exact issue is, but here is my revised working code in case anyone finds it useful.
private void addToScript(ScriptModel model, List<int> ids)
{
Script script = scriptRepository.GetScriptWithItems(model.ScriptId);
List<History> historyItems = historyRespository.History.Where(h => ids.Contains(h.Id)).ToList();
ScriptItem lastScriptItem = script.Items.OrderByDescending(item => item.SortIndex).FirstOrDefault();
int topSortIndex = lastScriptItem == null ? 0 : lastScriptItem.SortIndex;
if (script != null)
{
Mapper.CreateMap<History, ScriptItem>();
List<ScriptItem> Parents = new List<ScriptItem>();
List<History> SourceParents = historyItems
.Where(h => historyItems.Any(h2 => h2.ParentId == h.Id)).ToList();
SourceParents.Each(h =>
{
ScriptItem parent = new ScriptItem();
Mapper.Map(h, parent);
parent.Script = script;
Parents.Add(parent);
});
historyItems.Except(SourceParents).Each(h =>
{
ScriptItem child = new ScriptItem();
Mapper.Map(h, child);
child.Script = script;
if (child.ParentId.HasValue)
child.Parent = Parents.SingleOrDefault(p => p.Id == child.ParentId);
script.Items.Add(child);
});
//Todo: Get sortIndex "sorted" out
scriptRepository.SaveScript(script);
}
}
scriptRepository.SaveScript simply sets the script's state to modified and calls SaveChanges().

Related

EF Core NullReferenceException on Related Navigation Property

I have two related models.
public class Offer
{
public long Id { get; set; }
public string OfferCode { get; set; }
public string Description { get; set; }
// more properties
public int ProductId { get; set; }
public virtual Product Product { get; set; }
}
public class Product
{
public long Id { get; set; }
public string Name { get; set; }
// more properties
public virtual ICollection<Offer> Offers { get; set; }
}
I am trying to have an MVC form with a select HTML element where Offers are grouped and products
and have the Product Names serve as optgroups.
To this end, I have a view model that I intend to populate with the grouped Offers and I have a method
to do just that.
private OfferMessageViewModel PrepareViewModel(OfferMessageViewModel viewModel)
{
var offers = _context.Offers.Include(o => o.Product).ToList()
.GroupBy(o => o.Product.Name).ToList();
foreach (var offerGroup in offers)
{
var optionGroup = new SelectListGroup
{
Name = offerGroup.Key
};
foreach (var offer in offerGroup)
{
viewModel.Offers.Add(
new SelectListItem
{
Value = offer.OfferCode,
Text = offer.Description,
Group = optionGroup
}
);
}
}
return viewModel;
}
The code gets tripped up in the GroupBy clause.
o.Product is null even when o.ProductID has a value in it.
The ToList() call right before the GroupBy is not helping.
I have tried removing the virtual modifiers on the related entities
navigation properties but the error persisted.
Installing the NuGet package Microsoft.EntityFrameworkCore.Proxies and
modifying and configuring it as such
services.AddDbContext<ApplicationDbContext>(options =>
options.UseLazyLoadingProxies()
.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
also did not make the error go away.
Is there something else I am missing?
Any help would be greatly appreciated.
EDIT:
It has been suggested that my post might be solved by this SO question. But I get the null reference exception even with lazy loading explicitly turned on.
I have tried the suggested solutions there but still no luck.
I eventually solved it.
Apparently the problem was that the foreign key was an int referencing a primary key of type long.
So I changed
public int ProductId { get; set; }
to
public long ProductId { get; set; }
in the Offer model.
Added the necessary migration, updated the database and now it works.
No more null reference exceptions.
Don't know why I missed that but it's probably a combination of lack of sleep and
a not-so-helpful error message throwing me off in a completely different direction.

Using .Select to get single item from sub list, then assigning individual properties of that item to view model

I have pretty much solved this problem but I am wondering whether there is a more efficient way of doing this using Entity framework / SQL.
Essentially, what i am doing is performing a subquery to get a SINGLE item on a list of objects that are connected to a parent entity. I then want to extract only a few columns from that single entity.
The first way, which doesn't work but shows my possible thought process was to put each object into a temporary variable and then create the view:
_context.IcoInfos.Select((i) =>
{
var reward = i.SocialRewards.OrderByDescending(s => s.EndDate).FirstOrDefault();
return new IcoInfoRewardCountViewModel()
{
CampaignName = i.Name,
CurParticipants = reward.CurParticipants,
Title = reward.CustomTitle,
IsLive = reward.IsLive
};
});
The second way, which works, I am creating a temporary model which stores the single database row of the sublist result...
_context.IcoInfos.Select((i) => new
{
Reward = i.SocialRewards.OrderByDescending(s => s.EndDate).FirstOrDefault(),
IcoName = i.Name
}).Select(t => new IcoInfoRewardCountViewModel()
{
CampaignName = t.IcoName,
CurParticipants = t.Reward.CurParticipants,
Title = t.Reward.CustomTitle,
IsLive = t.Reward.IsLive
}).ToList();
My question is, is this second way the only/best way to achieve this?
Your second approach is ok but for bigger application will cause you trouble if application growth larger and you have a lot information to store in the model.
So I think you can use automapper to make your code more clean.
Example
To use autoampper I need to define a model class and DTO class that share some same properties.
public class Comment
{
public string Content { get; set; }
public virtual Comment ParentComment { get; set; }
public virtual Post Post { get; set; }
public virtual User? User { get; set; }
public CommentStatus CommentStatus { get; set; }
}
public class CommentDto
{
public int Id { get; set; }
public Guid UniqeId { get; set; }
public string Content { get; set; }
public Comment ParentComment { get; set; }
public CommentStatus CommentStatus { get; set; }
public DateTime DateCreated { get; set; }
}
I also need to define the profile class to register mapping
public class CommentProfile : Profile
{
public CommentProfile()
{
CreateMap<Comment, CommentDto>(MemberList.None).ReverseMap();
}
}
Then I will need to register into DI container in startup.cs
services.AddAutoMapper();
Then I can use like this
var comments = await _unitOfWork.Repository<Comment>().Query()
.Include(x => x.User)
.Include(c => c.Post)
.Select(x => new CommentViewModel
{
Comment = _mapper.Map<Comment, CommentDto>(x),
})
.ToListAsync();
It will make the code more clear and I dont have to do manual mapping

Deleting Child Entries when deleting parent

I've read around various posts but none seem to match my issue.
I need to delete child entries linked to a foreignkey when the parent is deleted.
Currently I have this code:
public async Task UpdateLineItemByOrderLineId(int orderLineId, int newQuantity)
{
var clientBasket = await GetBasketAsync();
var lineItem = clientBasket.OrderLines.FirstOrDefault(x => x.OrderLineId == orderLineId);
if (newQuantity == 0)
{
_orderLinesRepository.Delete(lineItem);
_orderLinesRepository.Save();
}
and this linked to the following repository
public class OrderLinesRepository : GenericRepository<OrderLine>, IOrderLinesRepository
{
public OrderLinesRepository(IDbContextFactory dbContextFactory)
: base(dbContextFactory)
{
}
}
Posts seem to mention entity framework etc and being as I'm learning C# from a solution handed to me to bring to completion I don't see matching code that reflects EF.
I don't necessarily need to delete the child elements but simply set the ForeignKey to null. One thing to note is that I can have multiple child entries linked to the ForeignKey.
What is the correct implementation to achieve the above?
Edit: Foreign Key Assignment
namespace TSW.Ecommerce.Data.Api
{
public class OrderDelegate
{
[Key]
public int OrderDelegateId { get; set; }
[ForeignKey("OrderLineId")]
public virtual OrderLine OrderLine { get; set; }
public int? OrderLineId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
}
Correct solution is:
foreach (var m in lineItem.DelegatesList.Where(f=>f.OrderLineId == orderLineId))
{
lineItem.DelegatesList.Remove(m);
}

Entity Framework 6 doesn't save navigated object (state is Unchanged) if parent object has all properties virtual

I have following classes:
public class A
{
[Key]
public virtual int ID { get; set; } //virtual here raises error!
public virtual B B { get; set; }
}
public class B
{
[Key]
public int ID { get; set; }
[Required]
public string title { get; set; }
}
and code:
var context = new Model1();
var dbSet = context.Set<A>();
var dbSet1 = context.Set<B>();
var a = dbSet.Find(1);
var b = a.B;
b.title = DateTime.Now.Ticks.ToString();
int changes1 = context.SaveChanges();
if (changes1 == 0)
throw new Exception("not updated");
if I remove "virtual" from property ID in class A everything is working. I need the property virtual in order to use the model also in nhibernate.
thanks
I was able to reproduce it, and apparently it's EF6 bug.
I can suggest 2 workarounds. Either (1) make all B members virtual as well, or (2) eager load (lazy and explicit loading doesn't work) the navigation property before editing it.
i.e. instead of
var a = dbSet.Find(1); // doesn't work
use
var a = dbSet.Include(e => e.B).First(e => e.ID == 1); // works

Recursive Entity Update

I have an entity which holds a list of entities (same as root entity) to represent a Folder structure:
public class SopFolder
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? LastUpdated { get; set; }
public int Status { get; set; }
public virtual ICollection<SopField> SopFields { get; set; }
public virtual ICollection<SopFolder> SopFolderChildrens { get; set; }
public virtual ICollection<SopBlock> Blocks { get; set; }
public virtual ICollection<SopReview> Reviews { get; set; }
}
This entity is stored in my DB using Code-First Approach which is working fine. I then print the entity to a KendoUI Treeview, let the user modify it and on "save" post it back to the Server to an Action as IEnumerable<TreeViewItemModel> items.
I then look for the ROOT entity with all it's children (there is only one root) and convert it back into an SopFolder object.
To get the full object updated in the database I do the following:
List<SopFolder> sopfolderlist = ConvertTree(items.First());
SopFolder sopfolder = sopfolderlist[0];
if (ModelState.IsValid)
{
SopFolder startFolder = new SopFolder { Id = sopfolder.Id };
//db.SopFolders.Attach(startFolder);
// db.SopFolders.Attach(sopfolder);
startFolder.Name = sopfolder.Name;
startFolder.LastUpdated = sopfolder.LastUpdated;
startFolder.SopFields = sopfolder.SopFields;
startFolder.SopFolderChildrens = sopfolder.SopFolderChildrens;
startFolder.Status = sopfolder.Status;
db.Entry(startFolder).State = EntityState.Modified;
db.SaveChanges();
return Content("true");
}
However this is not working. The model is not updated at all. If I shift the "entityState.Modified" before the modifications, it just creates a complete fresh duplicate of my data in the database (modified of course).
Is my approach correct or do I have to go a different path? What am I missing here? I guess there is another "hidden" id which lets the EF map the entities to the db entries but I am not sure about this. Thanks for help!
UPDATE:
Instead of creatinga new instance of SopFolder I also tried db.SopFolders.Find(sopfolder.Id) and this works for entries with no children. If I have entities with children, it creates a duplicate.
Regards,
Marcus
This is typical Disconnected Graph scenario. Please see this question for possible solutions:
Disconnected Behavior of Entity Framework when Updating Object Graph
You have already figure out the first solution - that is: update entities separately. Actually, what you should do is to fetch the original data from database and then do comparison of what have changed. There are some generic ways of doing that, some of them are described in "Programming EF DbContext" book by J.Lerman, which I strongly recommend to you before doing more coding using EF.
P.S. IMHO this is the worse downside of EF.
Replace SopFolder startFolder = new SopFolder { Id = sopfolder.Id }; with
SopFolder startFolder = db.SopFolders.FirstOrDefault(s=>s.Id.Equals(sopfolder.Id));
// then validate if startFolder != null
I recommend you to create your entity model with ParentId, not children object list. When you need treeview model collect it with recursive function from database.
public class SopFolder
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime? LastUpdated { get; set; }
public int Status { get; set; }
public virtual ICollection<SopField> SopFields { get; set; }
//public virtual ICollection<SopFolder> SopFolderChildrens { get; set; }
public int? ParentFolderId { get; set; }
public virtual ICollection<SopBlock> Blocks { get; set; }
public virtual ICollection<SopReview> Reviews { get; set; }
}
When you create children folders, select it's parent, so collect your data. In childrens case try this :
List<SopFolder> sopfolderlist = ConvertTree(items.First());
SopFolder sopfolder = sopfolderlist[0];
if (ModelState.IsValid)
{
SopFolder startFolder = new SopFolder { Id = sopfolder.Id };
//db.SopFolders.Attach(startFolder);
// db.SopFolders.Attach(sopfolder);
startFolder.Name = sopfolder.Name;
startFolder.LastUpdated = sopfolder.LastUpdated;
startFolder.SopFields = sopfolder.SopFields;
startFolder.SopFolderChildrens = sopfolder.SopFolderChildrens;
foreach (var child in sopfolder.SopFolderChildrens)
{
db.SopFolders.CurrentValues.SetValues(child);
db.SaveChanges();
}
startFolder.Status = sopfolder.Status;
db.Entry(startFolder).State = EntityState.Modified;
db.SaveChanges();
return Content("true");
}

Categories

Resources