错误CS0266
无法将类型'System.Linq.IQueryable'隐式转换为'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'。存在显式转换(请检查是否缺少强制转换)。urlapp12 C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 37 active。
以下是错误的截图:
![The captured screenshot of the error](https://istack.dev59.com/OUu3V.webp)
Urlid
都是int
类型。包含错误的代码如下:
//int id, [Bind("BlogId,Userid,Url,Title")] Blog blog
// GET: Tags
// public async Task<IActionResult> Index()
public async Task<IActionResult> Index(int id, [Bind("Urlid,Userid,UrlStr,Title")] Url blog, int Urlid)
{
/*
return View(await _context.Tag.ToListAsync());
*/
var blogging02Context = _context.Tag.Include(t => t.Blog);
if (!string.IsNullOrEmpty(Urlid.ToString()))
{
blogging02Context = blogging02Context.Where(t => t.Urlid == Urlid);
}
ViewBag.Urlid = Urlid;
return View(await blogging02Context.ToListAsync());
// return View (await _context.Tag.ToListAsync());
}
Url
模型 Url.cs 如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace urlapp12.Models
{
public partial class Url
{
public Url()
{
Post = new HashSet<Post>();
}
[Key]
public int Urlid { get; set; }
public string UserId { get; set; }
public string UrlStr { get; set; }
public string Title { get; set; }
public string CreatedBy { get; set; }
public string CreatedBy_UserName { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime CreatedAt_UtcDt { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime CreatedAt_LocalDt { get; set; }
public string LastUpdatedBy { get; set; }
public string LastUpdatedBy_UserName { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime LastUpdatedAt_UtcDt { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime LastUpdatedAt_LocalDt { get; set; }
public virtual ICollection<Tag> Tag { get; set; }
public virtual ICollection<BlogIUDSqlHistory> BlogIUDSqlHistory { get; set; }
public ICollection<Post> Post { get; set; }
/*
public List<Tag> Tag { get; set; }
public List<Post> Post { get; set; }
*/
}
}
Tag
模型Tag.cs如下:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace urlapp12.Models
{
public class Tag
{
[Key]
public int TagId { get; set; }
public int DispOrderNbr { get; set; }
public string tagItem { get; set; }
public int Urlid { get; set; }
public string CreatedBy { get; set; }
public string CreatedBy_UserName { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime CreatedAt_UtcDt { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime CreatedAt_LocalDt { get; set; }
public string LastUpdatedBy { get; set; }
public string LastUpdatedBy_UserName { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime LastUpdatedAt_UtcDt { get; set; }
[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "datetime2")]
public DateTime LastUpdatedAt_LocalDt { get; set; }
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("Urlid")]
public Url Blog { get; set; }
}
}
(2018-05-19 18:17 添加) 我尝试了Mohsin Mehmood的代码,但不幸出现了其他错误。
错误 CS0266 无法将类型“System.Linq.IQueryable”隐式转换为类型“Microsoft.EntityFrameworkCore.DbSet”。存在显式转换。(是否缺少强制转换?) C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 45 活动的
错误 CS0266 无法将类型“Microsoft.EntityFrameworkCore.Query.IIncludableQueryable”隐式转换为类型“Microsoft.EntityFrameworkCore.DbSet”。存在显式转换。(是否缺少强制转换?) C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 45 活动的
(2018-05-19 18:43 添加)
它奏效了!非常感谢user1672994
:有必要进行一些小更改,如下所示,但这不是一个大问题:
(更改前(user1672994的想法))
var blogging02Context = _context.Tag.Include(t => t.Blog).Where(t => t.Urlid == Urlid));
(更改后)
var blogging02Context = _context.Tag.Include(t => t.Blog).Where(t => t.Urlid == Urlid);