Help needed!

Feb 15, 2013 at 8:41 AM
Hello! My name is Tudor and I am currently developing a c# app for mobile devices (PDA's) WM 6.5 .Even if it may seem a silly question, I will ask it : What .dll files must be included in the project? Those from the CF folder of from the FFx? I included the OpenNETCF.ORM.dll and the OpenNETCF.SqlCE.dll and i keep getting lots and lots of issues like when i try to use the "isIdentity" value on a Field(OpenNETCF.ORM.FieldAttribute' does not contain a definition for 'IsIdentity') or the "DefaultTipe". Strangely the "SearchOrder", "IsPrimaryKey", "FieldName" and "Autofill" seem to work. I am following the examples from source code and documentation step by step. When I try to add items through the "Reference type", i keep getting the "Key not found" exception. I downloaded the latest ORM binaries from 29 january. So please point me to a good direction, cause i feel like i am loosing myself with this issues. Do I need to add another references to my project than the ones I specified earlier? Or more? What am I doing wrong?Please HELP! I will try to point some issues through source code :


Entity Inventare :

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.ORM;

namespace SmartPDA.Entities
{
[Entity(KeyScheme.Identity, NameInStore = "Inventare")]
public class Inventar
{
    [Field(IsPrimaryKey = true, IsIdentity = true, FieldName = "Inventar_ID")]
    public Int64 Inventar_ID { get; set; }

    [Field(FieldName = "Partener_ID")]
    public Int64 idPartener { get; set; }

    [Field(FieldName = "PctLucru_ID")]
    public Int64 idPctLucru { get; set; }

    [Field(FieldName = "DataInventar")]
    public  DateTime dataInventar { get; set; }

    [Field(FieldName = "Inventar_Tip")]
    public  Boolean tipInventar { get; set; }

    [Reference(typeof(LiniiInventar), "Linie_ID", Autofill = false,  ReferenceType = ReferenceType.OneToMany)]
    //public List<LiniiInventar> liniiInventar { get; set; }
    public LiniiInventar[] liniiInventar { get; set; }
}
}

Entity LiniiInventar

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OpenNETCF.ORM;


namespace SmartPDA.Entities
{
[Entity(KeyScheme.Identity, NameInStore = "LiniiInventare")]
public class LiniiInventar
{
    [Field(IsPrimaryKey = true, FieldName = "Linie_ID")]
    public Int64 Linie_ID { get; set; }

    [Field(FieldName = "CodArticol")]
    public String codArticol { get; set; }

    [Field(FieldName = "Cantitate")]
    public String cantitate { get; set; }

    //[Reference(typeof(Inventar), "Inventar_ID")]
    [Field(FieldName = "InventarID")]
    public Int64 Inventar_ID { get; set; }
}
}


The following function is called to Insert the new Object "Inventar" and its references, where m_store is of type SqlCeDataStore;

public void adaugaInventar(Inventar inventar)
    {
        m_store.Insert(inventar, true);
    }
For example, when I try to add a new object "Inventar" with some "InventarLines" I keep getting the "Key not found" exception. King regards! Tudor
Coordinator
Feb 15, 2013 at 3:02 PM
It sounds like a version mismatch. My suggestion is to get the very latest change set (use the Source Code->History link at the top of the page) and add a project reference to see if that alleviates things.
Feb 16, 2013 at 1:11 PM
Gonna try it myself, but one of my collegues already tried and got the same result...but you know what they say, never say no, untill you tried it :) I'll get back to you with the results on Monday. Thanks for replying!
Feb 18, 2013 at 7:50 AM
Hello again! Same exception : KeyNotFound. Used latest from source Code-> history.
Feb 18, 2013 at 9:12 AM
Also, tried your reference example. DIDN'T WORK. Added 2 authors and 1 books but got only 1 author reference. ANy ideas?


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartDeviceProject1.Entities;
using OpenNETCF.ORM;
using System.Diagnostics;

