self reference foreign key

6 posts, 1 answers
  1. PAG
    PAG avatar
    5 posts
    Member since:
    Aug 2012

    Posted 27 Jun 2013 Link to this post

    How to make foreign key in One-to-Many self reference association?
    I try like that, but no key is created:
        protected override IList<MappingConfiguration> PrepareMapping()
        {
            List<MappingConfiguration> preparedConfigs = new List<MappingConfiguration>();
            MappingConfiguration<Class> classConfiguration = new MappingConfigurationWithNamingRules<Class>();
            classConfiguration.MapType(p => new
            {
                UniqID = p.UniqID,
                ParentClassID = p.ParentClassID,
            }).ToTable("Class");
            classConfiguration.MapType().ToTable("Class");
            classConfiguration.HasProperty(p => p.UniqID).IsIdentity(KeyGenerator.IncrementalGuid).ToColumn("UniqID");
            classConfiguration.HasAssociation(p => p.ParentClass).WithOpposite(p => p.ChildClasses).HasConstraint((p, c) => p.ParentClassID == c.UniqID);
            preparedConfigs.Add(classConfiguration);
            return preparedConfigs;
        }
        protected override MetadataContainer CreateModel()
        {
            MetadataContainer container = base.CreateModel();
            container.DefaultMapping.NullForeignKey = true;
            return container;
        }
    public class Class
    {
     
        private Class _parentClass;
        private Guid _parentClassID;
        private IList<Class> _childClasses;
        private Guid _uniqID;
     
        public Class ParentClass
        {
            get return _parentClass; }
            set{_parentClass = value;}
        }
     
        public IList<Class> ChildClasses
        {
            get return _childClasses; }
            set{_childClasses = value;}
        }
     
        public Guid ParentClassID
        {
            get return _parentClassID; }
            set{_parentClassID = value;}
        }
     
        public Guid UniqID
        {
            get return _uniqID; }
            set{_uniqID = value;}
        }
     
        public Class()
        {
            ChildClasses = new List<Class>();
        }
    }


  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 28 Jun 2013 Link to this post

    Hello Sergey,

    By default when you map associations and update (create) your database by using the Schema Change API, foreign key constraints won't be created in the database.

    In order to generate DDL script for creating constraints in the database, you should override the CreateModel method in your FluentMetadataSource class and set the NullForeignKey property to True.

    I suggest you to check the information in this documentation article.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  3. DevCraft banner
  4. PAG
    PAG avatar
    5 posts
    Member since:
    Aug 2012

    Posted 01 Jul 2013 Link to this post

    Yes, I did it. but it does not change anything
    protected override MetadataContainer CreateModel()
    {
        MetadataContainer container = base.CreateModel();
        container.DefaultMapping.NullForeignKey = true;
        return container;
    }

  5. Answer
    Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 03 Jul 2013 Link to this post

    Hello PAG,

    Please excuse us for the misleading reply. What my colleague said is correct, however it does not apply to self-reference associations.

    OpenAccess does not support constraints on self-referencing associations because they need special handling of the insert and delete order. For normal 1:N associations OpenAccess calculates this order based on the table definitions and first inserts the record holding the primary key. For self-references however, the table is only one and this mechanism does not work - the insert/delete order should be calculated based on the actual PK/FK values. Even if this was implemented, there could be cyclic references that could cause problems.

    Having that said, I am afraid that you will not be able to create the constraints with OpenAccess. The only option at the moment is to do that manually in the database.

    Regards,
    Alexander
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  6. Talal
    Talal avatar
    1 posts
    Member since:
    Sep 2014

    Posted 19 Oct 2015 in reply to Alexander Link to this post

    Hello,

    Any update on this issue with the newest DataAccess version?

    The example provided here (about self-reference) does not work: http://docs.telerik.com/data-access/developers-guide/code-only-mapping/mapping-clr-types-properties-and-associations/mapping-associations/fluent-mapping-mapping-clr-mapping-associations-one-to-many

    Maybe you should update that page because it's misleading.

    thanks,
    Talal

     

  7. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 22 Oct 2015 Link to this post

    Hi Talal,

    There are no changes in Telerik Data Access in this regard - still, in case of self-references, you need to to create constrains manually as already mentioned. 

    Thank you for your feedback on our documentation. We appreciate that and we would schedule a task to review that article.

    Should you have any further questions do let us know. 

    Regards,
    Boyan
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top
DevCraft banner