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

Only bi-directional associations are allowed in a 1:1 autoinc setup

1 Answer 89 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paresh
Top achievements
Rank 1
Paresh asked on 10 Sep 2014, 11:02 PM
I am getting following error while setting up 1:1 relation

"Only bi-directional associations are allowed in a 1:1 autoinc setup. 'Models.Class1' is set to be autoinc."

Here are the tables 

TBLClass1:
     Class1Id   - Identity
     Property1

TBLClass2:
     Class2Id - Identity
     Class1Id - Foreign key to class1 
     Property2

Here are the classes 

BaseClass:
     Id

Class1: BaseClass
     String Property1
     Class2 objectA
     
Class2: BaseClass
    string Property2
    int class1id
    Class1 objectB
    
Here is the mapping

Base:
MappingConfiguration<BaseClass> baseConfiguration = new MappingConfiguration<BaseClass>();
baseConfiguration.MapType().Inheritance(Telerik.OpenAccess.InheritanceStrategy.Horizontal);

Class1
MappingConfiguration<Class1> configuration = new MappingConfiguration<Class1>();
configuration.MapType(x => new { Property1 = x.Property1 }).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable("TBLClass1");
configuration.HasProperty(x => x.Id).ToColumn("Class1Id").IsIdentity(KeyGenerator.Autoinc);
configuration.HasAssociation(x => x.objectA).WithOpposite(x => x.objectB).ToColumn("Class1Id");

Class2
MappingConfiguration<Class2> configuration = new MappingConfiguration<Class2>();
configuration.MapType(x => new { Property2 = x.Property2 }).WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable("TBLClass2");
configuration.HasProperty(x => x.Id).ToColumn("Class2Id").IsIdentity(KeyGenerator.Autoinc);
configuration.HasProperty(x => x.class1id).ToColumn("class1id");
configuration.HasAssociation(x => x.AssemblyHeader).WithOpposite(x => x.AssemblyOptions).ToColumn("AssemblyHeaderId").HasConstraint((x, y) => x.class1id == y.Id).IsRequired();

Let me know if I am missing anything here.

Thanks,
Paresh

1 Answer, 1 is accepted

Sort by
0
Accepted
Kaloyan Nikolov
Telerik team
answered on 15 Sep 2014, 07:56 AM
Hello Paresh,

1:1 associations are supported only between the primary keys of both entities. Which means you should not use a special FK property in your mapping but the identity property of Class2. 

Also you should decide which is the "main" class of both, this should be the one which you create first and then assign the other. Only this main class should be mapped with .IsIdentity(KeyGenerator.Autoinc); the other class should specify only its primary key with this mapping: .IsIdentity() but do not specify a key generator as the value will be taken from the main class which is stored previous. It is not allowed both to autoinc.

I hope this helps, should you have any other questions do not hesitate go get back to us. 

Regards,
Kaloyan Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Development (API, general questions)
Asked by
Paresh
Top achievements
Rank 1
Answers by
Kaloyan Nikolov
Telerik team
Share this question
or