The <classname> object type is not part of the model for the current context.

DB has a PackagingInfo table. I have a Package class and ShopEntities : DbContext .

 // Entity (ex. Package.cs) [Table("PackagingInfo")] public class Package { public decimal PackageID { get; set; } public decimal Title { get; set; } public decimal Cost { get; set; } public bool isFree { get; set; } } // Entity Context (ex. ShopEntities.cs) public class ShopEntities : DbContext { public DbSet<Package> Packages { get; set; } } // Controller Action (ex. HomeController.cs) public ActionResult Index() { ShopEntities _db = new ShopEntities(); var q = _db.Packages.ToList(); return View(q); } 

After creating the _db context _db and checking its properties, Packages and exceptions noticed:

 The entity type Package is not part of the model for the current context. 

Update

I edited this question and requested to reopen it, because the situation also arises in the first approach of the model, where the table is displayed in the EDMX file instead of the note noted here:

In the "Browser Model" window, the types of the "Model" and "Save" objects are displayed in the bot, and the mapping of object tables displays each property that is correctly displayed in the table column. This is the same mapping done in code style with annotation.

+28
entity-framework ef-code-first
Feb 13 2018-11-11T00:
source share
1 answer

Explicitly add the "DatabaseGenerated" attribute to set the "identity" value of the column in the database

 [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)] 

Specify the precision for the decimal data type. This is because by default it is assumed that after the decimal number for the decimal data type, there are two numbers. We need to set it to 0.

 modelBuilder.Entity<User>().Property(x => x.ID).HasPrecision(16, 0); 
+1
Jan 31 '13 at 6:51
source share



All Articles