1

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

 <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 也是数据库中的有效值。我怎样才能改变这种行为?

4

1 回答 1

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;
}
于 2016-06-21T09:04:13.663 回答