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

self reference foreign key

5 Answers 109 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.
Sergey
Top achievements
Rank 1
Sergey asked on 27 Jun 2013, 10:05 AM
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>();
    }
}


5 Answers, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 28 Jun 2013, 02:00 PM
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.
0
Sergey
Top achievements
Rank 1
answered on 01 Jul 2013, 07:41 AM
Yes, I did it. but it does not change anything
protected override MetadataContainer CreateModel()
{
    MetadataContainer container = base.CreateModel();
    container.DefaultMapping.NullForeignKey = true;
    return container;
}

0
Accepted
Alexander
Telerik team
answered on 03 Jul 2013, 03:29 PM
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.
0
Talal
Top achievements
Rank 1
answered on 19 Oct 2015, 09:31 AM

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

 

0
Boyan
Telerik team
answered on 22 Oct 2015, 07:51 AM
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.
Tags
Development (API, general questions)
Asked by
Sergey
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Sergey
Top achievements
Rank 1
Alexander
Telerik team
Talal
Top achievements
Rank 1
Boyan
Telerik team
Share this question
or