实体类型不是当前上下文模型的一部分
DB 有一个表PackagingInfo
。我有一个 Package
类和一个 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);
}
实例化 _db
上下文并检查其 Packages
属性后,注意到异常:
The entity type Package is not part of the model for the current context.
更新
我已编辑此问题并请求重新打开它,因为这种情况也首先发生在模型中在 EDMX 文件中完成表映射的方法,而不是在此处注意到的注释:
模型浏览器窗口显示 bot 中的 Package
模型和存储实体类型,并且实体的表映射显示正确映射的每个属性到表列。这与注释代码优先样式完成的映射相同。
DB has a table PackagingInfo
. I have a Package
class, and a 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 instantiating the _db
context and inspecting its Packages
property and exception is noticed:
The entity type Package is not part of the model for the current context.
Update
I've edited this question and requested its reopening because the situation is also occuring in a Model first approach where the table mapping is done in the EDMX file instead of the annotation noticed here:
Model Browser window shows the Package
in bot the Model and Store entity types, and the entity's Table Mapping shows each property properly mapped to the table column. This is the same mapping accomplished by the annotation code-first style.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显式添加“DatabaseGenerate”属性以设置数据库中列的“identity”值
指定十进制数据类型的精度。这是因为默认情况下,它假定十进制数据类型的小数点后有两个数字。我们需要将其设置为0。
Explicitly add the
“DatabaseGenerated”
attribute to set the“identity”
value of the column in databaseSpecify the precision for the decimal data type. This is because by default it assumes there are two numbers after the decimal for decimal data type. We need to set it 0.