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

Correcting Many to Many Relationships with the Domain Model

1 Answer 75 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jason
Top achievements
Rank 1
Jason asked on 10 Feb 2011, 09:10 PM

I have several many to many tables that OpenAccess did not recognize as such because they have a rowguid column that SqlServer added for replication purposes.

I've gone through the models in the Schema explorer and changed the incorrect table's IsJoinTable property to True.

But what now?  Can I force a regeneration of the .cs files?  

All of the examples I've seen have been for the older version of OpenAccess.

Any help or links would be appreciated.

[A few hours later]
Here's a specific example of three tables that work.  I've got Owner, PhoneNumber and OwnerPhoneNumber (OwnerPhoneNumber did not have the rowguild column so OpenAccess recognized it as a Many-Many table).

OwnerPhoneNumber shows up as a Many-Many table in the Model Schema Explorer.  When I pull up Owners in the Mapping Details Editor I see in the Relationships that:
Relates to: PhoneNumber (PhoneNumbers -> Owners)           [x] Use join table: OwnerPhoneNumber
  
Relationship Ends                                 Join Table Ends
Owner Table                                         OwnerPhoneNumber Table
     OwnerId:int                                           OwnerId:int
  
  
PhoneNumber Table                                   
     PhoneNumberId:int                              PhoneNumberId:int
(sorry for the adhoc 'screenshot')

Now for one that doesn't work.  I have the Facility table, PhoneNumber and FacilityPhoneNumber

When I look at the relations in the Mapping Details Editor for Facility the only 'Relates To' drop down I have is for FacilityPhoneNumber. (FacilityPhoneNumbers -> Facility) 

[Update]
I'm a bit closer, I think.  I deleted the relationships between Facility, PhoneNumber and FacilityPhoneNumber.  Made sure that FacilityPhoneNumber is set to IsJoinTable and created an association between Facility and PhoneNumber.  The new relationship uses the FacilityPhoneNumber join table.  My only problem is that I don't see this relationship in the .CS file.  Here's what got generated for the Facility table:
private IList<PhoneNumber> phoneNumbers = new List<PhoneNumbers>();
[Collection(InverseProperty = "Facility", IsManaged = true)]
[Storage("phoneNumber")]

How do I get generated code that looks like:

private IList<PhoneNumber> phoneNumbers = new List<PhoneNumber>();
[JoinTableAssociation(TableName = "OwnerPhoneNumber", OwnerColumns = "OwnerId", TargetColumns = "PhoneNumberId")]
[Column("OwnerId", OpenAccessType = OpenAccessType.Int32, IsPrimaryKey = true, SqlType = "int")]
[Column("PhoneNumberId", OpenAccessType = OpenAccessType.Int32, IsPrimaryKey = true, SqlType = "int")]
[Storage("phoneNumbers")]

-----



Thanks,

Jason

1 Answer, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 16 Feb 2011, 09:21 AM
Hello Jason,

Please note that in case of m:n relations, the join table is used internally and it is required that it is not mapped to a class. Having this in mind, to setup the second association you need to follow these steps:
  • Make sure the FacilityPhoneNumber table is not mapped to a class. If it is, delete the class from the diagram.
  • Set the IsJoinTable property of the FacilityPhoneNumber table to True. 
  • Drag an Association from the Toolbox from the Facility class to PhoneNumber. In the pop-up dialog enable the Use Join Table combobox and select the FacilityPhoneNumber table from the drop down.
    Then configure the mappings between the join table and the main tables.
I hope that helps. If you continue to experience problems, please let me know.
Best wishes,
Alexander
the Telerik team
Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
Tags
Getting Started
Asked by
Jason
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or