2016-06-21 224 views
1

我的模型包含一個實體與數字財產:LoadBy方法返回null 0

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

爲什麼CodeFluent如果提供的landCode參數爲0,則返回null? 我不希望發生這種情況,因爲landCode 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; 
}