One-to-One Artificial Association

3 posts, 0 answers
  1. Cory
    Cory avatar
    10 posts
    Member since:
    Nov 2014

    Posted 04 Nov 2015 Link to this post

    Hi,

    I'm trying to create an artificial property of a known type for an artificial entity.

    The artificial type has a non-key ​Guid field that maps to the ID field of the known type (one-sided relationship). The known type is essentially a reference table. If I were to explicitly write a class for the artificial type, it would look something like:

    public class ArtificialType
    {
        public Guid ID {get;set;}
        public string Name {get;set;}
        ...etc...
        public Guid TableCodeID {get;set;}
        public TableCode TableCode {get;set;}
    }

     Currently I have:

     

    // Add artificial TableCodeID property (this line is working fine as expected)
    userTableConfiguration.HasArtificialPrimitiveProperty("TableCodeID", typeof(Guid)).HasFieldName("TableCodeID").ToColumn("TableCodeID");
      
    // Add artificial TableCode<TableCode> object property (auto-generates 'ID2' field and causes exceptions during collection retrieval)
    userTableConfiguration.HasArtificialPrimitiveProperty<AttributeTableCode>("TableCode").AsTransient(true);
      
    // Add the association (Prevents the application from compiling unless I remove the 'ToColumn()' call, in which case it adds the 'ID2' field
    userTableConfiguration.HasArtificialAssociation("TableCode", typeof(AttributeTableCode)).ToColumn("TableCodeID");

    I never really expected this to work because there's no mention of which field the TableCodeID is supposed to map to unless it assumes the primary key of the related table. And I'm not sure how to use the Artificial API's HasConstraint extension method.

    I've also noticed that even trying to add the artificial property with type TableCode causes exceptions to be thrown as it seems to auto-generate an 'ID2' field.

     

    Any help is greatly appreciated. Let me know if you need anything clarified as to what I am trying to achieve.

     

    -Cory

     

    // Add artificial TableCodeID property
    userTableConfiguration.HasArtificialPrimitiveProperty("TableCodeID",typeof(Guid)).HasFieldName("TableCodeID").ToColumn("TableCodeID");
     
    // Add artificial TableCode<TableCode> object property
    userTableConfiguration.HasArtificialPrimitiveProperty<AttributeTableCode>("TableCode").AsTransient(true);
     
    // Add the association
    userTableConfiguration.HasArtificialAssociation("TableCode"typeof(AttributeTableCode)).ToColumn("TableCodeID");
    // Add artificial TableCodeID property
    userTableConfiguration.HasArtificialPrimitiveProperty("TableCodeID",typeof(Guid)).HasFieldName("TableCodeID").ToColumn("TableCodeID");
     
    // Add artificial TableCode<TableCode> object property
    userTableConfiguration.HasArtificialPrimitiveProperty<AttributeTableCode>("TableCode").AsTransient(true);
     
    // Add the association
    userTableConfiguration.HasArtificialAssociation("TableCode"typeof(AttributeTableCode)).ToColumn("TableCodeID");
  2. Cory
    Cory avatar
    10 posts
    Member since:
    Nov 2014

    Posted 04 Nov 2015 Link to this post

    So after 2-3 hours of trying to figure this out and another half hour of creating this thread, I figured it out immediately after lol. I guess the HasConstraint() method DOES assume the primary key of the other side. 

    Anyway, I'll include the solution here:

     

    // Add the foreign key property
    userTableConfiguration.HasArtificialPrimitiveProperty(propertyName, clrType).HasFieldName(propertyName).ToColumn(propertyName);
                          
    // Create the association.
    //The HasArtificialAssociation() method creates a property as well, so there was no need to create one first as I was trying to do, which is basically the same procedure as with non-artificial associations. Makes sense.
    //The HasConstraint() call was the key here.
    userTableConfiguration.HasArtificialAssociation(propertyName + "TableCode", typeof(AttributeTableCode)).HasConstraint().ToColumn(propertyName);

    -Cory

  3. DevCraft banner
  4. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 09 Nov 2015 Link to this post

    Hi Cory,

    I am glad that you were able to find the solution for your case. The HasConstraint() method by default map the association to the primary key column.

    If any other questions arise or if you need any assistance with Telerik Data Access do not hesitate to contact us again.

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