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

One-to-One Artificial Association

2 Answers 46 Views
Databases and Data Types
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cory
Top achievements
Rank 1
Cory asked on 04 Nov 2015, 07:45 PM

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 Answers, 1 is accepted

Sort by
0
Cory
Top achievements
Rank 1
answered on 04 Nov 2015, 08:07 PM

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

0
Boris Georgiev
Telerik team
answered on 09 Nov 2015, 05:10 PM
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.
Tags
Databases and Data Types
Asked by
Cory
Top achievements
Rank 1
Answers by
Cory
Top achievements
Rank 1
Boris Georgiev
Telerik team
Share this question
or