Entity Framework deletion of non-null foreign keyed rows - entity-framework

I have a schema similar to the standard Product / OrderDetails / Order setup. I want to delete a single Product and cascade delete all OrderDetails which reference that product.
Assuming that I've thought this through from the business rules perspective, what's the most elegant way to handle that with Entity Framework 4?

First thing is first:
Is there any reason on delete cascade at the database level won't work?
If that's really not a possibility, you could try the following:
Since ObjectContext doesn't have a DeleteAll style method...you could always implement your own:
public static void DeleteAll(this ObjectContext context,
IEnumerable<Object> records)
{
foreach(Object record in records)
{
context.DeleteObject(record);
}
}
Then you could write something like (probably in a Repository):
context.DeleteAll(context.OrderDetails.Where(od => od.Product == product));
Or, to be a little cleaner:
var toDelete = context.OrderDetails.Where(od => od.Product == product);
context.DeleteAll(toDelete);

Related

how to delete items using entity framework

To say I had proxy class and Lazyloading both disabled. and I two objects with a many to many relation. When coming to delete a record, I had the codes:
public void DeleteUser(List<int> ids)
{
using (var dbContext = new AccountDbContext())
{
dbContext.Users.Include("Roles").Where(u => ids.Contains(u.ID)).ToList().ForEach(a => {
//a.Roles.Clear();
//dbContext.Users.Remove(a);
dbContext.Delete(a);
});
dbContext.SaveChanges();
}
}
It looks like dbContext.Delete(user) will delete the Roles related to the user[which in the database a many to many record is deleted ]. As I thought cascade delete is enabled. So can I say user.Roles.Clear(); is totally unnecessary here? And when should I implement a Clear method?

Delete entities in EF that are related

I have two entities. File and Binary. File contains file metadata and Binary contains file content. I want Binary instance be deleted when I remove File instance. I use the following:
public partial class MyEntities : Entities
{
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<File>().Where(e => e.State == EntityState.Deleted))
{
entry.Reference<Binary>(i => i.FileBinary).EntityEntry.State = EntityState.Deleted;
}
return base.SaveChanges();
}
}
This code does not work. I mean Binary instance is not deleted and also there is no error. Can anyone tell the reason or a better way to do that?
Thanks
You must add more details of your code, like your EF type(code first,model first,schema first) and etc. however if there is a relation between these two entities you can see this(it seems your entities are related, so if there is not relationship you can add it):
code first:
Entity Framework: Delete Object and its related entities
model first and schema first:
Cascading deletes

Updating multiple records using Entity Framework

I am using Entity Framework 5. I am looking for a better approach to update multiple records.
People are talking about EF Extensions. But I am not sure how to use it with my scenario.
This is my method signature.
internal void Update( List<Models.StockItem> stockItemsUpdate)
I need to update all the corresponding stockitem entities.
using (var context = new eCommerceEntities())
{
var items = context.StockItems.Where(si => stockItemsUpdate.Select(it => it.ID).Contains(si.ID));
}
I believe above query will return those entities.
How can I use EF extensions in this scenario?
Thanks.
In EntityFramework.Extended's BatchExtensions there is an Update extension method with this signature:
public static int Update<TEntity>(
this IQueryable<TEntity> source,
Expression<Func<TEntity, TEntity>> updateExpression)
You can use this as follows:
items.Update(item => new StockItem { Stock = 0 });
to set the stock of the selected items to 0.

Extra Column in Many to Many Relationship in Entity Framework 5.0 reviewed

