简体   繁体   中英

EF 5.0 Code First Many to Many Relationship

My application has a number of classes that contain a "Notes" property. We would like to create a single Notes class to contain all of the notes linked back to a NoteId in each class. I've placed:

Private ReadOnly _class As New ObservableListSource(Of Class)()

Public Overridable ReadOnly Property Classes As ObservableListSource(Of Class)
    Get
        Return _class
    End Get
End Property

in the new Notes class for each class to bind to. Then I placed:

Public Property NoteId() As Guid
Public Overridable Property Note() As Note

in each of the classes to bind to. EF is not creating the db if I try to make more than one relationship. This seems to be a many to many issue but I'm not sure what to try next. Any help would be appreciated.

Assuming that each entity can have multiple notes I think you are out of luck. I don't think Entity framework or any other orm will be able to map the relationship in the way you are describing. What you describe is not a many to many relationship. Think of a database relationship like a line. There are two ends. Each end terminates at a single table or more specifically at a primary key property on one and and a foreign key property on the other. What you are describing is like spokes on a wheel where a single relationship is between one table and many others. where a single foreign key column would have to be linked to many other primary key columns. You can force it to work but you won't get any nice entity framework auto-magic where it pulls in notes through a navigation property.

Your options would be:

Have a foreign key to each type of entity that can have a note. Ie add a navigation property on your note class for each type of entity that can have a note,which I don't recommend.

Or

Have a separate note table for each kind of entity. Ie you would have something like ANotes, BNotes, CNotes. I would recommend this one as I think it's a cleaner model than the alternative.

Sorry, and I hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM