实体类型不是当前上下文模型的一部分

发布于 2024-10-17 16:29:04 字数 1054 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-10-24 16:29:04

显式添加“DatabaseGenerate”属性以设置数据库中列的“identity”值

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

指定十进制数据类型的精度。这是因为默认情况下,它假定十进制数据类型的小数点后有两个数字。我们需要将其设置为0。

modelBuilder.Entity<User>().Property(x => x.ID).HasPrecision(16, 0);

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

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

Specify 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.

modelBuilder.Entity<User>().Property(x => x.ID).HasPrecision(16, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文