0

Inside my applocation, the petapoco poco returns an empty object (all values are null). Using the UI-O-Matic Nuget package inside my Umbraco 7.5.12.

The query i'm currently running:

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 is my PetaPoco model with the fields like:

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

When debuging:

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

This retruns the object:

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

Inserting data is working with the same dbContext, that's working. What am I missing here?

RunnicFusion
  • 193
  • 1
  • 3
  • 12

2 Answers2

1

I have used Petapoco in various Umbraco project and my approach is a bit different than your approach. I am sharing it here, hope it helps you.

This is the nuget package that I have used:(http://nuget.org/List/Packages/PetaPoco)

Please see my sample code below or 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;
}
Nurhak Kaya
  • 1,470
  • 1
  • 18
  • 20
  • The UmbracoDatabase con = ApplicationContext.Current.DatabaseContext.Database;returns (and is the same) as in my question code. I received my record from the database, but whit null values inside the Fields. – RunnicFusion May 19 '17 at 14:18
  • Did you have a other suggestion? – RunnicFusion May 21 '17 at 11:16
  • 1
    That is correct that you create your connection just like I do but your table object has different attributes, I would recommend you to create your table class with less attributes and try the way I select the items from the db. I can assure you that my code works, so please make couple of simple changes in your code and see if it works. You can add other attributes after making sure select is working. For details please have a look at this. http://www.toptensoftware.com/petapoco/ – Nurhak Kaya May 21 '17 at 18:21
0

Removing the attribute [ExplicitColumns] above my class fixed the problem. No everything works as expected. Also the other decorations are working. So @Nurhak Kaya was partially right. After removing that attribute deleting the table and rebuild / generating the table.

RunnicFusion
  • 193
  • 1
  • 3
  • 12