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

2 posts, 1 answers
  1. Paresh
    Paresh avatar
    13 posts
    Member since:
    Jul 2014

    Posted 10 Sep 2014 Link to this post

    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 

         Class1Id   - Identity

         Class2Id - Identity
         Class1Id - Foreign key to class1 

    Here are the classes 


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

    MappingConfiguration<BaseClass> baseConfiguration = new MappingConfiguration<BaseClass>();

    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");

    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.

  2. Answer
    Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 15 Sep 2014 Link to this post

    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. 

    Kaloyan Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top