2017-05-19 4 views
0

In meiner Anwendung gibt das petapoco poco ein leeres Objekt zurück (alle Werte sind null). Verwenden Sie das UI-O-Matic Nuget-Paket in meinem Umbraco 7.5.12.PetaPoco gibt ein leeres Objekt zurück

Die Abfrage Ich laufe zur Zeit:

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 ist mein PetaPoco Modell mit den Feldern wie:

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

Wenn debug:

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

Diese Retruns das Objekt :

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

Das Einfügen von Daten funktioniert mit demselben dbContext, der funktioniert. Was fehlt mir hier?

Antwort

0

Entfernen des Attributs [ExplicitColumns] über meine Klasse das Problem behoben. Nein, alles funktioniert wie erwartet. Auch die anderen Dekorationen funktionieren. Also @Nurhak Kaya war teilweise richtig. Nach dem Entfernen dieses Attributs löschen Sie die Tabelle und erstellen/erstellen Sie die Tabelle neu.

+0

Ich bin froh, dass ich hilfreich war. Glückliche Kodierung. –

1

Ich habe Petapoco in verschiedenen Umbraco-Projekt verwendet und mein Ansatz ist ein bisschen anders als Ihre Vorgehensweise. Ich teile es hier, hoffe es hilft dir.

Dies ist das nuget Paket, das ich verwendet habe :(http://nuget.org/List/Packages/PetaPoco)

Bitte mein Beispielcode unten oder in my blog sehen:

[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

Die UmbracoDatabase con = ApplicationContext.Current.DatabaseContext.Database; gibt (und ist die gleiche) wie in meinem Fragecode. Ich habe meinen Datensatz von der Datenbank erhalten, aber Nullwerte innerhalb der Felder. – RunnicFusion

+0

Hatten Sie einen anderen Vorschlag? – RunnicFusion

+1

Das ist richtig, dass Sie Ihre Verbindung genau wie ich erstellen, aber Ihr Tabellenobjekt hat andere Attribute. Ich würde Ihnen empfehlen, Ihre Tabellenklasse mit weniger Attributen zu erstellen und den Weg zu wählen, wie ich die Elemente aus der Datenbank auswähle. Ich kann Ihnen versichern, dass mein Code funktioniert, also machen Sie bitte ein paar einfache Änderungen in Ihrem Code und sehen Sie, ob es funktioniert. Sie können weitere Attribute hinzufügen, nachdem Sie sichergestellt haben, dass select funktioniert. Für Details sehen Sie sich das bitte an. http://www.toptensoftware.com/petapoco/ –

Verwandte Themen