【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.HashSet'无法隐式转换类型“System.Collections.Generic.HashSet”
【发布时间】:2016-03-25 12:31:11
【问题描述】:

我正在尝试按照此处列出的教程进行操作: 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


    【解决方案1】:

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

    这样的事情应该可以工作:

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

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

    【讨论】:

      【解决方案2】:

      这个:

      public class ApplicationUser : IdentityUser
      {
          public ApplicationUser()
          {
              this.UserImages = new HashSet<UserImage>();
          }
      
          public ICollection<UserImage> UserImages { get; set; }
      
          // continue below ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多