Hi, I'm using SQLCE database and Entity Framework 4 as my Data Access in a Winforms project.
Create a class library to store all my Queries, Inserts, Updates, Deletes. I want to work in a disconnected environment without change tracking or contexts loaded, so i use code like this for a query (example):
public List<UserPlaylists> GetUserPlayLists()
{
using (var dbUserPlaylists = new PlaylistsEntities())
{
List<UserPlaylists> playLists = dbUserPlaylists.UserPlaylists
.Include("PlaylistSongs").ToList();
playLists.ForEach(dbUserPlaylists.Detach);
return playLists;
}
}
OK, context is disposed and Entities are detached. I use the List<> to bind to a BindindSource and this to a XtraGrid (DevExpress). Now I create a new playlist and want to update or Insert the PlayLists binded to the grid. I found from a post here this very nice extension class, created for the EF 1 but it seems it should work for EF 4 too. Tried some operations without this class too and I'm keep getting the same UpdateException with InnerException message:
{"Server-generated keys and server-generated values are not supported by SQL Server Compact."}
My code while Insert or Update:
public void SaveUserPlayLists(IList list)
{
using (var db = new PlaylistsEntities())
{
foreach (UserPlaylists playlist in list)
{
playlist.AttachGraph(db, () => new PlaylistsEntities());
}
db.SaveChanges();
}
}
I can't seem to explain what it needs exactly. I used LINQ to SQL for a project of mine before but i want to use EF 4 now for my new projects.
Any help or idea's would be really helpful. Thank you in advance.