Mapping relations

33 views
Skip to first unread message

FabioDA

unread,
Jul 27, 2012, 6:25:13 AM7/27/12
to catnap-or...@googlegroups.com
Hi,
i'm having problem mapping a List relation.  I have an entity that holds Products, and an entity that holds a set of prices for such products.
In my db each record in the prices has a foreign key to the products table.
I have mapped the entities as such:


public class Product

{

public int idProduct { get; set; } 

public int? position { get; set; }

public string codProduct { get; set; }

public string description { get; set; }

public float? weight { get; set; }

public float? itemsAvailable { get; set; }

public float? itemsPerBox { get; set; }

public int active { get; set; }

public string filePicture { get; set; }

public IEnumerable<ListPrices> listPrices {

get { return _listPrices;}

set { _listPrices = value;}

}   


private IEnumerable<ListPrices> _listPrices = new List<ListPrices> ();


public Product ()

{

}

}



and

public class ListPrices

{


public int idListPrice { get; set; }

public float price { get; set; }

public int active{ get; set; }


public DateTime? validFrom { get; set; }

public DateTime?  validTo{ get; set; }


public List list { get; set; }

public Product product { get; set; }

public Discount discount { get; set; }


public ListPrice ()

{

}

}



and my mapping is as follows...

d.Entity<Product> (e => {

e.Table ("Product");

e.Id (x => idProduct);

e.Property (x => x.codProduct);

e.Property (x => x.position);

e.Property (x => x.description);

e.Property (x => x.weight);

e.Property (x => x.itemsAvailable);

        e.Property (x => x.active);

e.Property (x => x.itemsPerBox);

e.Property (x => x.filePicture);

e.List (x => listPrices).ParentIdColumn ("idProduct").CascadeDeletes (true).CascadeSaves (true).Lazy (false);

}

);


d.Entity< listPrices > (e => {

e.Table ("ListPrices");

e.Id (x => x.idListPrices);

e.Property (x => x.price);

e.Property (x => x.validFrom);

e.Property (x => x.validTo);

e.Property (x => x.active);

e.Property (x => x.product);

e.Property (x => x.discount);

}

);


Then I query my object like this...


public IList<Product> getItems (string filter)

{

string filtro = "%" + filter + "%";

var criteria = Criteria.For<Product> ();

criteria.Equal (x => x.active, 1);


if (!string.IsNullOrEmpty (filter)) {

criteria.Or (or =>

{

or.Like (x => x.codProduct, filtro);

or.Like (x => x.description, filtro);

}

);

}


IList<Product> products = Repository.InSession (s => s.List (criteria));

    return products;

}