I'm using the newest Entity Framework and ran into a problem with Many To Many Relationship when I want to create an extra column.
The issue is the same raised in this older post:
EF Code First Additional column in join table for ordering purposes
Is it still the problem today that one can not add an extra column without loosing the many to many relation ship (link from object A to B as A.B because the mapping becomes and entity it self) ?
What are the work a rounds ?
Look up the a of class A I need and then query for mapping table where(e=>e.A == a) to get my Bs? And when I need the extra colums i would do MappingTable.find(a,b) ?
Are there other modeling options, linq to sql that would make it easier ?
As far as I know things haven't changed with EF 5. You would need to do it as the link says to. I like to stick with EF as its easy to use, but that's just my opinion...
I had the same problem. What I did to work-around it was create another derivative DbContext specifically to handle joins. I.E.:
public class JoinContext : DbContext
{
internal JoinContext() : base("name=SampleConnectionString")
{
PreventErrorIfDatabaseSchemaChanges();
// Get the ObjectContext related to this DbContext
var objectContext = (this as IObjectContextAdapter).ObjectContext;
}
public DbSet<StudentImage> StudentImages { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentImage>()
.ToTable("StudentImages");
.HasKey(joinTable => new { joinTable.StudentId, joinTable.ImageId });
base.OnModelCreating(modelBuilder);
}
private static void PreventErrorIfDatabaseSchemaChanges()
{
Database.SetInitializer<JoinContext>(null);
}
}
I left the other application context with the Student/Image many-to-many join mapping as-is. Don't forget to specify a compounded key for the join table (refer to HasKey method above), else EF bombs on databse initialization.
After you have your special join context, use a repository to access this context and get or set the desired fields from mapped join table:
public class StudentRepository
{
public int GetImageSortOrder(int studentId, int imageId)
{
var joinContext = new JoinContext();
var joinTuple = joinContext.StudentImages.Find(studentId, imageId);
return joinTuple.SortOrder;
}
}
Hope this helps!

Entity Framework Delete All on Submit

In LINQ to SQL, I could do:
context.User_Roles.DeleteAllOnSubmit(context.User_Roles.Where(ur => ur.UserId == user.UserId));
Whats the equivalent to this for entity framework?
foreach(var entity in context.User_Roles.Where(ur => ur.UserId == user.UserId))
{
context.User_Roles.DeleteObject(entity);
}
context.SaveChanges();
Of course, you can write an extension method, which would encapsulate this.
This would be something like this:
public static void DeleteObjects<TEntity> (this ObjectSet<TEntity> set, IEnumerable<TEntity> data) where TEntity : class
{
foreach(var entity in data)
set.DeleteObject(entity);
}
Called like:
context.User_Roles.DeleteObjects(context.User_Roles.Where(ur => ur.UserId == user.UserId))
context.SaveChanges();
#Femaref has the right idea, but for a true analog to L2E's DeleteAllOnSubmit, you'll want your extension method to make a copy of the entities being deleted before enumerating so that you don't get "collection modified while enumerating" exceptions.
public static void DeleteAllObjects<TEntity>(this ObjectSet<TEntity> set, IEnumerable<TEntity> data) where TEntity : class {
foreach(var entity in data.ToList()) //data.ToList() makes a copy of data for safe enumeration
set.DeleteObject(entity);
}
foreach(var entity in context.User_Roles.Where(ur => ur.UserId == user.UserId))
{
context.User_Roles.DeleteObject(entity);
}
context.SaveChanges();
of course, this solution can work. But, it is the most inefficient solution.
This solution will generate one delete SQL command for each record (entity).
Imaging that you want to delete all data before year 2000 . there are more than 1,000,000 records in the database. If delete these objects in this way, more than 1,000,000 SQL commands will be sent to the server, it is a unnecessary big waste.
What
There is no RemoveAll equivalent in Entity Framework, so you can load entities in memory and remove them one by one using DeleteObject method.
You can use Linq : context.MyEntitie.RemoveAll(context.MyEntitie);
use EntityFramework.Extensions
1) First install EntityFramework.Extensions using NuGet
2) Here is the code similar to Linq2Sql's DeleteAllOnSubmit():
using EntityFramework.Extensions;
....
public void DeleteAllUsers(User_Role user){
context.User_Roles.Delete(ur => ur.UserId == user.UserId);
context.SaveChanges();
}
...