Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何防止实体框架将类映射到数据库表_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

Asp.net 如何防止实体框架将类映射到数据库表

Asp.net 如何防止实体框架将类映射到数据库表,asp.net,asp.net-mvc,entity-framework,Asp.net,Asp.net Mvc,Entity Framework,我创建了一个viewmodel,它有一个对实体类型的引用 以下是我的viewmodel: namespace SuperMart.Areas.Admin.ViewModels { [NotMapped] public class ProductDetailVM { public int ProductID { get; set; } [Display(Name = "Name")] public string Product

我创建了一个viewmodel,它有一个对实体类型的引用

以下是我的viewmodel:

namespace SuperMart.Areas.Admin.ViewModels
{
    [NotMapped]
    public class ProductDetailVM
    {
        public int ProductID { get; set; }
        [Display(Name = "Name")]
        public string ProductName { get; set; }
        [Display(Name = "Price")]
        public double Price { get; set; }
        [Display(Name = "Description")]
        public string Description { get; set; }

        [Display(Name = "Sub Category")]
        public string SubCat { get; set; }

        [Display(Name = "Category")]
        public string Category { get; set; }

        [Display(Name = "Status")]
        public bool Available { get; set; }

        [Display(Name = "Date Created")]
        public DateTime? DateCreated { get; set; }
        public virtual ICollection<ProductImages> ProductImages { get; set; }
    }
} 
namespace SuperMart.Entities
{
    public class ProductImages
    {
        [Key]
        public int ImageID { get; set; }

        public byte[] ImageContent { get; set; }
        public string ImageContentType { get; set; }
        public string ImageName { get; set; }
        public FileType FileType { get; set; }

        [ForeignKey("Product")]
        public int ProductID { get; set; }

        public virtual Product Product { get; set; }
    }
}
我用
[NotMapped]
属性标记了这个类,表示我不希望它是一个数据库表,因为它只是一个视图模型,而不是一个实体类型。但是,当我运行addmigration命令时,EF仍试图将其创建为表,这会导致外键冲突。我也尝试了
modelBuilder.Ignore
,但一种或另一种EF似乎忽略了该代码,因为它仍在以表的形式创建类,如下所示

public override void Up()
{
    CreateTable(
                "dbo.ProductDetailVMs",
                c => new
                    {
                        ProductID = c.Int(nullable: false, identity: true),
                        ProductName = c.String(),
                        Price = c.Double(nullable: false),
                        Description = c.String(),
                        SubCat = c.String(),
                        Category = c.String(),
                        Available = c.Boolean(nullable: false),
                        DateCreated = c.DateTime(),
                    })
                .PrimaryKey(t => t.ProductID);

            AddForeignKey("dbo.ProductImages", "ProductID", "dbo.ProductDetailVMs", "ProductID", cascadeDelete: true);
        }

通常,要映射实体,需要在DbContext中包含DbSet属性马丁·利弗塞奇


好吧,结果我有点撞到自己的腿了。我发现该类存在于我的DbContext类中,因此存在映射问题。但我必须承认,我真的不知道它是怎么来的。无论如何,问题已经解决。

请向我们展示有问题的ViewModel类。@Serv我已经用我的代码更新了问题您的
DbContext
包含哪些实体?通常,要映射实体,您需要在
DbContext
中包含
DbSet
属性。我只在属性上看到了
[notmap]
。但是我找不到可靠的文档说明这是唯一有效的用法。回答我自己的评论:
表示属性或类应该从数据库映射中排除。
。我也遇到过这种情况。我右键单击ActionResult以添加视图,并选择了一个模板,该模板自动将相关类添加到DbContext。