EF 4.1 Code First - 忽略抽象基类的属性 [英] EF 4.1 Code First - Ignore property of abstract base class

查看:120
本文介绍了EF 4.1 Code First - 忽略抽象基类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的模式,其中有许多表具有公共列定义,我想在我的Code First实体模型中使用抽象基类对其进行建模。

I have an existing schema with many tables that have common column definitions and I want to use an abstract base class to model this in my Code First entity model.

在大多数情况下,这可以正常工作,而不必做任何特殊的事情,但有些表格有不同的列类型,例如。表示布尔值的列的位与字节类型。在这种情况下,我在
派生类中创建了一个附加属性,我正在映射,我希望EF Code First Model忽略抽象基类上的属性。

In most cases this works fine without having to do anything special, but some of the tables have varying column types, eg. bit versus byte types for a column that represents a boolean value. In this cases, I have created an additional property in the derived class which I am mapping and I want the EF Code First Model to ignore the property on the abstract base class.

但是,在基类属性上使用Fluent API中的Ignore方法没有任何效果。当我查询实体时,EF抛出异常:

However, using the Ignore method in the Fluent API on the base class property is not having any effect. When I query the entity, the EF throws an exception:

测试方法Tests.TestDataAccess.MyEntityQuery引发异常:
$
System.Data.EntityCommandExecutionException:发生错误在执行命令定义时。有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:无效的列名'IsActive'。

Test method Tests.TestDataAccess.MyEntityQuery threw exception:
System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'IsActive'.

以下是代码的摘录:


  public abstract class BaseEntity
  {
    public virtual int InstanceId { get; set; }
    public virtual string Name { get; set; }
    public virtual bool IsActive { get; set; }
  }

  public class MyEntity : BaseEntity
  {
    public override bool IsActive { get { return Convert.ToBoolean(ActiveByte); } set { ActiveByte = Convert.ToByte(value); } }

    public virtual byte ActiveByte { get; set; }
  }

  internal class MyEntityConfiguration : EntityTypeConfiguration<MyEntity>
  {
    public MyEntityConfiguration()
    {
      this.ToTable("MyEntities");
      this.HasKey(e => e.InstanceId);
      this.Property(e => e.InstanceId).HasColumnName("MyEntityId").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
      this.Property(e => e.Name).HasColumnName("sName");
      this.Ignore(e => e.IsActive);
      this.Property(e => e.ActiveByte).HasColumnName("bActive");
    }
  }

推荐答案

嗨格雷厄姆,

欢迎!

根据您的描述,子类的IsActive已经覆盖了supclass的属性,如果您想忽略IsActive属性,可以在其上添加NotMapped属性:

According to your description, the subclass's IsActive has overide the supclass's property, If you want to ignore the IsActive property, you can add NotMapped attribution on it:


 public abstract class BaseEntity
  {  [Key]
    public virtual int InstanceId { get; set; }
    public virtual string Name { get; set; }
    public virtual bool IsActive { get; set; }
  }

  public class MyEntity : BaseEntity
  {
    [NotMapped]
    public override bool IsActive { get { return Convert.ToBoolean(ActiveByte); } set { ActiveByte = Convert.ToByte(value); } }

    public virtual byte ActiveByte { get; set; }
  }
  public class MyContext : DbContext
  {
    public DbSet<MyEntity> MyEntities { get; set; }
  }


这篇关于EF 4.1 Code First - 忽略抽象基类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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