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

时间:2016-03-25 12:31:11

标签: asp.net-mvc azure hashset icollection

我正在尝试按照此处列出的教程: 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();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

现在不要把我的笔记本电脑放在这里,但它甚至可以编译吗?你使用System.Collections.Generic,但在你的代码中没有任何关于UserUploads的通用...

这样的事情应该有效:

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

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

答案 1 :(得分:0)

此:

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

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

    // continue below ...
}