F#

时间:2016-05-23 20:44:05

标签: entity-framework f# asp.net-identity

所以我有一个基于Entity Framework Identity模型的用户类:

type ApplicationUser() = 
    inherit IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>()
    let mutable id = 0
    member this.GenerateUserIdentityAsync (manager : UserManager<ApplicationUser, int>) =
        manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie)
    [<DatabaseGenerated(DatabaseGeneratedOption.Identity)>] override this.Id with get() = id and set(v) = id <- v

我还有一份外部身份列表(例如Facebook):

[<CLIMutable>]
type ExternalIdentity = 
    {
        [<DatabaseGenerated(DatabaseGeneratedOption.Identity)>] Id : int
        [<Required; MaxLength(128)>] ExternalUserId : string
        [<Required; MaxLength(128)>] Provider : string
        UserId : int
        [<ForeignKey("UserId")>] User : ApplicationUser
    }

我想将导航属性添加到外部身份列表中。但每次我尝试增加ApplicationUser类型时,我都会得到“扩充中不允许使用此声明元素”。

我最近的尝试是:

type ApplicationUser with
    abstract member ExternalIdentities : ICollection<ExternalIdentity> with get, set

但这不会编译。

请不要告诉我,我必须回到C#才能完成这项工作。

1 个答案:

答案 0 :(得分:2)

为了在F#中定义相互依赖的类型,有and关键字:

#I @"..\packages\Microsoft.AspNet.Identity.EntityFramework.2.2.1\lib\net45"
#I @"..\packages\Microsoft.AspNet.Identity.Core.2.2.1\lib\net45\"
#r "Microsoft.AspNet.Identity.Core.dll"
#r "Microsoft.AspNet.Identity.EntityFramework.dll"
#r "System.ComponentModel.DataAnnotations.dll"

open Microsoft.AspNet.Identity
open Microsoft.AspNet.Identity.EntityFramework
open System.Collections.Generic
open System.ComponentModel.DataAnnotations
open System.ComponentModel.DataAnnotations.Schema

type ApplicationUserLogin() = inherit IdentityUserLogin<int>()

type ApplicationUserRole() = inherit IdentityUserRole<int>()

type ApplicationUserClaim() = inherit IdentityUserClaim<int>()

type ApplicationUser() = 
    inherit IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>()
    let mutable id = 0
    member this.GenerateUserIdentityAsync (manager : UserManager<ApplicationUser, int>) =
        manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie)
    [<DatabaseGenerated(DatabaseGeneratedOption.Identity)>] override this.Id with get() = id and set(v) = id <- v
    member val ExternalIdentities : ICollection<ExternalIdentity> = upcast [||] with get, set


and [<CLIMutable>] ExternalIdentity =
    {
        [<DatabaseGenerated(DatabaseGeneratedOption.Identity)>] Id : int
        [<Required; MaxLength(128)>] ExternalUserId : string
        [<Required; MaxLength(128)>] Provider : string
        UserId : int
        [<ForeignKey("UserId")>] User : ApplicationUser
    }