如果提供的参数为0,则LoadBy方法返回null

时间:2016-06-21 08:24:57

标签: codefluent

我的模型包含一个带有数字属性的实体:

 <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>

LoadByLandcode方法的生成代码如下所示:

   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;
    }

如果提供的landCode参数为0,为什么CodeFluent会返回null? 我不希望发生这种情况,因为landCode 0也是数据库中的有效值。 我该如何改变这种行为?

1 个答案:

答案 0 :(得分:0)

方法的参数使用持久性默认值(默认为0)。因此,为了避免默认值检查,您必须指示参数可以为空:

<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;
}