I look for a way of executing RemoveRange() with 1-query-approach. With Remove() method it works like this:
public void Delete()
{
Record record = new Record() {
id = 1,
value = 5
};
using(SomeContext ctx = new SomeContext()) {
ctx.Records.Entry(record).State = EntityState.Deleted;
ctx.SaveChanges();
}
}
But the same approach doesn't work for RemoveRange(). EF documentation says that the method sets each entity to EntityState.Deleted. If it would be so as I understand it - this would work:
public void DeleteAll()
{
List<Record> records = new List<Record>() {
new Record() { id = 1, value = 5 },
new Record() { id = 2, value = 10 }
};
using(SomeContext ctx = new SomeContext()) {
ctx.Records.RemoveRange(records);
ctx.SaveChanges();
}
}
because this works:
public void DeleteAll()
{
List<Record> records = new List<Record>() {
new Record() { id = 1, value = 5 },
new Record() { id = 2, value = 10 }
};
using(SomeContext ctx = new SomeContext()) {
foreach(var item in records)
{
ctx.Entry(item).State = EntityState.Deleted;
}
ctx.SaveChanges();
}
}
But it doesn't and throws the exception
The object cannot be deleted because it was not found in the ObjectStateManager.
Can I use this method without retrieving all of them from database via separate query?
When you use ctx.Entry(item) it adds this item to the context tracking and then you can edit it. With RemoveRange it does not implicitly add to the data context first and that is why you get the exception.
Try using AttachRange before RemoveRange
Edit:
Alternative way to do it. This is a bit how RemoveRange does it behind the scenes. It first disables AutoDetectChanges, removes what it should and then calls DetectChanges. Could wrap this in an extension method to make it a one liner again.
public void DeleteAll()
{
List<Record> records = new List<Record>() {
new Record() { id = 1, value = 5 },
new Record() { id = 2, value = 10 }
};
using(SomeContext ctx = new SomeContext()) {
ctx.Configuration.AutoDetectChangesEnabled = false;
foreach(var item in records)
{
ctx.Entry(item).State = EntityState.Deleted;
}
ctx.ChangeTracker.DetectChanges();
ctx.SaveChanges();
}
}
As I found your entity-collection is detached(as you creating it there) so objectContext not tracking it. You may need to attach it. you can do this as below:
//Find all records in database with an id that is in your record collection
var recordsToBeDeleted = ctx.Records.Where(dbr => records.Any(r => r.id == dbr.id));
ctx.Records.RemoveRange(recordsToBeDeleted);
context.SaveChanges();
Solution taken from this thread
Related
while filling a combobx, I need to convert a Linq-result to a viewmodel.
Actually, I query the records and then I fill a list of the viewmodel in a loop, but that seems to be a bit strange:
public static IEnumerable<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
List<ComboBoxActivities> list = new List<ComboBoxActivities>();
foreach(var res in result)
{
ComboBoxActivities listItem = new ComboBoxActivities()
{
ActivityId= res.ActivityId,
ActivityName= res.ActivityName,
Available= res.Available
};
list.Add(listItem);
}
return list;
}
}
Is this really the right way?
I also tried:
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
But then my razorview crashes with the message that direct binding to a quers (DbSet, DbQuery...) is not supported.
You can convert the IEnumerable<T> to a List<T> by using ToList()
public static List<ComboBoxActivities> GetActivitySelectList()
{
using(ApplicationDbContext db = new ApplicationDbContext())
{
var result = from activity in db.Activities
where activity.Available
select new ComboBoxActivities()
{
ActivityId = activity.Id,
ActivityName = activity.ActivityName,
Available = activity.Available
};
return result.ToList();
}
}
As for loading a ComboBox from a table query, ComboBox has a DataSource property which you can assign the List to.
I'm somewhat new to EF 6.0 so I'm pretty sure I'm doing something wrong here.
there are two questions related to the problem
what am I doing wrong here
what's the best practice to achieve this
I'm using a code first model, and used the edmx designer to design the model and relationships, the system needs to pull information periodically from a webservice and save it to a local database (SQL Lite) in a desktop application
so I get an order list from the API, when I populate and try to save Ticket, I get a duplicate key exception when trying to insert TicketSeatType -
how do I insert the ticket to dbContext, so that It doesn't try and re-insert insert TicketSeatType and TicketPriceType, I have tried setting the child object states to unchanged but it seems to be inserting
secondly, what would be the best practice to achieve this using EF ? it just looks very inefficient loading each object into memory and comparing if it exists or not
since I need to update the listing periodically, I have to check against each object in the database if it exists, then update, else insert
code:
//read session from db
if (logger.IsDebugEnabled) logger.Debug("reading session from db");
dbSession = dbContext.SessionSet.Where(x => x.Id == sessionId).FirstOrDefault();
//populate orders
List<Order> orders = (from e in ordersList
select new Order {
Id = e.OrderId,
CallCentreNotes = e.CallCentreNotes,
DoorEntryCount = e.DoorEntryCount,
DoorEntryTime = e.DoorEntryTime,
OrderDate = e.OrderDate,
SpecialInstructions = e.SpecialInstructions,
TotalValue = e.TotalValue,
//populate parent refernece
Session = dbSession
}).ToList();
//check and save order
foreach (var o in orders) {
dbOrder = dbContext.OrderSet.Where(x => x.Id == o.Id).FirstOrDefault();
if (dbOrder != null) {
dbContext.Entry(dbOrder).CurrentValues.SetValues(o);
dbContext.Entry(dbOrder).State = EntityState.Modified;
}
else {
dbContext.OrderSet.Add(o);
dbContext.Entry(o.Session).State = EntityState.Unchanged;
}
}
dbContext.SaveChanges();
//check and add ticket seat type
foreach (var o in ordersList) {
foreach (var t in o.Tickets) {
var ticketSeatType = new TicketSeatType {
Id = t.TicketSeatType.TicketSeatTypeId,
Description = t.TicketSeatType.Description
};
dbTicketSeatType = dbContext.TicketSeatTypeSet.Where(x => x.Id == ticketSeatType.Id).FirstOrDefault();
if (dbTicketSeatType != null) {
dbContext.Entry(dbTicketSeatType).CurrentValues.SetValues(ticketSeatType);
dbContext.Entry(dbTicketSeatType).State = EntityState.Modified;
}
else {
if (!dbContext.ChangeTracker.Entries<TicketSeatType>().Any(x => x.Entity.Id == ticketSeatType.Id)) {
dbContext.TicketSeatTypeSet.Add(ticketSeatType);
}
}
}
}
dbContext.SaveChanges();
//check and add ticket price type
foreach (var o in ordersList) {
foreach (var t in o.Tickets) {
var ticketPriceType = new TicketPriceType {
Id = t.TicketPriceType.TicketPriceTypeId,
SeatCount = t.TicketPriceType.SeatCount,
Description = t.TicketPriceType.Description
};
dbTicketPriceType = dbContext.TicketPriceTypeSet.Where(x => x.Id == ticketPriceType.Id).FirstOrDefault();
if (dbTicketPriceType != null) {
dbContext.Entry(dbTicketPriceType).CurrentValues.SetValues(ticketPriceType);
dbContext.Entry(dbTicketPriceType).State = EntityState.Modified;
}
else {
if (!dbContext.ChangeTracker.Entries<TicketPriceType>().Any(x => x.Entity.Id == ticketPriceType.Id)) {
dbContext.TicketPriceTypeSet.Add(ticketPriceType);
}
}
}
}
dbContext.SaveChanges();
//check and add tickets
foreach (var o in ordersList) {
dbOrder = dbContext.OrderSet.Where(x => x.Id == o.OrderId).FirstOrDefault();
foreach (var t in o.Tickets) {
var ticket = new Ticket {
Id = t.TicketId,
Quantity = t.Quantity,
TicketPrice = t.TicketPrice,
TicketPriceType = new TicketPriceType {
Id = t.TicketPriceType.TicketPriceTypeId,
Description = t.TicketPriceType.Description,
SeatCount = t.TicketPriceType.SeatCount,
},
TicketSeatType = new TicketSeatType {
Id = t.TicketSeatType.TicketSeatTypeId,
Description = t.TicketSeatType.Description
},
Order = dbOrder
};
//check from db
dbTicket = dbContext.TicketSet.Where(x => x.Id == t.TicketId).FirstOrDefault();
dbTicketSeatType = dbContext.TicketSeatTypeSet.Where(x => x.Id == t.TicketSeatType.TicketSeatTypeId).FirstOrDefault();
dbTicketPriceType = dbContext.TicketPriceTypeSet.Where(x => x.Id == t.TicketPriceType.TicketPriceTypeId).FirstOrDefault();
if (dbTicket != null) {
dbContext.Entry(dbTicket).CurrentValues.SetValues(t);
dbContext.Entry(dbTicket).State = EntityState.Modified;
dbContext.Entry(dbTicket.Order).State = EntityState.Unchanged;
dbContext.Entry(dbTicketSeatType).State = EntityState.Unchanged;
dbContext.Entry(dbTicketPriceType).State = EntityState.Unchanged;
}
else {
dbContext.TicketSet.Add(ticket);
dbContext.Entry(ticket.Order).State = EntityState.Unchanged;
dbContext.Entry(ticket.TicketSeatType).State = EntityState.Unchanged;
dbContext.Entry(ticket.TicketPriceType).State = EntityState.Unchanged;
}
}
}
dbContext.SaveChanges();
UPDATE:
Found the answer, it has to do with how EF tracks references to objects, in the above code, I was creating new entity types from the list for TicketPriceType and TicketSeatType:
foreach (var o in ordersList) {
dbOrder = dbContext.OrderSet.Where(x => x.Id == o.OrderId).FirstOrDefault();
foreach (var t in o.Tickets) {
var ticket = new Ticket {
Id = t.TicketId,
Quantity = t.Quantity,
TicketPrice = t.TicketPrice,
TicketPriceType = new TicketPriceType {
Id = t.TicketPriceType.TicketPriceTypeId,
Description = t.TicketPriceType.Description,
SeatCount = t.TicketPriceType.SeatCount,
},
TicketSeatType = new TicketSeatType {
Id = t.TicketSeatType.TicketSeatTypeId,
Description = t.TicketSeatType.Description
},
Order = dbOrder
};
....
in this case the EF wouldn't know which objects they were and try to insert them.
the solution is to read the entities from database and allocate those, so it's referencing the same entities and doesn't add new ones
foreach (var t in o.Tickets) {
//check from db
dbTicket = dbContext.TicketSet.Where(x => x.Id == t.TicketId).FirstOrDefault();
dbTicketSeatType = dbContext.TicketSeatTypeSet.Where(x => x.Id == t.TicketSeatType.TicketSeatTypeId).FirstOrDefault();
dbTicketPriceType = dbContext.TicketPriceTypeSet.Where(x => x.Id == t.TicketPriceType.TicketPriceTypeId).FirstOrDefault();
var ticket = new Ticket {
Id = t.TicketId,
Quantity = t.Quantity,
TicketPrice = t.TicketPrice,
TicketPriceType = dbTicketPriceType,
TicketSeatType = dbTicketSeatType,
Order = dbOrder
};
...}
Don't you think that you are trying to write very similar codes for defining the state of each entity?
We can handle all of these operations with a single command.
You can easily achieve this with the newly released EntityGraphOperations for Entity Framework Code First. I am the author of this product. And I have published it in the github, code-project (includes a step-by-step demonstration and a sample project is ready for downloading) and nuget. With the help of InsertOrUpdateGraph method, it will automatically set your entities as Added or Modified. And with the help of DeleteMissingEntities method, you can delete those entities which exists in the database, but not in the current collection.
// This will set the state of the main entity and all of it's navigational
// properties as `Added` or `Modified`.
context.InsertOrUpdateGraph(ticket);
By the way, I feel the need to mention that this wouldn't be the most efficient way of course. The general idea is to get the desired entity from the database and define the state of the entity. It would be as efficient as possible.
I am inserting objects at the same time using Entity framework like below.
context = new MyContext();
foreach (var x in lstX)
{
var abc = new abc{ name= x.abcName };
context.abcs.Add(abc);
var xyz = new xyz{ name = x.xyzName };
context.xyzs.Add(xyz);
}
context.SaveChanges();
Is it possible get the identity of all these inserted objects?
When you call SaveChanges, the Identity field is populated on the original entity. So to obtain this id, simply store a reference to the identity and access it after SaveChanges:
context = new MyContext();
List<abc> addedABCs = new List<abc>();
List<xyz> addedXYZs = new List<xyz>();
foreach (var x in lstX)
{
var abc = new abc{ name= x.abcName };
addedABCs.Add(abc);
context.abcs.Add(abc);
var xyz = new xyz{ name = x.xyzName };
addedXYZs.Add(xyz);
context.xyzs.Add(xyz);
}
context.SaveChanges();
foreach (var abc in addedABCs)
{
Console.WriteLine("Added item with ID {0}", abc.Id);
}
I have this code:
List<MyObjectOne> myListOne = new List<MyObjectOne>(){new MyObjectOne { ID = 1, field2 = 2}};
List<MyObjectTwo> myListTwo = new List<MyObjectTwo>(){new MyObjectTwo { ID = 4, field6 = "string"}};
bool hasSomething = false;
var result = new[] { new {ID = 0 }}.ToList();
if (hasSomething)
{
// Use list one.
result = myListOne.Select(x => new { ID = x.ID});
}
else
{
// Use list two.
result = myListTwo.Select(x => new { ID = x.ID });
}
foreach (var item in result)
{
// Some logic to manipulate item.ID.
item.ID;
}
What I trying to do it's to use the same anonymous type to select a list of IDs from two different lists. So I use the Select(x => new { ID = x.ID }) in order to create the anonymous type for each table in order to have only one for loop.
The error raised is "Cannot implicitly convert type IEnumerable to List"
¿any idea?
Assuming ID in MyObjectOne and MyObjectTwo are both int's, your code will work if you replace ToList with AsEnumerable:
var result = new[] { new { ID = 0 } }.AsEnumerable();
If the ID properties are some other type (e.g. long's), you need to specify that when creating the anonymous type here:
var result = new[] { new { ID = 0L } }.AsEnumerable();
Or like this:
var result = new[] { new { ID = (long)0 } }.AsEnumerable();
However, this kind of code is kind of confusing, and I wouldn't recommend it for a production environment. Here's an alternative solution that avoids creating a 'dummy' object just for implicit anonymous typing:
var result = hasSomething
? myListOne.Select(x => new { ID = x.ID })
: myListTwo.Select(x => new { ID = x.ID });
I am inserting child records that are associated with an existing parent record. How would I refresh the parent record to show all, including the newly inserted, child records?
context.Refresh(RefreshMode.OverwriteCurrentValues, entity) isn't working.
A more complete example of my attempt:
Location newLocation = Json.deserialize<Location>(json);
if (newLocation != null) {
IEnumerable<string> zipCodes = Regex.Split(newLocation.zipCodes, #"[\s,;]+");
// this verifies the new zipcodes against a table of all US zipcodes and returns matches
var newLocationZipCodes = _zipCodeRepository.match(zipCodes).Select(item => new LocationZipCode { idLocation = newLocation.id, state = item.state, zipcode = item.zipcode });
// get the parent entity
var domainLocation = _unitOfWork.locationRepository.getFirst(l => l.id == newLocation.id);
// insert child entities
if (newLocationZipCodes.Any()) {
_unitOfWork.locationZipCodeRepository.insertAll(newLocationZipCodes);
_unitOfWork.saveChanges(ConflictMode.ContinueOnConflict);
}
// this isn't working
_unitOfWork.refresh(RefreshMode.OverwriteCurrentValues, domainLocation);
return domainLocation;
}
Here is a basic representation of the LocationZipCode class created by linq-to-sql:
public class LocationZipCode {
int idLocation;
string zipcode;
string state
EntityRef<Location> location;
}
And here is my refresh method in my UnitOfWork:
public void refresh(RefreshMode refreshMode, object entity) {
_context.Refresh(refreshMode, entity);
}
Instead of refreshing the context I changed the way I was inserting my child records into the database. So instead of...
_unitOfWork.locationZipCodeRepository.insertAll(newLocationZipCodes);
I'm doing this...
domainLocation.LocationZipCodes.AddRange(newLocationZipCodes);
So the updated code looks like so...
Location newLocation = Json.deserialize<Location>(json);
if (newLocation != null) {
IEnumerable<string> zipCodes = Regex.Split(newLocation.zipCodes, #"[\s,;]+");
var newLocationZipCodes = _zipCodeRepository.match(zipCodes).Select(item => new LocationZipCode { idLocation = newLocation.id, state = item.state, zipcode = item.zipcode });
var domainLocation = _unitOfWork..locationRepository.getFirst(l => l.id == newLocation.id);
if (newLocationZipCodes.Any()) {
domainLocation.LocationZipCodes.AddRange(newLocationZipCodes);
_unitOfWork.saveChanges(ConflictMode.ContinueOnConflict);
}
return new Mapper<DomainLocation, Location>(new LocationMapTemplate()).map(domainLocation);
}