.net c#无法使用模型类"不包含类名的缩位"

Unable to use Model Classes “Does not Contain deffination for Class Name”
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我正在关注Mvc MysicStore项目.在我的模型课上,我有Genre.cs

namespace MvcMusicStore.Models
{
    public class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Descriptio { get; set; }

        public List<Album> albums { get; set; }

    }
}

并以相同的方式增加了2个类,例如Artis.cs Album,cs和Genre.cs.

体裁

namespace MvcMusicStore.Models
{
    public partial class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Album> Albums { get; set; }
    }
}

相册

namespace MvcMusicStore.Models
{
    public class Album
    {
        public int AlbumId     { get; set; }
        public int GenreId     { get; set; }
        public int ArtistId    { get; set; }

        public string Title    { get; set; }

        public decimal Price   { get; set; }
        public string AlbumUrl { get; set; }

        public Genre genre     { get; set; }
        public Artist artist   { get; set; }
    }
}

我的问题是,当我尝试将这些类与示例数据一起使用时,它说

无法使用模型类

我确保没有名称空间问题,并且创建了SampleData.cs类,该类具有以下提供错误的代码

new List<Album>
            {
                new Album { Title = "The Best Of Men At Work", 
                Genre = genres.Single(g => g.Name == "Rock"),
                Price = 8.99M, Artist = artists.Single(a => a.Name == "Men At Work")
                , AlbumArtUrl = "/Content/Images/placeholder.gif" },

它向我显示了上述代码错误,这是怎么回事?

速聊1:
更新
解决过程1

我认为您的媒体资源是用小写字母编写的,因此您尝试使用来访问它Genre.尝试重命名.

这是因为您的Album课程没有Genre属性,而是genre属性.

代替

public Genre genre     { get; set; }

尝试

public Genre Genre     { get; set; }

i am following Mvc MysicStore , Project . In my model class i have Genre.cs

namespace MvcMusicStore.Models
{
    public class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Descriptio { get; set; }

        public List<Album> albums { get; set; }

    }
}

and same way 2 more classes like Artis.cs Album,cs and Genre.cs .

Genre.cs

namespace MvcMusicStore.Models
{
    public partial class Genre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public List<Album> Albums { get; set; }
    }
}

Album.cs

namespace MvcMusicStore.Models
{
    public class Album
    {
        public int AlbumId     { get; set; }
        public int GenreId     { get; set; }
        public int ArtistId    { get; set; }

        public string Title    { get; set; }

        public decimal Price   { get; set; }
        public string AlbumUrl { get; set; }

        public Genre genre     { get; set; }
        public Artist artist   { get; set; }
    }
}

My problem is that when i try to use these classes with sample data it says

Unable to use Model Classes “Does not Contain deffination for Class Name”

i have made sure there are no namespace issues and i have created a class SampleData.cs that has following code that gives error

new List<Album>
            {
                new Album { Title = "The Best Of Men At Work", 
                Genre = genres.Single(g => g.Name == "Rock"),
                Price = 8.99M, Artist = artists.Single(a => a.Name == "Men At Work")
                , AlbumArtUrl = "/Content/Images/placeholder.gif" },

it shows me above mentioned error for this code whats wrong with it ?

Talk1:
updated
Solutions1

I think your property is written in lowercase and you try to access it as Genre. Try renaming it.

It's because your Album class doesn't have a Genre property, but a genreproperty.

Instead of

public Genre genre     { get; set; }

try

public Genre Genre     { get; set; }
转载于:https://stackoverflow.com/questions/23054181/unable-to-use-model-classes-does-not-contain-deffination-for-class-name

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入