当使用基类和私有setter时,关键组件“Id”不是类型“TypeName”上的声明属性 [英] The key component 'Id' is not a declared property on type 'TypeName', when using base class and private setter

查看:157
本文介绍了当使用基类和私有setter时,关键组件“Id”不是类型“TypeName”上的声明属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的实体的抽象基类,这是不是任何表映射:

I want to use an abstract base class for entities, which is not mapped on any table:

public abstract class Entity
{
    public virtual int Id { get; private set; }
}



由于编号将自动递增,我不想让这个属性从外部改变。因此,它的制定者是私人

Since Id will be auto-increment, I don't want to allow to change this property from outside. Hence, its setter is private.

下面是一个示例实体类型:

Here's a sample entity type:

public class Order : Entity
{
    public virtual string Customer { get; set; }
}



...配置类型:

...configuration types:

public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity : Entity
{
    public EntityConfiguration()
    {
        HasKey(o => o.Id);
        Property(o => o.Id).HasColumnName("id").HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

public class OrderConfiguration : EntityConfiguration<Order>
{
    public OrderConfiguration()
    {
        Property(o => o.Customer).HasColumnName("customer");
        ToTable("Customers");
    }
}



...和上下文:

...and context:

public class Context : DbContext
{
    public Context()
        : base()
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Configurations.Add(new OrderConfiguration());
    }

    public DbSet<Order> Orders { get; set; }
}

现在,当我试图查询订单是这样的:

Now, when I'm trying to query orders like this:

        using (var context = new Context())
        {
            foreach (var order in context.Orders)
            {
                Console.WriteLine(order.Customer);
            }
        }



我得到一个例外:

I'm getting an exception:

的关键组成部分ID不上键入订单声明的属性。
验证它尚未明确从模型和
,这是一个有效的基本属性排除。

The key component 'Id' is not a declared property on type 'Order'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

我读过的SO几个问题,发现,我的方法看起来是正确的。然后,我修改基类,一点点,并提出编号公共的setter方法:

I've read several questions at SO, and found, that my approach looks correct. Then, I've modified base class a little and made Id with public setter:

public abstract class Entity
{
    public virtual int Id { get; set; }
}

和(这是一个奇迹!)示例代码工作正常。此外,它工作正常不带底座实体类(当编号定义顺序)与私人的制定者。

And (it's a miracle!) the sample code works fine. Also, it works fine without base Entity class (when Id is defined in Order) with private setter.

逻辑告诉我,那是EF的错误行为。结果
但是,可能是我M失去了一些东西?

Logic tells me, that is a buggy behavior of EF.
But, may be, I'm missing something?

推荐答案

EF喜欢访问所有的钥匙。尝试使用保护使组件可访问ID,但外部不能。似乎是为EF队给我一个合理的问题。

EF likes to access all keys. Try using protected so the assembly can access the ID but externals cant. Seems like a reasonable question for the EF team to me.

请参阅相关的岗位的是否实体框架代码首先支持只读导航属性

这篇关于当使用基类和私有setter时,关键组件“Id”不是类型“TypeName”上的声明属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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