namespace SmartDeviceProject1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //DataService testDS = new DataService();
        var store = new DataService();

        store.AddBook(null, null, null);

        var authors = new List<Person>();
        var illustrators = new List<Person>();

        authors.Add(store.AddPerson("Alexadre", "Dumas"));
        authors.Add(store.AddPerson("Richard", "Castle"));
        illustrators.Add(store.AddPerson("Stan", "Lee"));

        store.AddBook("Title", authors.ToArray(), illustrators.ToArray());
        Book books = store.GetAllBooks();
        books.Title = "test";
        store.UpdateBooks(books);
        Book book2 = store.GetAllBooks();
        foreach (Person persoana in books.Authors)
        {
            Debug.WriteLine(persoana.FirstName);
        }
    }
}

public class DataService
{
    private SqlCeDataStore m_store;

    public DataService()
    {
        m_store = new SqlCeDataStore("MyData.sdf");

        m_store.DeleteStore();

        if (!m_store.StoreExists)
        {
            m_store.CreateStore();
        }

        m_store.AddType<Book>();
        m_store.AddType<Person>();

        m_store.EnsureCompatibility();
    }

    public Person AddPerson(string firstName, string lastName)
    {
        var person = new Person()
        {
            FirstName = firstName,
            LastName = lastName
        };

        m_store.Insert(person);

        return person;
    }

    public Book AddBook(string title, Person[] authors, Person[] illustrators)
    {
        var book = new Book()
        {
            Title = title,
            Authors = authors,
            Illustrators = illustrators
        };

        m_store.Insert(book);

        return book;
    }

    public Book GetAllBooks()
    {
        return m_store.Select<Book>(true).ToArray().First<Book>();
    }
    public void UpdateBooks(Book carte)
    {
        m_store.Update(carte);
    }
}
}
Coordinator
Feb 18, 2013 at 2:56 PM
I agree, there's something wrong here. I'm working on it now and hope to have a resolution this morning. I'll add this as a bug so you can track it.
Feb 18, 2013 at 3:37 PM
thanks alot dude! :)
Coordinator
Feb 18, 2013 at 3:41 PM
My documentation on references (the lack of any, really) also sucks, making it difficult to understand how these should work. I'll try to fix that as well.
Coordinator
Feb 18, 2013 at 7:29 PM
Ok, I've updated the ReferenceSample (FFx) for 1:n references and made some code updates. See the docs for a little more detail. I still have to update the sample and validate n:1, so I'll leave the bug as "open" until that is done.
Feb 22, 2013 at 12:20 PM
FYI, I am getting the same issue at head (#101760). I can post a sample here if it helps.
Coordinator
Feb 22, 2013 at 3:19 PM
@bouvrais: when you say you are "getting the same issue", which issue do you mean? The wrong number of references coming back or the field attribute errors? If you have a repro sample, that would be fantastic. Just mail it to me or attach it.
Feb 22, 2013 at 4:44 PM
I get a KeyNotFoundException in in FieldAttributeCollection.cs whenever I try to set insertReferences to true while inserting an Entity which has a ManyToOne reference to another entity. I will send the full project sample by email.
Feb 22, 2013 at 4:45 PM
Edited Feb 22, 2013 at 4:47 PM
Didn't find a way to attach files, here is the sample I used:
namespace TestOpenNetCFReferences
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using OpenNETCF.ORM;
    using System.Reflection;

    [Entity(KeyScheme.Identity)]
    public class Asset
    {
        [Field(IsPrimaryKey = true)]
        public int Id { get; set; }
        [Field]
        public string Name { get; set; }
        [Reference(typeof(User), "Username", ReferenceType = ReferenceType.ManyToOne)]
        public User Owner { get; set; }
    }

    [Entity(NameInStore = "Users")]
    public class User
    {
        [Field(IsPrimaryKey = true)]
        public string Username { get; set; }
        [Field]
        public string Fullname { get; set; }
    }    
    
    class Program
    {


        static void Main(string[] args)
        {
            // Load the store.
            string path = "TestReferences.sdf";
            SqlCeDataStore Store = new SqlCeDataStore(path);
            if (!Store.StoreExists)
            {
                Store.CreateStore();
                Store.AddType<Asset>();
                Store.AddType<User>();
            }
            else
            {
                Store.DiscoverTypes(Assembly.GetExecutingAssembly());
            }


            User myUser = new User { Username = "MyUser", Fullname = "My User" };
            Asset myAsset = new Asset { Name = "myItem", Owner = myUser };

            Store.Insert(myAsset, true);
        }
    }
}