New Posts New Posts RSS Feed: update & delete problem
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

update & delete problem

 Post Reply Post Reply
Author
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Topic: update & delete problem
    Posted: 12-Jun-2012 at 10:18am
rasel,
 
In the earlier post you were actually creating 2 entities with the same ID. (i.e. not modifying the entity the second time around)
In that case, you will get a save exception. (unless the IDs are being resolved at the datasource level)
 
If you want to modify an entity, it must exist in the datasource first. So you'd need to retrieve it, make the changes, and then save back to the datasource:
 
var testEm = new boolEntities(compositionContextName: CompositionContext.Fake.Name);
var testProducts = new Product[] {
new Product {ProductID = 0, Name = "test", Description = "tesr", Price = 12, Category = "test"} };
testEm.AddEntities(testProducts);
testEm.SaveChanges();
testEm.Clear();
var prod = testEm.Products.FirstOrDefault(p => p.ProductID == prod.ProductID);
 
prod.Description = "test";
 
SaveResult sr = testEm.SaveChanges(new Product[] {prod});
 
I suggest you visit our DevForce Resource Center. There you will have access to several articles and samples that will help better understand how DevForce works.
 
Regards,
   Silvio.
Back to Top
rasel View Drop Down
Newbie
Newbie


Joined: 11-Jun-2012
Posts: 3
Post Options Post Options   Quote rasel Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2012 at 11:26pm
I found the solution:

            // Act - update the product
            testEm.AttachEntity(prod, EntityState.Modified);
            SaveResult sr = testEm.SaveChanges();

Back to Top
rasel View Drop Down
Newbie
Newbie


Joined: 11-Jun-2012
Posts: 3
Post Options Post Options   Quote rasel Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2012 at 9:43pm
Hello sbelini,

Thank you for the fast reply. I also found the EntityAspect.Delete method after I posted my message. How about update? What is wrong with my below code?

            // Arrange
            var testEm = new boolEntities(compositionContextName: CompositionContext.Fake.Name);
            var testProducts = new Product[] {
            new Product {ProductID = 0, Name = "test", Description = "tesr", Price = 12, Category = "test"} };
            testEm.AddEntities(testProducts);
            testEm.SaveChanges();
            testEm.Clear();

            Product prod = new Product { ProductID = 0, Name = "test", Description = "test", Price = 12, Category = "test" };

            // Act - update the product
            SaveResult sr = testEm.SaveChanges(new Product[] {prod});

            // Assert
            Product pp = testEm.Products.FirstOrDefault(p => p.ProductID == prod.ProductID);
            Assert.IsTrue(pp.Description.Equals("test"));

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2012 at 12:23pm
Hi rasel,
 
To delete an entity you should use myEntity.EntityAspect.Delete().
 
You can find details about the Delete method and RemoveEntity method in our API documentation.
 
Regards,
   Silvio.
Back to Top
rasel View Drop Down
Newbie
Newbie


Joined: 11-Jun-2012
Posts: 3
Post Options Post Options   Quote rasel Quote  Post ReplyReply Direct Link To This Post Posted: 11-Jun-2012 at 11:15am
Hello,

I'm trying Devforce with a sample application. I can read from sqlite DB and add records to it but can't update or delete db. I wrote a sample unit test to simulate my delete routine. This test fails and saveresult status is OK-NoOperation. What is wrong with my code?

            // Arrange
            var testEm = new boolEntities(compositionContextName: CompositionContext.Fake.Name);
            var testProducts = new Product[] {
            new Product {ProductID = 0, Name = "test", Description = "test", Price = 12, Category = "test"} };
            testEm.AddEntities(testProducts);
            testEm.SaveChanges();
            testEm.Clear();

            Product prod = new Product { ProductID = 0, Name = "test", Description = "test", Price = 12, Category = "test" };

            // Act - delete the product
            testEm.Products.EntityManager.RemoveEntity(prod);
            SaveResult sr = testEm.SaveChanges();

            // Assert
            int ct = testEm.Products.Count();
            Assert.IsTrue(ct == 0);


Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down