I thought I understood from experience how to do many-to-one relationships and from the documentation how to attempt a many-to-many relationship. Unfortunately, things are not working at the moment. If I comment out the property/relationship State.Cities, remove the Association declaration from City.State, and comment out both sides of the many-to-many relationship between ZipCode and AreaCode then my website builds. If I do not, then during Domain.Build() I get:

Item with key 'Fields' was not found. You might have forgotten to apply [Field] attribute on property Fields.State. OR Field 'ZipCode.AreaCodes' is already paired with 'AreaCode.ZipCodes'. Please remove [Association] attribute at 'AreaCode.ZipCodes'. I am particularly confused as to why Country->States works but State->ZipCodes does not. I was trying to learn how to use a many-to-many relationship from linq-many-to-many-relationship topic @ support and documentation

I am working with a legacy database (DomainUpgradeMode.LegacyValidate) and there is no way around that. A partial excerpt of my code base, which I hope is the relevant part, is:

<TableMapping("tblCountries")>
<HierarchyRoot(), KeyGenerator(KeyGeneratorKind.None)>
Public Class Country Inherits Entity

    <Field(), Key()>
    Property ID As Integer

    <Field(Nullable:=False), FieldMapping("geoLocationID")>
    Property GeoLocation As GeoLocation

    <Field(), Association("Country")>
    Property States As EntitySet(Of State)
End Class

<TableMapping("tblStates")>
<HierarchyRoot(), KeyGenerator(KeyGeneratorKind.None)>
Public Class State
    Inherits Entity

    <Field(), Key()>
    Property ID As Integer

    <Field(), FieldMapping("geoLocationID")>
    Property GeoLocation As GeoLocation

    <Field(Nullable:=False), FieldMapping("countryID"), Association("States")>
    Property Country As Country

    <Field(), Association("State")>
    Property Cities As EntitySet(Of City)
End Class

<TableMapping("tblCities")>
<HierarchyRoot(), KeyGenerator(KeyGeneratorKind.None)>
Public Class City
    Inherits Entity

    <Field(), Key()>
    Property ID As Integer

    <Field(Nullable:=False), FieldMapping("geoLocationID")>
    Property GeoLocation As GeoLocation

    <Field(Nullable:=False), FieldMapping("stateID"), Association("Cities")>
    Property State As State
End Class

<TableMapping("tblZipCodes")>
<HierarchyRoot(), KeyGenerator(KeyGeneratorKind.None)>
Public Class ZipCode
    Inherits Entity

    <Field(), Key()>
    Property ID As Integer

    <Field(Nullable:=True), FieldMapping("geoLocationID")>
    Property GeoLocation As GeoLocation

    <Field(), Association("ZipCodes")>
    Property AreaCodes As EntitySet(Of AreaCode)
End Class

<TableMapping("tblAreaCodes")>
<HierarchyRoot(), KeyGenerator(KeyGeneratorKind.None)>
Public Class AreaCode
    Inherits Entity

    <Field(), Key()>
    Property ID As Integer

    <Field(Nullable:=False), FieldMapping("geoLocationID")>
    Property GeoLocation As GeoLocation

    <Field(), Association("AreaCodes")>
    Property ZipCodes As EntitySet(Of ZipCode)
End Class

I have tried to post a simplified example of my problem. If the full code is needed to diagnose the issue then I can make it available in a zip archive. If there are any other questions I can answer or information I can provide about what I am trying to do or how I am trying to do it, please let me know. Thank you very much for your time and assistance.

asked Feb 14 '12 at 11:43

GigMastersTech's gravatar image

GigMastersTech
27111115

edited Feb 15 '12 at 05:30

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211


One Answer:

Greetings GigMastersTech,

I see 2 problems here.

  1. Avoid using State name for a persistent property because property with the same name is already declared in Persistent class and this leads to a conflict. So in your case you'd better rename City.State to something else (e.g. City.CityState or similar).

  2. When you declare paired association, you should not do this on both sides of the association, just on one side.

Hope that helps.

answered Feb 15 '12 at 06:23

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

1

Thank you very much. That solved the problem. I renamed my .State properties to .ContainingState and removed Association("ZipCodes") from ZipCode.AreaCodes

(Feb 15 '12 at 12:07) GigMastersTech GigMastersTech's gravatar image

You are welcome! Thanks for accepting the answer! =)

(Feb 15 '12 at 12:20) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA