Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
C# 当我尝试更新父实体时,实体框架正在更新子实体_C#_Entity Framework_Entity Framework 6 - Fatal编程技术网

C# 当我尝试更新父实体时,实体框架正在更新子实体

C# 当我尝试更新父实体时,实体框架正在更新子实体,c#,entity-framework,entity-framework-6,C#,Entity Framework,Entity Framework 6,我有一个映射到产品表的产品实体: [Table("Product")] public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get'; set; } } 我还有一个映射到CombinedProductView的CombinedProduct实体,请注意,此实体仅用于读取,此视图不可更新: [Table("Co

我有一个映射到产品表的
产品
实体:

[Table("Product")]
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } 
    public decimal Price { get'; set; }
}
我还有一个映射到CombinedProductView的
CombinedProduct
实体,请注意,此实体仅用于读取,此视图不可更新:

[Table("CombinedProductView")]
public class CombinedProduct : Product
{
    public string Store { get; set; } 
}
这是我的
MyDbContext

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyDbContext: DbContext
{
    public MyDbContext() : base("MyDB")
    {
    }

    public DbSet<Product> Product { get; set; }
    public DbSet<CombinedProduct> CombinedProduct { get; set; }
}
我想将某些产品的价格提高5美元,因此我得到了
产品的列表,并提高了价格:

var products = _productRepository.GetProductsMoreExpensiveThan(100);
foreach(var p in products)
{
    p.Price += 5;
}

_productRepository.Update(products);

这会引发异常,因为EF正在尝试更新不可更新的
CombinedProductView
。为什么实体框架在更新基本实体
Product
时试图更新派生实体(
CombinedProduct
)?

我认为混淆来自于
Product
的基类上有一个
[表(“产品”)]
属性,然后派生类还有一个属性

您可以在EF中的对象类型中使用基类,但是,该基类通常不表示表


关于如何实现这一点,这里有很多方法需要展开,因此,与其尝试将其扩展到一个疯狂的细节层次,本文应该有助于澄清这一点,因为它提供了有关表名等基本信息的详细信息。

GetProductsCheaperThan()的定义是什么
?context.Purchase如何退货?什么是上下文。购买?ProductRepository的修饰符为
Public
,它是如何编译的?@Caius jard,很抱歉这是一个打字错误。。。纠正
var products = _productRepository.GetProductsMoreExpensiveThan(100);
foreach(var p in products)
{
    p.Price += 5;
}

_productRepository.Update(products);