New entities added status

Posts   
 
    
he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 23-Oct-2009 11:32:25   

If one changes an entity inside a collection one can test to see if the collection contains dirty records using the ContainsDirtyContents method.

If one has used the addnew method on an entity collection, how does one determine that there are new records waiting to be inserted?

There isn't a ContainsNewContent method. I did a quick test and did an addnew on an entity collection but it did not change it's dirty status.

How do you find out that there are pending inserts.

This is important in that before a user closes the application or does something else you might want to prompt them if they want to save any changes made? Updates are fine. Inserts are another question.

For AddNew() I have found a reference to a 'flag'. Howe do I gain access to this flag?

Deletes arn't a problem as one can use a RemovedEntitiesTracker and simply count the number of entities present in the deleted entities collection.

See this test code used for testing purposes only

    public bool ChangesInEffect()
    {

        recentMessages.AddNew();
        bool x = (deletedMessages.Count > 0);
        bool y = recentMessages.ContainsDirtyContents; // not true after addnew()

        return x || y;

    }
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Oct-2009 11:36:12   

If one has used the addnew method on an entity collection, how does one determine that there are new records waiting to be inserted?

AddNew() should only be used by databinding code, please use the Add() method.

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 26-Oct-2009 01:09:39   

Walaa wrote:

If one has used the addnew method on an entity collection, how does one determine that there are new records waiting to be inserted?

AddNew() should only be used by databinding code, please use the Add() method.

Thanks Add() or AddNew (). You are missing the point of my query.

How does one determine if there have been new entities added to an entity collection?

The code snippit is something I would not use in production. It was a simple mechanism to add a new entity to a collection. The mostly likey way that new entities would be added to a collection would be by the collection being bound to a datagrid etc. Ok so I should use Add() not AddNew(), but my original query remains

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 26-Oct-2009 02:39:58   

I was after something like


        public bool EntityCollectionChanged()
        {
            
            bool DeletedEntities = (deletedMessages.Count > 0);
            bool DirtyEntities = false;
            bool NewEntities = false;
            foreach (MotdEntity motd in recentMessages)
            {
                if(motd.IsNew)
                {
                    NewEntities = true;
                    break;
                }
                if (motd.IsDirty)
                {
                    DirtyEntities = true;
                    break;
                }
            }

            return DeletedEntities || NewEntities || DirtyEntities;
        }

I guess I have answered my own post now. stuck_out_tongue_winking_eye But is this the best approach? confused

I was interested to see of there was something like a ContainsNewContents as the new entity equivalent of the ContainsDirtyContents method for dirty entities.wink

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Oct-2009 08:07:45   

Please use the **ContainsDirtyContents **property. The following test succeeds.

            var col = new EntityCollection<ProductsEntity>();
            Debug.Assert(!col.ContainsDirtyContents);

            col.Add(new ProductsEntity());
            Debug.Assert(!col.ContainsDirtyContents);

            var product = new ProductsEntity();
            product.ProductName = "LCD Screen";
            col.Add(product);
            Debug.Assert(col.ContainsDirtyContents);

Note only when a new entity which has a dirty field is added the ContainsDirtyContents is true.

The following test succeeds too:

            var category = new CategoriesEntity(1);

            var col = category.Products;
            Debug.Assert(!col.ContainsDirtyContents);

            col.Add(new ProductsEntity());

            Debug.Assert(col.ContainsDirtyContents); // ContainsDirtyContents is true here

In the second code block, when you ad a new entity to the products collection of the specified category entity, it get dirty too, since the FK of the product is set to the PK of the category.

he00273
User
Posts: 133
Joined: 02-Oct-2008
# Posted on: 26-Oct-2009 08:31:35   

Ok Thanks

I will give that a test