Identifying Default value field in a IEntityField2

Posts   
 
    
Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 21-Aug-2012 09:55:38   

Hi,

I have following function which will check for Not Null and inform user that some of the Not Null fields are not yet completely filled in. I ran into this big issue now that for default value columns, I couldnt find a way to avoid prompting user.

My code is as follows. Is there a way to find that a field has default value set in db?

   private bool AreAllNotNullValuesAvailable(IEntity2 obj)
        {
            bool isNullNotMet = false;
            foreach (IEntityField2 f1 in obj.Fields)
            {
                if (f1.IsNullable == false && (f1.CurrentValue == null || (f1.CurrentValue != null && f1.CurrentValue.Equals(string.Empty)))
                    && (f1.Name.ToUpper() != "CREATEDATETIME" &&
                    f1.Name.ToUpper() != "CREATEUSERID" && f1.Name.ToUpper() != "INACTIVEIND" && f1.Name.ToUpper() != "SYNCIND" && f1.Name.ToUpper() != "LOCKEDIND" 
                    && f1.DataType != typeof(Guid))
                    )
                {
                    isNullNotMet = false;
                    break;
                }
                isNullNotMet = true;
            }
            return isNullNotMet;
        }
Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 21-Aug-2012 13:24:54   

Hi,

This is a critical functionality in my application. Can you please help me on this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Aug-2012 19:40:26   
Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 21-Aug-2012 20:30:31   

Hi Walaa,

Can you please give me a code snippet for the given example? It would be of great help.

Regards,

Praveen.V.Nair

Praveen
User
Posts: 31
Joined: 09-Jan-2012
# Posted on: 22-Aug-2012 09:38:29   

Hi,

I could derive field info from the example attached. Can you help me to find a given field has default value set in db.. It's really weird that such important flag is not available part of field info.

Regards,

Praveen.V.Nair

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 22-Aug-2012 10:32:16   

Hi Praveen,

That flag is not passed to the field's persistence info. Default values are managed by DB, or by your code, but the generated code doesn't make assumptions on DB defaults. One reason is that the default value on DB could be an expression like "GetDate()". Also, this is something that is not supported by all DBs.

However you can write a template that generates this info, as mentioned in the thread posted by Walaa. I have written the template for you, is very simple but it shows the concept.

The template generates a helper class like:

namespace  <yourRootNamespace>
{
    public static class DefaultValuesFinder
    {
        public static bool FieldHasDefaultValueOnDb(IEntityFieldCore fieldToSearch)
        {
            var fieldsThatHasDefaultValue = new List<IEntityFieldCore>();
            
            fieldsThatHasDefaultValue.Add(OrderFields.Freight);
            fieldsThatHasDefaultValue.Add(OrderDetailFields.Discount);
            fieldsThatHasDefaultValue.Add(OrderDetailFields.Quantity);
            fieldsThatHasDefaultValue.Add(OrderDetailFields.UnitPrice);
            fieldsThatHasDefaultValue.Add(ProductFields.Discontinued);
            fieldsThatHasDefaultValue.Add(ProductFields.ReorderLevel);
            fieldsThatHasDefaultValue.Add(ProductFields.UnitPrice);
            fieldsThatHasDefaultValue.Add(ProductFields.UnitsInStock);
            fieldsThatHasDefaultValue.Add(ProductFields.UnitsOnOrder);
            
            return fieldsThatHasDefaultValue.Any(f => f.ActualContainingObjectName == fieldToSearch.ActualContainingObjectName
                && f.Name == fieldToSearch.Name);
        }
    }
}

Then you can use it like this:

var freightHasDefaultValue = DefaultValuesFinder.FieldHasDefaultValueOnDb(OrderFields.Freight);

var orderIdHasDefaultValue = DefaultValuesFinder.FieldHasDefaultValueOnDb(OrderFields.OrderId);

Attached is a zip with the relevant files. To use them: 1. Unzip it in a known folder. 2. At your project settings, set the "Additional Tasks folder" and "Additional Templates folder" to the corresponding folders. 3. When generate the code, add the task. SD.Tasks.DefaultValuesFileGenerator to your preset. You can save this custom preset if you want. 4. If you placed the task at the top, the file will be added to your DBGeneric project, if not, just add that file to your DBGeneric project.

Make the changes you want to the template. Let me know if that helped to solve your problem.

Attachments
Filename File size Added on Approval
DefaultValuesTemplates.zip 2,813 22-Aug-2012 10:32.26 Approved
David Elizondo | LLBLGen Support Team