Answered by:
EF Core will Scaffold one Model, not another

Question
-
User-2097536512 posted
Hello all, I'm building a .NET Core website along with Entity Framework using Code-First, and am getting the following error while scaffolding my second Model:
There was an error running the selected code generator:
There is no entity type Product in DbContext MyCompany.MyApp.Data.LoanDbContext
at
Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine> b__6_0()
at
Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at
Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)I've worn out my Google Fu, having only found a couple people hitting the same error, and in their case it was either due to a missing Primary Key, or a Many-to-Many relationship. My Keys in both cases are integers named ID, and I even tried adding the [Key] annotation in case that helped. I also have no Many-to-Many relationships in the entire program.
The first scaffold (with a more complex Model) went fine; I used the tool to generate a new DbContext type, with the intent of then using that Context to handle all my other Controllers/Models, leaving the auto-generated ApplicationDbContext alone.
Has anyone had success scaffolding a second Controller + Views into an existing, custom (but auto-generated) DbContext class?
Models\Product.cs (this is the one that throws the error)
using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace MyCompany.MyApp.Models { public class Product { #region Main Properties [Key] public int ID { get; set; } [Required, StringLength(20)] [Display(Name = "Model Number")] public string ModelNumber { get; set; } public int OwnerID { get; set; } [StringLength(30)] public string Description { get; set; } [DataType(DataType.Currency), Column(TypeName = "money"), DefaultValue(0.00)] [Display(Name = "List Price")] public decimal ListPrice { get; set; } [DataType(DataType.Currency), Column(TypeName = "money"), DefaultValue(0.00)] [Display(Name = "Standard Cost")] public decimal StandardCost { get; set; } public char Status { get; set; } #endregion #region Navigation Properties public virtual Owner Owner { get; set; } public virtual List<Inventory> Inventory { get; set; } #endregion } }
Models\LoanRequest.cs (this one was scaffolded first, and worked fine)
using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace MyCompany.MyApp.Models { public class LoanRequest { #region Main Properties public int ID { get; set; } public int CompanyID { get; set; } public string EmployeeID { get; set; } public string DivisionType { get; set; } public string SalesAreaID { get; set; } public int LoanReasonID { get; set; } [DataType(DataType.Date)] [Display(Name = "Ship Date"), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ShippedDate { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime ReturnedDate { get; set; } [StringLength(5)] public string BudgetDemoCode { get; set; } [StringLength(255)] public string Comment { get; set; } [DefaultValue(0)] public int Purpose { get; set; } [StringLength(255)] public string AgreementComment { get; set; } [DefaultValue(false)] public bool LoanConfirmed { get; set; } [StringLength(20)] public string TradeShowName { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime TradeShowDate { get; set; } #endregion #region Navigation Properties public virtual Company Company { get; set; } [ForeignKey("LdapID")] public virtual Employee ResponsibleEmployee { get; set; } public virtual SalesArea SalesArea { get; set; } public virtual LoanReason LoanReason { get; set; } public virtual List<LoanRequestItem> Items { get; set; } #endregion } }
Data\LoanDbContext.cs (this was generated by the first scaffold. Not much there to see, but hey, here it is.)
using Microsoft.EntityFrameworkCore; using MyCompany.MyApp.Models; namespace MyCompany.MyApp.Data { public class LoanDbContext : DbContext { public LoanDbContext(DbContextOptions<LoanDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize the ASP.NET Identity model and override the defaults if needed. // For example, you can rename the ASP.NET Identity table names and more. // Add your customizations after calling base.OnModelCreating(builder); } public DbSet<LoanRequest> LoanRequests { get; set; } } }
Thanks for any help, advice or suggestions. I'm a long-time .NET Developer trying to prove to my company that .NET can be an affordable option with .NET Core hosted in Linux, and really want to show off the power of things like Entity Framework so we can revitalize some of our old sites/apps and get some cost savings as we migrate them to the cloud.
This is my first attempt at using .NET Core, so bear with me if I miss something obvious.
Wednesday, December 21, 2016 5:43 PM
Answers
All replies
-
User-691209617 posted
add this line LoadDBContext and check
public DbSet<Poroduct> Products { get; set; }
you code for LoadDBContext will become like this
using Microsoft.EntityFrameworkCore; using MyCompany.MyApp.Models; namespace MyCompany.MyApp.Data { public class LoanDbContext : DbContext { public LoanDbContext(DbContextOptions<LoanDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // Customize the ASP.NET Identity model and override the defaults if needed. // For example, you can rename the ASP.NET Identity table names and more. // Add your customizations after calling base.OnModelCreating(builder); } public DbSet<LoanRequest> LoanRequests { get; set; } public DbSet<Poroduct> Products { get; set; } } }
hope this helps
Wednesday, December 21, 2016 5:55 PM -
User-2097536512 posted
Thanks for the suggestion. I forgot to mention I tried this already, but I gave it a try again just in case. Same error being returned.
I even tried
public DbSet<Product> Product { get; set; }
in case it was picky about "Products" vs "Product". Neither worked.
Wednesday, December 21, 2016 6:02 PM -
-
User-2097536512 posted
Missing Migrations was indeed the problem! I had planned to hold off on creating Migrations until I had completed all of the scaffolding. Strange how only the one Model class was affected by this; I was able to Scaffold four other classes just fine.
I ended up having to run the following command, because Enable-Migrations is obsolesced in Core 1.1 and the next "Add-Migration" command doesn't yet support .csproj projects.
dotnet ef migrations add FirstVersion --context LoanDbContext -o Data\Migrations
Appreciate the help!
Wednesday, December 21, 2016 8:41 PM