Bartosz
Bartosz

Reputation: 4602

Cant delete record from SQL Server CE

I have an ASP.NET MVC application. I use EF 5.0 to persist data in the database of a SQL Server CE type. I have installed SqlServerCe.Enitity NuGet package. I can successfully process Create, Read and Update operations on the database records using Linq and my model object. For some reasons however, I cannot delete any records from the database. Objects are being erased on the model level but changes are not being persisted in the database. What might be causing that?

This my approach to deleting records:

var files = context.Files.ToList();
files.RemoveAll(x => x.Name == "test");
context.SaveChanges();

And this is my connection string:

<add name="DefaultConnection" connectionString="Data Source=C:\SLG PROJECTS\BACKUPS CHECK SYSTEM\Data\aspnet.sdf" providerName="System.Data.SqlServerCe.4.0" />

Upvotes: 1

Views: 235

Answers (1)

dvjanm
dvjanm

Reputation: 2381

Try this:

context.Files.Where(x => x.Name == "test").ToList()
.ForEach( f => context.Files.Remove(f));

context.SaveChanges();

The RemoveAll may not compiled to SQL and not executed in SQL Server.

Upvotes: 1

Related Questions