I read a number of posts from Julie Lerman and here on SO trying to figure out how to configure the relationship between 2 entities. I used EF Code First to generate the classes from an existing db over which I have no control and that already has a lot of data. In one table PostId
is a guid
while PostRowID
is the PK
. When I modify the classes to try and get a relationship I end up with the error Invalid object name 'dbo.be_PostTagbe_Posts'.
Most if not all of the examples seem to deal with Code First and not Code First from DB. How do I map the relationship between the 2 classes? Or do I need a 3rd table to map the relations?
I also looked at the fluent api and got this far:
modelBuilder.Entity(Of be_Posts)().HasMany(Function(a)
a.be_PostTag).WithMany().Map(Function(x)
x.MapLeftKey("PostID")x.MapRightKey("PostID") 'Don't know what to do next
I am trying to use the Include
syntax when this happens. I added the 2 ICollection properties but I suspect since the db deviates from EF convention that's why the EF cannot figure out the relationship.
Partial Public Class be_Posts
<Key>
Public Property PostRowID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
'Other Stuff
Public Property be_PostTag As ICollection(Of be_PostTag)
End Class
And
Partial Public Class be_PostTag
<Key>
Public Property PostTagID As Integer
Public Property BlogID As Guid
Public Property PostID As Guid
'Other stuff
Public Property be_Posts As ICollection(Of be_Posts)
End Class
And the include:
Using db As New BetterBlogContext
post = db.be_Posts.OrderByDescending(Function(x)
x.DateCreated).Include("be_PostTag").Where(Function(p) p.PostRowID
= id).FirstOrDefault
End Using
Got it figure out without using Fluent API. After I added the 2 ICollection Properties, I had to manually add the table Entity Framework needed with the appropriate 2 columns to handle the relationship. Ran a SQL command to copy the needed data and everything worked.