Sunday, February 05, 2012
Support » Support Forum
Register Login
HomeWhat is NDO?- Executive Overview- NDO for DevelopersHow NDO Works- Persistent Classes- Mapping- Queries- Reverse Engineering- Inheritance, Polymorphism- Distributed ApplicationsLicensingSupport- Support Forum- FAQ- Solutions- Downloads- E-Mail Support- Tutorial VideosOnline ShopContact
Welcome to the NDO support forum!
Please register/login at the site if you want to post a thread. We sometimes copy support mails of common interest to the forum. They appear with anonymous sender. Please post your messages in English language if possible. You can do your posts also in German, we will translate your post and our answer so that all users of the forum can read your posts. The forum software requires that javascript is enabled. Please do not insert licence information or licence keys in forum posts. If you know a good answer to a post of another user, feel free to reply! The forum is monitored. We reserve the right to remove posts we consider not to be useful for other forum users.
I have business object classes which are all derived from one base class. The base class provides the ID field for the derived classes. How should I map the id columns of the derived classes, so i can access them? If i do the mapping like this I get the following runtime exception: "Can't find field boGender._nId.". In my application it's necessary to read the primary keys from the database. Here is my base class:
[NDOPersistent] public class boStandard { public int _nId; public int Id { get { return this._nId; } set { this._nId = value; } } ... }
and a derived class: [NDOPersistent]public class boGender : boStandard { private string _sName; private string _sSalutation; public string Name { get { return this._sName; } set { this._sName = value; } } public string Salutation { get { return this._sSalutation; } set { this._sSalutation = value; } } }
Here is the NDO mapping: I'm sure there must be a simple solution for my problem, but I can't find it somewhere in the documentation or support-forum
Hi Jesicca,
sorry for the late answer, the portal software has eaten up my first reply somehow.
What do you mean, if you write, you need to read the primary keys from the database? If you mean, you have autonumbered IDs there, you don't have to bother with it anyway, because that's the default behavior of NDO. If you want to read the value of an Id, use the following code:
Employee emp = new Employee();PersistenceManager pm = new PersistenceManager;pm.MakePersistent(emp);pm.Save();
Console.WriteLine(emp.NDOObjectId.Id.Value);
In the most cases you don't need to map a field to the Id.
Note, that autonumbered IDs have some disadvantages compared with client-side generated keys. We recommend using Guid Ids. You can force NDO to use Guid Ids with the custom attribute [NDOOidType(typeof(Guid))] which may be applied to classes and to whole assemblies. NDO generates automatically a new ID before saving a new object.
I hope, this answers your question. If you have more questions, I'm happy to answer it.
Best RegardsMirko
after the call to MakePersistent your object is still in a transient state, having a temporary id. NDO assings negative IDs for temporary IDs. While you save the object, the database generates the id and this new id will be read back by NDO. After saving the id is available via the NDOObjectId.Id.Value property.
But I think, browsing through Id's is not, what you want. What you might consider is writing a class which contains kind of a preview of your more complex objects. You query the preview objects first and if you need the real object you can construct an ObjectId using the ObjectId of the preview object and the type of the real object class. Our tutorial application shows, how it works. In this app you have the class PictureHeader for showing some Header information in a Grid or whatever UI to select your objects. Using the ObjectId of the PictureHeader a hollow Picture object is constructed, which will be filled from the database, if one of the persistent fields is touched.
Again: you don't have to assign a specific property or field as object id. NDO does it automatically. After compiling with the NDO enhancer enabled your classes magically implement an additional interface IPertsistentCapable which contains the NDOObjectId property.
I hope this answers your question. If not, just ask back.
Mirko