2017-05-19 15 views
0

私の配置の中で、petapoco pocoは空のオブジェクトを返します(すべての値はnullです)。私のUmbraco 7.5.12の中にUI-O-Matic Nugetパッケージを使用する。PetaPocoは空のオブジェクトを返します

私は、現在実行しているクエリ:

var dbContext = ApplicationContext.Current.DatabaseContext; 

    var objects = dbContext.Database.Fetch<ObjectDB>("select Id, Name, CreatedOn, PlaceId, InActive, CityMapping, CountryIsoMapping, Globalsearch from ObjectsDB"); 
    return objects.Where(n => n.PlaceId == PlaceId).FirstOrDefault(); 

TableDBのようなフィールドを持つ私のPetaPocoモデルです:デバッグのとき

[UIOMatic("ObjectsDB", "Object", "Object", FolderIcon = "icon-globe-inverted-europe-africa", ItemIcon = "icon-pin-location", RenderType = UIOMaticRenderType.List)] 
     [TableName("ObjectsDB")] 
     [PrimaryKey("Id", autoIncrement = false)] 
     [ExplicitColumns] 
     public class ObjectDB 
     { 
      [PrimaryKeyColumn(AutoIncrement = true)] 
      public int Id { get; set; } 

      [UIOMaticListViewFilter] 
      [UIOMaticListViewField(Name = "Name")] 
      [UIOMaticField(Name = "Name", Description = "Name")] 
      public string Name { get; set; } 
} 

`Debug result: con.Single<ObjectsDB>("select Name, Id from ObjectsDB where Id = 4") 

これは、オブジェクトをretruns :

{Umbraco.Extensions.Models.Custom.ObjectsModel.ObjectsDB} _createdOn: {1/1/0001 12:00:00 AM} 
CityMapping: null 
CountryIsoMapping: null 
CreatedOn: {5/19/2017 4:22:16 PM} 
Globalsearch: false 
Id: 0 
InActive: false 
InCache: false 
Name: null 
Object: null 
PlaceId: null ` 

データの挿入は、動作している同じdbContextを使用しています。 私はここで何が欠けていますか?

答えて

0

は、問題を修正しました。期待どおりにすべてが機能しません。他の装飾も働いています。だから@Nurhak Kayaは部分的に正しい。その属性を削除してテーブルを削除し、テーブルを再構築/生成します。

+0

私は助けてくれてうれしいです。ハッピーコーディング。 –

1

私はPetapocoを様々なUmbracoプロジェクトで使用しています。私のアプローチはあなたのアプローチとは少し異なります。私はここでそれを共有しています。

これは私が使用している:(http://nuget.org/List/Packages/PetaPocoをnugetパッケージです)

私のサンプル以下のコードまたはin my blogを参照してください:私のクラスの上に属性[ExplicitColumns]を削除

[PetaPoco.TableName("fsCarts")] 
[PetaPoco.PrimaryKey("RecordID")] 
public class Cart 
{ 
    [Key] 
    public int RecordId { get; set; } 
    public string CartId { get; set; } 
    public Guid ProductId { get; set; } 
    public int Count { get; set; } 
    public DateTime DateCreated { get; set; } 

} 


UmbracoDatabase con = ApplicationContext.Current.DatabaseContext.Database; 


public void AddToCart(Product product) 
{ 
    try 
    { 
     var cartItem = con.FirstOrDefault<Cart>("SELECT * FROM fsCarts WHERE [email protected] AND [email protected]", ShoppingCardId, product.ProductId); 

     if (cartItem == null) 
     { 
      cartItem = new Cart 
      { 
       ProductId = product.ProductId, 
       CartId = ShoppingCardId, 
       Count = 1, 
       DateCreated = DateTime.Now 
      }; 
      con.Insert("fsCarts", "RecordID", cartItem); 
     } 
     else 
     { 
       cartItem.Count++; 
       con.Update("fsCarts", "RecordID", cartItem); 
      } 
    } 
    catch (Exception ex) 
    { 
     Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart AddToCart: " + ex.ToString()))); 
    } 
} 

//////////////////// 

public int RemoveFromCart(int id) 
{ 
    int itemCount = 0; 
    try 
    { 
     var cartItem = con.FirstOrDefault<Cart>("SELECT * FROM fsCarts WHERE [email protected] AND [email protected]", ShoppingCardId, id); 

     if (cartItem != null) 
     { 
      if (cartItem.Count > 1) 
      { 
       cartItem.Count--; 
       itemCount = cartItem.Count; 
       con.Update("fsCarts", "RecordID", cartItem); 
      } 
      else 
      { 
       con.Delete("fsCarts", "RecordID", cartItem); 
       } 
     } 

    } 
    catch (Exception ex) 
    { 
     Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart RemoveFromCart: " + ex.ToString()))); 
     } 

    return itemCount; 
} 


//////////////////// 

public List<Cart> GetCartItems() 
{ 
    List<Cart> cartItemList = new List<Cart>(); 
    try 
    { 
     cartItemList = con.Query<Cart>("SELECT * FROM fsCarts WHERE [email protected]", ShoppingCardId).ToList(); 

    } 
    catch (Exception ex) 
    { 
     Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart GetCartItems: " + ex.ToString()))); 
    } 
    return cartItemList; 
} 

//////////////////// 

public decimal GetTotal() 
{ 
    decimal? total = null; 
    try 
    { 
     total = con.ExecuteScalar<decimal>("SELECT SUM(ISNULL(p.Price,0)*c.Count) FROM fsCarts c INNER JOIN fsProducts p ON c.ProductID=p.ProductID WHERE [email protected]", ShoppingCardId); 

    } 
    catch (Exception ex) 
    { 
     Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Shopping Cart GetTotal: " + ex.ToString()))); 
     } 
    return total ?? decimal.Zero; 
} 
+0

UmbracoDatabase con = ApplicationContext.Current.DatabaseContext.Database;は、私の質問のコードと同じです(同じです)。私はデータベースから自分のレコードを受け取ったが、フィールド内のnull値を返す。 – RunnicFusion

+0

他にご提案ありましたか? – RunnicFusion

+1

あなたのテーブルオブジェクトには異なる属性があるのと同じように接続を作成するのは間違いですが、属性の少ないテーブルクラスを作成し、dbから項目を選択する方法を試してみることをお勧めします。私は自分のコードが動作することを保証することができますので、あなたのコードに簡単な変更を加えて、動作するかどうか確認してください。選択が機能していることを確認した後、他の属性を追加することができます。詳細はこちらをご覧ください。 http://www.toptensoftware.com/petapoco/ –

関連する問題