Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取“无法隐式转换类型”错误,但无法找出原因_C#_Asp.net Mvc_Entity Framework_Entity Framework Core - Fatal编程技术网

C# 获取“无法隐式转换类型”错误,但无法找出原因

C# 获取“无法隐式转换类型”错误,但无法找出原因,c#,asp.net-mvc,entity-framework,entity-framework-core,C#,Asp.net Mvc,Entity Framework,Entity Framework Core,我的Razor pages项目中有两个文件。一个是AllTracks.htmlcs.cs和AllAlbums.htmlcs.cs 除了一些小的差异外,它们共享的代码基本相同,尽管在我的AllTracks.html.cs页面上,我发现以下错误: Pages\AllTracks.htmlcs.cs(25,26): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<namespaceHere

我的Razor pages项目中有两个文件。一个是AllTracks.htmlcs.cs和AllAlbums.htmlcs.cs

除了一些小的差异外,它们共享的代码基本相同,尽管在我的AllTracks.html.cs页面上,我发现以下错误:

Pages\AllTracks.htmlcs.cs(25,26): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<namespaceHere.Album>' to 'System.Collections.Generic.List<namespaceHere.Tracks>'
我试着调试这个,我试着改变变量名,四处看看,但我一辈子都不明白为什么我会被抛出这个错误,有人能看看我的代码,让我知道为什么会发生这种情况吗

AllTracks.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using namespaceHere;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace namespaceHere
{
    public class AllTracksModel : PageModel
    {
        DatabaseContext _Context;
        public AllTracksModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }
 
        public List<Track> TracksList { get; set; }
        public void OnGet(int? id)
        {
        var data = (from album in _Context.albums
                            where album.AlbumId == id
                            select album).ToList(); 
 
            TracksList = data;  
        }

        public ActionResult OnGetDelete(int? id)
        {
            if (id != null)
            {
                        var data = _Context.tracks.ToList(); 
                            _Context.Remove(data);
                            _Context.SaveChanges();
            }
            return RedirectToPage("/AllTracks");
        }
    }
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using namespaceHere;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace namespaceHere
{
    public class AllAlbumModel : PageModel
    {
        DatabaseContext _Context;
        public AllAlbumModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }
 
        public List<Album> AlbumList { get; set; }
        public void OnGet()
        {
        var data = _Context.albums.Include(x => x.Artist).ToList(); 
 
            AlbumList = data;
        }

        public ActionResult OnGetDelete(int? id)
        {
            if (id != null)
            {
                var data = (from album in _Context.albums
                            where album.AlbumId == id
                            select album).SingleOrDefault();

                            _Context.Remove(data);
                            _Context.SaveChanges();
            }
            return RedirectToPage("/AllAlbum");
        }
    }
}
AllAlbum.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using namespaceHere;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace namespaceHere
{
    public class AllTracksModel : PageModel
    {
        DatabaseContext _Context;
        public AllTracksModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }
 
        public List<Track> TracksList { get; set; }
        public void OnGet(int? id)
        {
        var data = (from album in _Context.albums
                            where album.AlbumId == id
                            select album).ToList(); 
 
            TracksList = data;  
        }

        public ActionResult OnGetDelete(int? id)
        {
            if (id != null)
            {
                        var data = _Context.tracks.ToList(); 
                            _Context.Remove(data);
                            _Context.SaveChanges();
            }
            return RedirectToPage("/AllTracks");
        }
    }
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using namespaceHere;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace namespaceHere
{
    public class AllAlbumModel : PageModel
    {
        DatabaseContext _Context;
        public AllAlbumModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }
 
        public List<Album> AlbumList { get; set; }
        public void OnGet()
        {
        var data = _Context.albums.Include(x => x.Artist).ToList(); 
 
            AlbumList = data;
        }

        public ActionResult OnGetDelete(int? id)
        {
            if (id != null)
            {
                var data = (from album in _Context.albums
                            where album.AlbumId == id
                            select album).SingleOrDefault();

                            _Context.Remove(data);
                            _Context.SaveChanges();
            }
            return RedirectToPage("/AllAlbum");
        }
    }
}

您好@DonnieBerry,asp.net-mvc是与asp.net-core-mvc不同的框架。entity framework和entity-framework-core也是如此。异常消息非常清楚。在AllTracksModel类的OnGet方法中,您应该查询Track,但您正在查询Album。希望这个提示能帮助你解决这个问题。谢谢。你指的是哪一行?我看不到我在AllTracksModel类上查询“Album”的位置。嗨@Donnie,检查AllTracksModel类的OnGet方法。您的LINQ查询正在使用基于您问题中的源代码的相册。您好,对不起,我是Razor pages的新手。我认为,当我想在这个特定用例中使用相册表时,通过使用_Context.tracks可以访问tracks表。这不对吗?