Print Page | Close Window

update & delete problem

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3491
Printed Date: 28-Mar-2025 at 5:22pm


Topic: update & delete problem
Posted By: rasel
Subject: update & delete problem
Date 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);





Replies:
Posted By: sbelini
Date 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 http://drc.ideablade.com/ApiDocumentation/IdeaBlade.EntityModel~IdeaBlade.EntityModel.Extensions.EntityAspectExtensions~Delete.html - Delete method and http://drc.ideablade.com/ApiDocumentation/IdeaBlade.EntityModel~IdeaBlade.EntityModel.EntityManager~RemoveEntity.html - RemoveEntity method in our http://drc.ideablade.com/ApiDocumentation/webframe.html - API documentation .
 
Regards,
   Silvio.


Posted By: rasel
Date 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"));



Posted By: rasel
Date Posted: 11-Jun-2012 at 11:26pm
I found the solution:

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



Posted By: sbelini
Date 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 http://drc.ideablade.com/xwiki/bin/view/Documentation/ - DevForce Resource Center . There you will have access to several articles and samples that will help better understand how DevForce works.
 
Regards,
   Silvio.



Print Page | Close Window