This is a migrated thread and some comments may be shown as answers.

primary keys

5 Answers 231 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Koray Taylan
Top achievements
Rank 1
Koray Taylan asked on 22 Dec 2008, 11:54 PM
hi,

i'm new to openaccess orm and trying to learn how it works.

i'm enabling openaccess for a project that called the engine and it includes both data access services and entity classes.

i have a BaseObject with three fields Id, CreatedDate, CreatedBy and i'm deriving all the orhers from this object.

i want this BaseObject to be a nonpersistent object but if i make it so, those three fields aren't accessible from the derived classes. Also i want to define Id field to be a auto incremented primary key.

How can i achieve this?

By the way, im using sql express and when i change anything in entities i'm getting,

"Error    51    OpenAccess Error: Telerik.OpenAccess: Updating SQL schema failed. System.IndexOutOfRangeException: Index was outside the bounds of the array."

error while i'm building...

5 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 23 Dec 2008, 09:39 AM
        Hi Koray Taylan,

        For a start I'm recommending you our knowledge base where you can find resources that can help you a lot in getting more familiar with our product.
 As for your current problem, the Persistent attribute is not inherited so you must specify all the classes in your hierarchy as persistent (you have to mark the m with the Persistent attribute). Also you can make your base class abstract and the id field protected: that way it can be used as identity for the derived classes. If I understood correctly you don't require a predefined table for the base class (you dont develop the db schema first). For that reason you can go with the forward mapping wizard where you select (in the tree view on the left) the base class and in the inheritance section you select strategy to be horizontal.
After that you select every derived class and in the Identity section select Type: Single Field, ID Field: BaseClass.id(or your desired id field) and Key Generator: AUTOINC. That way you will get a single table for every derived class with all the fields from the base class and the id field as auto-incrementing key generator.

       I hope this helps you. If there are more questions regarding the discussed problem, send us more details on the topic or even send us your sample project.

Sincerely yours,
Zoran
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Koray Taylan
Top achievements
Rank 1
answered on 23 Dec 2008, 03:22 PM
hi,

thanks for your reply.

yes you understand me correct. i want to use forward mapping.

in forward mapping wizzard i select my BaseObject and choose horizontal for inheritance strategy. then i select one of my derived classes and choose sigle field for identity. i select BaseObject._Id for the ID Field but there is no option called AUTOINC for Key Generator. There are <default>, NONE and VERIFY. I can write AUTOINC into the combobox by hand but it doesn't work. it creates a new key column for primary key.

i can send you some screenshots if you want.

oh by the way i'm using the express edition of openaccess does it matter?
0
Zoran
Telerik team
answered on 23 Dec 2008, 04:12 PM
Hello Koray Taylan,
I reproduced the described behavior and I do have an explanation what is happening. The reason that the AUTOINC choice was not available for the primary key is that your BaseObject.Id is probably not of type int. You must change the type of the field so it can be able to auto-increment itself. I must also note that probably you will have to fix your database or create new: if the tables that have the primary key are already created with a different type (from the one used for the objects, example: table PK is string, whether object ID is int), they can not be changed automatically.

Sharing your feedback will surely help us at Telerik make OpenAccess better.

All the best,
Zoran
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Koray Taylan
Top achievements
Rank 1
answered on 23 Dec 2008, 08:13 PM
hi again,

[Telerik.OpenAccess.Persistent()] 
    public abstract class BaseObject 
    { 
         
        protected int _Id; 
        public int Id 
        { 
            get 
            { 
                return _Id; 
            } 
            protected set 
            { 
                if (_Id != value) 
                { 
                    _Id = value; 
                } 
            } 
        } 

i'm defining my BaseObject exactly as above. there are also two more fields i mentioned in my first post but they don't matter about our concern here i think.

also i can mail you the whole project if you want...

Thanks,
Koray.


0
Zoran
Telerik team
answered on 27 Dec 2008, 08:17 AM
Hello Koray Taylan,

       The model you have produced is a perfectly working with OpenAccess standard or express edition. There should be no problems with the autoinc feature. You should try to do all the steps from scratch, because maybe during the development some changes are made that created wrong tables in the database that are associated with your model. If you are unable to solve the problem in the described way, feel free to send me a sample project. Hope that helps.

Greetings,
Zoran
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Getting Started
Asked by
Koray Taylan
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Koray Taylan
Top achievements
Rank 1
Share this question
or