1

My model contains an entity with a numeric property:

 <cf:entity name="Land">
     <cf:property name="Id" key="true" />
     <cf:property name="Landcode" typeName="ushort" nullable="false" usePersistenceDefaultValue="false" />

      <cf:method name="LoadByLandcode"
          body="LOADONE(ushort landCode) WHERE Landcode = @landcode">
      </cf:method>
 </cf:entity>

The generated code for the LoadByLandcode method looks like this:

   public static Land LoadByLandcode(ushort landCode)
    {
        if ((landCode == CodeFluentPersistence.DefaultUInt16Value))
        {
            return null;
        }
        Land land = new Land();
        CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(BusinessLayerStoreName).Persistence;
        persistence.CreateStoredProcedureCommand(null, "Land", "LoadByLandcode");
        persistence.AddParameter("@landCode", landCode);
        System.Data.IDataReader reader = null;
        try
        {
            reader = persistence.ExecuteReader();
            if ((reader.Read() == true))
            {
                land.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
                land.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Unchanged;
                return land;
            }
        }
        finally
        {
            if ((reader != null))
            {
                reader.Dispose();
            }
            persistence.CompleteCommand();
        }
        return null;
    }

Why does CodeFluent return null if the provided landCode parameter is 0? I do not want this to happen, because landCode 0 is also a valid value in the database. How can I change this behaviour?

BremHi
  • 21
  • 1

1 Answers1

0

The parameter of the method uses the persistence default value (0 by default). So to avoid the default value check, you have to indicate the parameter is nullable:

<cf:method name="LoadByLandcode"
    body="LOADONE(Landcode?) WHERE Landcode = @Landcode">
</cf:method>


public static Land LoadByLandcode(ushort landcode)
{
    Land land = new Land();
    CodeFluent.Runtime.CodeFluentPersistence persistence = CodeFluentContext.Get(Constants.StoreName).Persistence;
    persistence.CreateStoredProcedureCommand(null, "Land", "LoadByLandcode");
    persistence.AddRawParameter("@Landcode", landcode);
    System.Data.IDataReader reader = null;
    try
    {
        reader = persistence.ExecuteReader();
        if ((reader.Read() == true))
        {
            land.ReadRecord(reader, CodeFluent.Runtime.CodeFluentReloadOptions.Default);
            land.EntityState = CodeFluent.Runtime.CodeFluentEntityState.Unchanged;
            return land;
        }
    }
    finally
    {
        if ((reader != null))
        {
            reader.Dispose();
        }
        persistence.CompleteCommand();
    }
    return null;
}
meziantou
  • 20,589
  • 7
  • 64
  • 83