For the present context, the entity type classname> is not included in the model.

ef-code-first entity-framework

Question

Table in DBPackagingInfo . I possessPackage a class andShopEntities : 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 launching the_db analyzing its context andPackages Noticed are a property and an exception:

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

Update

Since the issue also arises in a Model-first approach, where the table mapping is completed in the EDMX file rather than the annotation noted above, I have updated this question and asked for its revisiting.

The display window for the modelPackage Each property is correctly mapped to the table column in both the Model and Store entity types, as seen by the entity's Table Mapping. The annotation code-first technique does the same mapping.

1
28
11/28/2012 7:24:49 PM

Popular Answer

Clearly include the“DatabaseGenerated” a property that sets the“identity” value of the database column

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

Give the decimal data type's precision. This is due to the fact that for decimal data types, it automatically believes there are two integers after the decimal. We must set it to 0.

modelBuilder.Entity<User>().Property(x => x.ID).HasPrecision(16, 0);
1
1/31/2013 6:51:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow