告诉EF 6忽略私有财产 [英] Telling EF 6 to Ignore a Private Property

查看:50
本文介绍了告诉EF 6忽略私有财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 6.0.2将一些简单的手工编码模型映射到现有的数据库结构。目前的主要模型是:

I'm using Entity Framework 6.0.2 to map some simple hand-coded models to an existing database structure. The primary model at the moment is:

public class Occurrence
{
    public int ID { get; set; }
    public Guid LegacyID { get; set; }
    public string Note { get; set; }
    public virtual ICollection<OccurrenceHistory> History { get; set; }
}

OccurrenceHistory 模型与此无关,但是那部分工作正常,因此EF可以加载该模型的子记录。)

(The OccurrenceHistory model isn't really relevant to this, but that part is working fine whereby EF loads up the child records for this model.)

映射很简单,我尝试尽可能地明确(由于应用程序的增长,将出现一些不太直观的映射):

The mapping is simple, and I try to be as explicit as I can be (since as the application grows there will be some less-intuitive mapping):

public class OccurrenceMap : EntityTypeConfiguration<Occurrence>
{
    public OccurrenceMap()
    {
        ToTable("Occurrence");
        HasKey(o => o.ID);
        Property(o => o.ID).IsRequired().HasColumnName("ID");
        Property(o => o.LegacyID).IsRequired().HasColumnName("LegacyID");
        Property(o => o.Note).IsUnicode().IsOptional().HasColumnName("Note");
    }
}

但是如果我在模型中添加私有财产, EF尝试将其映射到数据库。像这样:

But if I add a private property to the model, EF tries to map it to the database. Something like this:

private OccurrenceHistory CurrentHistory { get; set; }

(在模型内部,我有一些逻辑可以维护该字段以及其他私有操作。 )当EF生成 SELECT 语句时,它最终会查找名为 CurrentHistory_ID 的列,该列当然不存在。

(Internal to the model I would have some logic for maintaining that field, for other private operations.) When EF generates a SELECT statement it ends up looking for a column called CurrentHistory_ID which of course doesn't exist.

我可以将该属性设为公开,并设置映射以将其忽略:

I can make the property public and set the mapping to ignore it:

Ignore(o => o.CurrentHistory);

但我不希望该物业公开。该模型将在内部跟踪一些应用程序代码不应该看到的信息。

But I don't want the property to be public. The model is going to internally track some information which the application code shouldn't see.

是否可以让EF忽略所有私有成员?即使是基于每个地图?我特别想这样做,而不必在模型本身上添加EF数据注释,因为这不仅有点漏水的抽象(对持久性无知的模型将具有持久性信息),但这也意味着持有模型的域核心程序集会随处引用 EntityFramework.dll ,这是不理想的。

Is there a way to tell EF to just ignore any and all private members? Even if it's on a per-map basis? I'd particularly like to do this without having to add EF data annotations to the models themselves, since that would not only be a bit of a leaky abstraction (persistence-ignorant models would then have persistence information on them) but it would also mean that the domain core assembly which holds the models would carry a reference to EntityFramework.dll everywhere it goes, which isn't ideal.

推荐答案

同事向我指出一篇博客文章导致了一种非常实用的方法。

A colleague pointed me to a blog post that led to a very practical approach.

所以我拥有的是私有财产:

So what I have is a private property:

private OccurrenceHistory CurrentHistory { get; set; }

问题的核心是我不能在映射中使用它:

The core of the problem is that I can't use that in my mapping:

Ignore(o => o.CurrentHistory);

因为显然,该属性是私有的,因此在这种情况下无法访问。博客文章的建议是公开一个引用私有属性的公共静态表达式:

Because, clearly, the property is private and can't be accessed in this context. What the blog post suggests is exposing a public static expression which references the private property:

private OccurrenceHistory CurrentHistory { get; set; }
public static readonly Expression<Func<Occurrence, OccurrenceHistory>> CurrentHistoryExpression = o => o.CurrentHistory;

然后我可以在映射中引用那个

I can then reference that in the mapping:

Ignore(Occurrence.CurrentHistoryExpression);

与其他任何事物一样,它既有优点也有缺点。但是在这种情况下,我认为利弊远大于利弊。

As with anything, it's a mix of pros and cons. But in this case I think the pros far outweigh the cons.

利弊:


  • 域核心程序集不需要携带对 EntityFramework.dll 的引用。

  • 持久性映射完全封装在

  • The domain core assembly doesn't need to carry a reference to EntityFramework.dll.
  • The persistence mapping is entirely encapsulated within the DAL assembly.

缺点:


  • 模型需要公开一些有关其内部工作原理的信息。

缺点是破坏了封装,但只是轻微的 。消费代码仍无法访问该属性或其在实例上的值,只能看到该属性静态存在。实际上,这没什么大不了的,因为开发人员仍然可以看到它。我觉得封装的精神仍然保留在该模型的任何给定实例上。

The con breaks encapsulation, but only slightly. Consuming code still can't access that property or its value on instances, it can only see that the property exists statically. Which, really, isn't a big deal, since developers can see it anyway. I feel that the spirit of encapsulation is still preserved on any given instance of the model.

这篇关于告诉EF 6忽略私有财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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