简体   繁体   中英

F# ASP.net MVC Project won't compile in VS 2019

I've been revisiting one of my old experiments. The code below is a file I added to Daniel Mohl's F# MVC5 project which I created in Visual Studio 2015. It compiled and worked in VS2015 (and still does) but when I try to compile it in VS2019 I get an error message on the |> this.View lines towards the end: "FS0405: A protected member is called or 'base' is being used. This is only allowed in the direct implementation of members since they could escape their object scope". Does anyone have any idea what I need to do to get rid of the error

namespace fsmvcproject.Models

open System
open System.ComponentModel.DataAnnotations

type Newarticle() = 
    [<Key>]member val Id  = 0 with get, set
    member val Headline = "" with get, set
    member val Author = "" with get, set
    member val Publication = "" with get, set
    member val Intro = "" with get, set
    member val Story = "" with get, set

namespace fsmvcproject.Repositories

open System.Data.Entity
open fsmvcproject.Models
open System.Collections.Generic

type SGdbEntities() = 
    
    inherit DbContext("Data Source=127.0.0.1\SQLEXPRESS;Persist Security Info=True;Initial Catalog=SG;User ID=xxxx;Password=xxxx")
    
    [<DefaultValue()>] val mutable newarticles : IDbSet<Newarticle>
    member x.Newarticles with get() = x.newarticles and set v = x.newarticles <- v    

type NewarticlesRepository() =  
    
    
    member x.GetAll () = 

        use context = new SGdbEntities()    
        query { for a in context.Newarticles do
                sortByDescending a.Id
                select a }
        |> Seq.toList
    
    member x.GetDetail (id) =
        
        use context = new SGdbEntities()         
        query { for a in context.Newarticles do
                where (a.Id = id)
                select a }
        |> Seq.toList

    
namespace fsmvcproject.Controllers

open fsmvcproject.Repositories
open System.Web.Mvc

[<HandleError>]
type ArticlesController(repository :NewarticlesRepository) =
    inherit Controller()
    new() = new ArticlesController(NewarticlesRepository())
    
    member this.Index () =
        repository.GetAll()
        |> this.View

    member this.Detail (id) =
        repository.GetDetail(id)
        |> this.View

I think the problem here is that View is a protected method, which means that you can call it directly from your derived class, but you can't treat it like a first-class F# function.

Thus, to fix the compiler error, try changing model |> this.View to this.View(model) .

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