When I call the s.List(criteria) above I get the following error:
System.InvalidOperationException: Failed to assign the value to property 'listiniArticolo'. Value is of type Int64 ---> System.Exception: The given key was not present in the dictionary.
  at System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) [0x00000] in <filename unknown>:0
  at Catnap.Mapping.Impl.EntityMap`1[PowerAge.Entities.ListinoArticolo].BuildFrom (IDictionary`2 record, ISession session) [0x00053] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/EntityMap.cs:148
  at Catnap.Session+<List>c__AnonStorey14`1[PowerAge.Entities.ListinoArticolo].<>m__32 (IDictionary`2 x) [0x00000] in /Users/fabio/Projects/catnap/src/Catnap/Session.cs:61
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],PowerAge.Entities.ListinoArticolo].MoveNext () [0x00000] in <filename unknown>:0
  at System.Collections.Generic.List`1[PowerAge.Entities.ListinoArticolo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0
  at System.Collections.Generic.List`1[PowerAge.Entities.ListinoArticolo]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable.ToList[ListinoArticolo] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
  at Catnap.Session.List[ListinoArticolo] (ICriteria`1 criteria) [0x0004f] in /Users/fabio/Projects/catnap/src/Catnap/Session.cs:61
  at Catnap.Mapping.Impl.ListPropertyMap`2[PowerAge.Entities.Articolo,PowerAge.Entities.ListinoArticolo].Load (ISession session, System.Object parentId) [0x0002a] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/ListPropertyMap.cs:136
  at Catnap.Mapping.Impl.ListPropertyMap`2[PowerAge.Entities.Articolo,PowerAge.Entities.ListinoArticolo].InnerSetValue (PowerAge.Entities.Articolo instance, System.Object parentId, ISession session) [0x0003d] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/ListPropertyMap.cs:117
  at Catnap.Mapping.Impl.BasePropertyMap`3[PowerAge.Entities.Articolo,System.Collections.Generic.IEnumerable`1[PowerAge.Entities.ListinoArticolo],Catnap.Mapping.Impl.ListPropertyMap`2[PowerAge.Entities.Articolo,PowerAge.Entities.ListinoArticolo]].SetValue (PowerAge.Entities.Articolo instance, System.Object value, ISession session) [0x0009a] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/BasePropertyMap.cs:51
  --- End of inner exception stack trace ---
  at Catnap.Mapping.Impl.BasePropertyMap`3[PowerAge.Entities.Articolo,System.Collections.Generic.IEnumerable`1[PowerAge.Entities.ListinoArticolo],Catnap.Mapping.Impl.ListPropertyMap`2[PowerAge.Entities.Articolo,PowerAge.Entities.ListinoArticolo]].SetValue (PowerAge.Entities.Articolo instance, System.Object value, ISession session) [0x000a9] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/BasePropertyMap.cs:53
  at Catnap.Mapping.Impl.EntityMap`1[PowerAge.Entities.Articolo].BuildFrom (IDictionary`2 record, ISession session) [0x00074] in /Users/fabio/Projects/catnap/src/Catnap/Mapping/Impl/EntityMap.cs:150
  at Catnap.Session+<List>c__AnonStorey14`1[PowerAge.Entities.Articolo].<>m__32 (IDictionary`2 x) [0x00000] in /Users/fabio/Projects/catnap/src/Catnap/Session.cs:61
  at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],PowerAge.Entities.Articolo].MoveNext () [0x00000] in <filename unknown>:0
  at System.Collections.Generic.List`1[PowerAge.Entities.Articolo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0
  at System.Collections.Generic.List`1[PowerAge.Entities.Articolo]..ctor (IEnumerable`1 collection) [0x00000] in <filename unknown>:0
  at System.Linq.Enumerable.ToList[Articolo] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
  at Catnap.Session.List[Articolo] (ICriteria`1 criteria) [0x0004f] in /Users/fabio/Projects/catnap/src/Catnap/Session.cs:61
  at PowerAge.DAO.ItemDAO+<getItems>c__AnonStorey9.<>m__18 (ISession s) [0x00000] in /Users/fabio/Projects/PowerAge/Shared/DAO/DAO.cs:159
  at PowerAge.DAO.Repository.InSession[IList`1] (System.Func`2 func) [0x00011] in /Users/fabio/Projects/PowerAge/Android/DAO/PowerAge.DAO/Repository.cs:37
  at PowerAge.DAO.ItemDAO.getItems (System.String filter) [0x000a4] in /Users/fabio/Projects/PowerAge/Shared/DAO/DAO.cs:159
  at PowerAge.Business.boItems.getItems (System.String filter) [0x00006] in /Users/fabio/Projects/PowerAge/Android/Business/PowerAge.Business/boItems.cs:32
  at PowerAge.actOrder.FillItemListView (System.String filter) [0x00000] in /Users/fabio/Projects/PowerAge/Android/PowerAge/Activities/actOrder.cs:142
  at PowerAge.actOrder.OnCreate (Android.OS.Bundle bundle) [0x00184] in /Users/fabio/Projects/PowerAge/Android/PowerAge/Activities/actOrder.cs:125
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (IntPtr jnienv, IntPtr native__this, IntPtr native_savedInstanceState) [0x00010] in /Users/builder/data/lanes/monodroid-mac-master/ba7b5b19/source/monodroid/src/Mono.Android/platforms/android-12/src/generated/Android.App.Activity.cs:1837
  at (wrapper dynamic-method) object:f28f842e-e747-47c5-b96a-cd2123aad5ea (intptr,intptr,intptr)


(sorry about formatting and eventual syntax errors... i translated all properties from italian.. might have screwed up someplace).

Please advice what I'm doing wrong.

Thanks!


Fabio


Tim Scott

unread,
Jul 27, 2012, 6:16:02 PM7/27/12
to catnap-or...@googlegroups.com
Fabio,

I can't quite tell what's wrong.  Basically the error says that Catnap expects your results set to have a column named "listintiArticolo" but it does not.

I made a change to Catnap to throw a richer exception in your case, which will help me to better see what's happening.  Could you build from the latest source, run that, and then send us your stack trace again?  Also, please translate the key error message in the stack trace.  I think that listinoArticolo mean "listPrices" but I'm not sure.

One thing I noticed is that you have a Product property in ListPrices.  It's not mapped.  Maybe that's what you want.  If you were to map it, I think it would not work.  I don't think that Catnap supports inverse mapping.  Sorry to be a little vague, but it's been a long time since I have used Catnap myself, and I'm a little foggy.  However, this should not be related to the error you're having.

Tim
Reply all
Reply to author
Forward
0 new messages