无法隐式转换类型'System.Collections.Generic.HashSet'

问题描述 投票:0回答:2

我正在尝试按照此处列出的教程:Link

当我插入下面的代码

 public ApplicationUser()
    {
        UserUpload = new HashSet(); } public ICollection UserUpload { get; set; } 
    }

进入IdentityModels.cs的ApplicationUser类我收到以下错误:

'无法将类型'System.Collections.Generic.HashSet'隐式转换为'System.Collections.Generic.ICollection'。存在显式转换(您是否错过了演员?)。

我是编程新手,不知道该怎么做!任何帮助将不胜感激,谢谢。整个代码如下。它建立在VS 2015的MVC 5上。

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;

namespace ENUSecretShare.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser

    {

        public string FName { get; set; }

        public string LName { get; set; }

        public int nValue { get; set; }

        public int tValue { get; set; }


        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;

        }

    public ApplicationUser()
        {
            UserUpload = new HashSet(); } public ICollection UserUpload { get; set; } 
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}
asp.net-mvc azure hashset icollection
2个回答
0
投票

现在没有我的笔记本电脑,但它甚至编译?你使用System.Collections.Generic,但在你的代码中没有关于UserUploads的通用...

这样的事情应该有效:

ICollection<string> UserUploads = new HashSet<string>();

只需用你的类型交换字符串。


0
投票

这个:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        this.UserImages = new HashSet<UserImage>();
    }

    public ICollection<UserImage> UserImages { get; set; }

    // continue below ...
}
© www.soinside.com 2019 - 2024. All rights reserved.