Verify Entity does not exist before add

Posts   
 
    
Posts: 1268
Joined: 10-Mar-2006
# Posted on: 10-Mar-2006 16:34:41   

All using latest version and generating for .net 2.0. This seems like a simple task, and is if you do it in the UI layer. Here is what my UI layer might look like, accomplishing the task.



MyEntity entity = new MyEntity();
entity.Title="My title";  //assume user keyed this in somewhere...
entity.AuthorId=13; //assume user keyed this in somewhere...

MyEntity dupCheck = new MyEntity();
dupCheck.FetchUsingUCAuthorTitle(entity.AuthorID, entity.Title);

if (!dupCheck.IsNew){
    //display something to user:
    console.write(dupCheck.AuthorId.ToString()+" was already found - key:"+dupCheck.PrimaryKey);
} else
   entity.Save();


So, this should go at the data layer, prevent duplicate titles for each author. I do this:


protected override bool InsertEntity()
{
    MyEntity dupCheck = new MyEntity();
    dupCheck.FetchUsingUCAuthorTitle(this.AuthorID, this.Title);

    if (!dupCheck.IsNew){
        //here I want to set 'this' to be exactly like the one I just looked up so 
        //user can display or use the 'found' one.  However, cannot do that.  End up
        //having to refetch the same data???
        this.FetchUsingUCAuthorTitle(this.AuthorID, this.Title);
        return false;
    } else
    return base.InsertEntity();

}


As you can see I have to fetch the found data twice in order to load it into the current object? The above works, has an extra fetch/query, and results in code like this in the UI:


MyEntity entity = new MyEntity();
entity.Title="My title";  //assume user keyed this in somewhere...
entity.AuthorId=13; //assume user keyed this in somewhere...

if (!entity.Save()){
    //display something to user:
    console.write(entity.AuthorId.ToString()+" was already found - key:"+entity.PrimaryKey);
} 


Any idea how to avoide the duplicate fetch and still get this accomplished?

As a side note, I have been using your product for about 2 days and all I can say is it is unbelievable - love it. simple_smile

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 11-Mar-2006 03:01:49   

Have you tried using the FetchUniqueEntity in the ui and regardless of what it returns assigning the rest of your values and performing the save? This will perform the insert if it is not in the database and would perform an update if it did.

Posts: 1268
Joined: 10-Mar-2006
# Posted on: 12-Mar-2006 17:00:11   

I want the insert to NOT work - I do not want an update to happen at all.

What I have now works - just I have to look for the entity, if I find it, then I have to look for it again (this time in the 'this' instance) to get the values loaded.

Seems like a waste and there would be an easier/better way.

Marcus avatar
Marcus
User
Posts: 747
Joined: 23-Apr-2004
# Posted on: 13-Mar-2006 09:36:19   
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Mar-2006 14:50:42   

Another opinion (Frans opinion):

To verify if an entity exists, you have to read something from the db. I'd go for a scalar query, (GetScalar() on the MyEntityCollection) which fetches the ID with a filter for the ID. If a value is read, the entity is there, otherwise it's not (and the value returned is null, so be sure to store it in an object first). That's the cheapest way to check if an entity is available.

Another opinion (My opinion):

Since you are going to visit the database to check if an Entity Exists, I prefer to try the insert (provided I have the correct Unique Constrain defined). And if this fails I know it already existed. (catching the exception) This way I can save a query to the database when the Entity is not there.