如何将concret DbSet分配给Dbset <interface> </interface>

时间:2014-10-17 10:19:02

标签: c# .net entity-framework dbcontext covariance

我不知道如何正确地将具体DbSet分配给DbSet<Interface>

这是我实现IProviderToken的GoogleToken类:

public class GoogleToken : IProviderToken
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public Guid UserId { get; set; }

    [MaxLength(500)]
    public string Token { get; set; }
}

DropboxToken也一样:

public class DropboxToken : IProviderToken
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public Guid UserId { get; set; }

    [MaxLength(500)]
    public string Token { get; set; }
}

然后,如何在下面的代码中将concret DbSet<GoogleToken>DbSet<DropboxToken>分配给DbSet<IProviderToken>

public async Task<bool> AddExternalTokenAsync(IProviderToken providerToken)
{
    DbSet<IProviderToken> providerTokens = null;
    switch (providerToken.GetType().ToString())
    {
        case "Google":
            providerTokens = context.GoogleTokens;
            break;
        case "Dropbox":
            providerTokens = context.DropboxTokens;
            break;
    }

    ...
}

错误在行中:providerTokens = context.GoogleTokens; :无法隐式转换类型&#39; DbSet<GoogleToken>&#39;到&#39; DbSet<IProviderToken>&#39;

被修改

据我所知,这些类型是兼容的,因为我可以这样做:

IProviderToken token = null;
switch (providerToken.GetType().ToString())
{
    case "Google":
        token = new GoogleToken();
        break;
    case "Dropbox":
        token = new DropboxToken();
        break;
}

问题出现在DbSet上。

0 个答案:

没有答案