我是个脚手架新手。我正在使用VS 2019,我正在尝试搭建一些模型。唯一的问题是,当我运行搭建过程时,它只生成相册模型的代码。这就是我为脚手架过程所做的事情

相册模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication36.Models
{
public class Album
{
public virtual int AlbumId { get; set; }
public virtual int GenreId { get; set; }
public virtual int ArtistId { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set;}
public virtual string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
}艺术家模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication36.Models
{
public class Artist
{
public virtual int ArtistId { get; set; }
public virtual string Name { get; set; }
}
}通用模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication36.Models
{
public class Genre
{
public virtual int GenreId { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual List<Album> Albums { get; set; }
}
}数据上下文类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace WebApplication36.Models
{
public class WebApplication36Context : DbContext
{
public WebApplication36Context (DbContextOptions<WebApplication36Context> options)
: base(options)
{
}
public DbSet<WebApplication36.Models.Album> Album { get; set; }
}
}可以看出,在相册模型中同时引用了艺术家模型和通用模型,但是当我搭建相册模型时,其他两个模型并没有被拉到数据上下文类中。为什么?我做错了什么?我正在写一本更老的MVC5书,这本书是VS2019,这在2019年正常吗?
发布于 2019-09-14 13:24:05
需要在Dbcontext类中添加任何引用或不引用其他模型类的模型类。您需要手动添加它们。
public DbSet<WebApplication36.Models.Album> Album { get; set; }
public DbSet<WebApplication36.Models.Artist> Artist { get; set; }
public DbSet<WebApplication36.Models.Genre> Genre { get; set; }https://stackoverflow.com/questions/57932537
复制相似问题