PetaPoco返回空对象

时间:2017-05-19 13:07:59

标签: umbraco7 petapoco

在我的applocation中,petapoco poco返回一个空对象(所有值都为null)。在我的Umbraco 7.5.12中使用UI-O-Matic Nuget包。

我目前正在运行的查询:

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

这会重新对象:

    {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,它正在工作。 我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

我在各种Umbraco项目中使用了Petapoco,我的方法与您的方法略有不同。我在这里分享,希望它对你有所帮助。

这是我使用的nuget包:(http://nuget.org/List/Packages/PetaPoco

请参阅下面的示例代码或in my blog

[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 CartID=@0 AND ProductID=@1", 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 CartID=@0 AND RecordId=@1", 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 CartID=@0", 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 c.CartID=@0", ShoppingCardId);

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

答案 1 :(得分:0)

删除我班级上方的[ExplicitColumns]属性修复了问题。没有一切按预期工作。其他装饰也在工作。所以@Nurhak Kaya部分正确。删除该属性后删除表并重建/生成表。