none
Entity Framework exception - Invalid object name 'dbo.be_Postsbe_Categories'. RRS feed

  • Question

  • I am getting this exception with EF6 in an mVC5 application. I am using EF Code First From Database. I have tables named be_Posts and be_Categories and the classes as generated by Code First from DB. I added the necessary navigation properties to the classes. From everything I have seen this means that the table does not exist. I suspect this is the table EF needs to link up the 2 classes.   So should I just use some SQL and generate the needed table?

    Partial Public Class be_Posts
           <Key>
           PublicProperty PostRowID AsIntegerPublicProperty BlogID AsGuidPublicProperty PostID AsGuid
     
           <Column(TypeName:="nvarchar")> <StringLength(255)>
           PublicProperty Title AsString
     
           <Column(TypeName:="nvarchar")> PublicProperty Description AsString
     
           <Column(TypeName:="nvarchar")> <AllowHtml> PublicProperty PostContent AsStringPublicProperty DateCreated AsDate?
     
           PublicProperty DateModified AsDate?
     
           <StringLength(50)>
          <Column(TypeName:="nvarchar")> PublicProperty Author AsStringPublicProperty IsPublished AsBoolean?
     
           PublicProperty IsCommentEnabled AsBoolean?
     
           PublicProperty Raters AsInteger?
     
           PublicProperty Rating AsSingle?
     
           <StringLength(255)>
           <Column(TypeName:="nvarchar")> PublicProperty Slug AsStringPublicProperty IsDeleted AsBooleanPublicOverridableProperty be_PostTag AsICollection(Ofbe_PostTag)
     
           <ForeignKey("be_Categories")> PublicOverridableProperty be_Categories AsICollection(Ofbe_Categories) 
         
           PublicOverridableProperty be_PostCategory AsICollection(Ofbe_PostCategory)
     
       End Class
    
    Partial Public Class be_Categories
        <Key>
        Public Property CategoryRowID As Integer
        Public Property BlogID As Guid
        Public Property CategoryID As Guid 
        <StringLength(50)>
        Public Property CategoryName As String 
        <StringLength(200)>
        Public Property Description As String
        Public Property be_Posts As ICollection(Of be_Posts)
        Public Property be_PostCategory As ICollection(Of be_PostCategory)
        Public Property ParentID As Guid?  
    End Class


                                                                                                  

        
    Monday, February 23, 2015 6:14 PM

Answers

  • Hello,

    >> No there is no configuration generated by EF Code First from Database for the many to many

    I mean we need to configure in the OnModelCreating method as you did.

    >> I added the mapping below but it wants a physical table for the association

    It does not want a physical table, it would generate the physical table with the given name. After you run the code, there should be a table named be_Postsbe_Categories in your database.

    >> And when i try to insert data no association is inserted in to the association table even if call Add or Attach.

    In Entity Framework, the injection table is not designed to be accessed in project. There records in this table is added/deleted by modifying the collection property of both be_Categories and be_PostCategory in your case. And here is a link shows how to insert data in with many to many: relationship

    http://stackoverflow.com/questions/11551760/ef-code-first-insert-many-to-many

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 2, 2015 10:50 AM
    Moderator

All replies

  • Hello,

    >> So should I just use some SQL and generate the needed table?

    As far as I know, if we use approach as code first model generated from existed database, we do not need to wirte any other additional sql. From your description, it seems that you are wondering why the injection table is not shown, this is by designed in Entity Framework for the many to many relationship. Please check the generated code, it should have a configuration for the many to many relationship.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 24, 2015 4:35 AM
    Moderator
  • No there is no configuration generated by EF Code First from Database for the many to many. I added the mapping below but it wants a physical table for the association. And when i try to insert data no association is inserted in to the association table even if call Add or Attach. 

     modelBuilder.Entity(Of be_Posts)().HasMany(Function(t) t.be_Categories).WithMany(Function(t) t.be_Posts).Map(Sub(m)
                                                                                                                                 m.ToTable("be_Postsbe_Categories")
                                                                                                                                 m.MapLeftKey("be_Categories_CategoryRowID")
                                                                                                                                 m.MapRightKey("be_Posts_PostRowID")
                                                                                                                             End Sub)

    Thursday, February 26, 2015 1:32 PM
  • Hello,

    >> No there is no configuration generated by EF Code First from Database for the many to many

    I mean we need to configure in the OnModelCreating method as you did.

    >> I added the mapping below but it wants a physical table for the association

    It does not want a physical table, it would generate the physical table with the given name. After you run the code, there should be a table named be_Postsbe_Categories in your database.

    >> And when i try to insert data no association is inserted in to the association table even if call Add or Attach.

    In Entity Framework, the injection table is not designed to be accessed in project. There records in this table is added/deleted by modifying the collection property of both be_Categories and be_PostCategory in your case. And here is a link shows how to insert data in with many to many: relationship

    http://stackoverflow.com/questions/11551760/ef-code-first-insert-many-to-many

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 2, 2015 10:50 AM
    Moderator