将派生类型映射到EF中的同一个表 [英] Map derived type to the same table in EF

查看:74
本文介绍了将派生类型映射到EF中的同一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下简单的代码示例说明了有关的场景。我有一个Person实体,它简单地映射到DB中的Person表。我正在使用默认的实体对象代码生成器。

The following simple code example illustrates the scenario in question. I have a Person entity, which is simply mapped to the Person table in the DB. I am using the default Entity Object code generator.

public partial class Person { ... }

我有一个派生自Person的PersonDetail类,其中包含一些额外的属性,这是GUI所需要的。这些属性都不需要保留在数据库中,所以我不想将这个类映射到数据库。

I have a PersonDetail class deriving from Person, which contains some extra properties, which are needed by the GUI. None of these properties needs to be persisted in the DB, so I do not want to map this class to the database.

public class PersonDetail : Person
{
    public int TheGuiNeedsThisInformation { get; set; }
}

我发送了具有WCF的GUI类的Detail类的实例。
我的问题是,如果GUI修改PersonDetail实例的某些属性,并将其发送回服务器进行更新,我不能简单地将其附加到我的上下文,因为派生类未映射到任何表(EntityType PersonDetail找不到映射和元数​​据信息)。我试图将它映射到与Person类映射的相同的表中,但是EF会以这种方式抛出一些关于映射的异常。

And I send down instances of the Detail class the GUI with WCF. My problem is, if the GUI modifies some properties of the PersonDetail instance, and sends it back to the server to update it, I can not simply Attach it to my context, because the derived class is not mapped to any table ("Mapping and metadata information could not be found for EntityType PersonDetail"). I tried to map it to the same table as the Person class is mapped, but the EF throws some exception about the mapping that way.

我设法通过创建通过将PersonDetail实例的属性值复制到新的Person实例,然后将其保存到DB中。不过,我想避免额外的工作。我究竟做错了什么?这不可能吗在Linq到SQL中,这个工作就像一个魅力,基本上是开箱即用的。

I managed to workaround this by creating a copy of the PersonDetail instance by copying its property values to a new Person instance, and then saving it to the DB. However I would like to avoid that extra work. What am I doing wrong? Is this not possible? In Linq to SQL this worked like a charm, basically out of the box.

如果这不可能,建议的方法是用一些扩展我的实体类额外的信息,不必持续吗? (明显的方法是简单地向部分Person类添加额外的属性,但是我不想这样做,因为GUI无法确定额外的属性是否已经在服务器上被填充。)

And if this is not possible, what is the suggested way to extend my entity classes with some extra information, which don't have to be persisted? (The obvious way would be to simply add extra properties to the partial Person class. However, i do not want to do that, because that way the GUI can never be sure whether the extra properties have been filled on the server or not.)

更新:感谢您的建议,但我的主要问题仍然是开放的:是否可能有一个基类和派生类(派生类没有任何必须保存到DB中的附加属性)映射到同一个表,以便我可以简单地将派生类型的实例附加到我的上下文中,并将其保存为基础类型的实例?我不能这样做,我很惊讶,因为Linq到SQL它只是工作。

UPDATE: Thanks for the suggestions, however my main question is still open: Is it possible to have a base and a derived class (where the derived class does not have any additional properties which have to be saved to the DB) mapped to the same table, so that I can simply attach an instance of the derived type to my context and save it like if it was an instance of the base type? I could not do this yet, and I am surprised, because with Linq to SQL it simply worked.

推荐答案

你可以添加你的属性到类和忽略他们为实体框架使用modelBuilder,看起来像你做代码开发。对于该选项,可以看看modelBuilder =>

You can add your properties to the class and "ignore" them for the entity framework using the modelBuilder, looks like you doing code first development. For that options have a look at the modelBuilder =>

    public class Db : DbContext
    {
      public DbSet<MyClass> MyClasses{ get; set; }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
        modelBuilder.Entity<MyClass>().Ignore(x => x.MyProperty);
      }
    }

希望这有帮助!

这篇关于将派生类型映射到EF中的同一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