vb.netef-code-firstedmx

How to link a view to a model in vb.net code First


I've an old programm with edmx. Inside this one, I've linked a class (Table) To a View (Table/filter on a value of a column) I want migrate this project to code first. I copy/paste the project delete edmx file and generate models from an existing database. All is good except this link.

<Table("JoinAffectation")>
partial public Class JointAffectation
 public property Id as Long
 public IdRecherche as Integer 'the link with my view
 <NotMapped>
 <ForeignKey("Id")>
 PUBLIC OVERRIDABLE PROperty RechercheJoint as ViewRechercheJoint

But When I try to use function of automatical sort/filter using expression I've error : The specified type member 'RechercheJoint' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

If I removed I error saying I don't same comumn and property... Also , How Can I stipulate RechercheJoint is mapped on IdRecherche

thanks for your help


Solution

  • Finally Using modelbuilder, I can join my view and my table like in edmx

    <Table("JointAffectation")>
    Partial Public Class JointAffectation
      Public Property Id As Long
      Public Property IdTypeJoint As Long
      Public Property IdRecherche As Integer
      Public Overridable Property JointType As JointType
    
      <ForeignKey("Id")>
      Public Overridable Property RechercheJoint As ViewRechercheJoint
    
    End Class
    
    <Table("ViewRechercheJoint")>
    Partial Public Class ViewRechercheJoint
      <Key>
      <DatabaseGenerated(DatabaseGeneratedOption.None)>
      Public Property Id As Integer
    
      <StringLength(50)>
      Public Property Libelle As String
    
      <ForeignKey("IdRecherche")>
      Public Overridable Property JointAffectations As ICollection(Of JointAffectation)
    
    End Class 
    
    modelBuilder.Entity(Of JointAffectation)() _
            .HasRequired(Function(e) e.RechercheJoint) _
            .WithMany(Function(e) e.JointAffectations) _
            .HasForeignKey(Function(e) e.IdRecherche)