gpt4 book ai didi

c# - 无法将类型 'System.Linq.IQueryable' 隐式转换为 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'

转载 作者:行者123 更新时间:2023-11-30 15:16:09 29 4
gpt4 key购买 nike

当我开发我的 ASP.Net 应用程序时,显示了以下错误。

Error CS0266
Cannot convert implicitly a type 'System.Linq.IQueryable' into 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable' There is an explicit conversion (check if you lack a cast) urlapp12 C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 37 active

The captured screenshot of the error

t.UrlidUrlid 都是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 代码,不幸的是出现了其他错误。

Error CS0266 Cannot cast implicitly Type 'System.Linq.IQueryable' into 'Microsoft.EntityFrameworkCore.DbSet'. There is an explicit cast. (Are you missing cast?) C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 45 Active

Error CS0266 Cannot cast implicitly Type 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable' into 'Microsoft.EntityFrameworkCore.DbSet'. There is an explicit cast. (Are you missing cast?) C:\Users\mjkpt\source\repos\teststage\urlapp12\urlapp12\Controllers\TagsController.cs 45 Active

(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);

最佳答案

我遇到了同样的问题。 Include 在 IQueryable 之上产生一个新的抽象级别,称为 IIncludableQueryable。您的 var blogging02Context 变为 IIncludeQueryable,不能直接从您的 Where 语句分配。

将您的 blogging02Context 变量声明为 IQueryable<Tag>而不是 var .这对我有帮助。

        IQueryable<Tag> blogging02Context = _context.Tag.Include(t => t.Blog);

if (!string.IsNullOrEmpty(Urlid.ToString()))
{
blogging02Context = blogging02Context.Where(t => t.Urlid == Urlid);
}

关于c# - 无法将类型 'System.Linq.IQueryable' 隐式转换为 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50423538/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com