C# 实体框架CTP4代码优先:映射受保护的属性

C# 实体框架CTP4代码优先:映射受保护的属性,c#,entity-framework-4,code-first,C#,Entity Framework 4,Code First,我希望在模型上使用延迟加载集合,但我希望通过单独的方法来完成添加/删除功能。比如说: class Model { protected virtual ICollection<Something> _somethings { get; set; } public IEnumerable<Something> Somethings { get { return _somethings; } } public void AddSomethi

我希望在模型上使用延迟加载集合,但我希望通过单独的方法来完成添加/删除功能。比如说:

class Model
{
  protected virtual ICollection<Something> _somethings { get; set; }

  public IEnumerable<Something> Somethings 
  { 
    get { return _somethings; } 
  }

  public void AddSomething(Something thingToAdd)
  {
    /*  logic */
    _somethings.Add(thingToAdd);
  }
}
类模型
{
受保护的虚拟ICollection\u somethings{get;set;}
公共数不清的东西
{ 
获取{return\u somethings;}
}
公共空间添加某物(添加某物)
{
/*逻辑*/
_添加(要添加的内容);
}
}

我不知道如何为此配置映射。我研究了如何使用配置类:EntityConfiguration。但由于该属性受保护,我无法确定如何在其上设置配置。有什么方法可以完成我在这里要做的吗?

我想如果您在模型类中声明配置类(继承EntityConfiguration),它就可以工作了。这不是一个很好的解决方案,因为通常不鼓励子类化,但这是我唯一能想到的事情。

我听说只有使用EDMX文件方式才能做到这一点。。不要先编写代码。

您可以使用只读静态表达式访问受保护的属性,如下所示

protected virtual ICollection<Something> _somesing { get; set; }
public static readonly Expression<Func<Model, ICollection<Something>>> Expression = p => p._something;

public IReadOnlyCollection<Something> Something
{
     return _sumething.AsReadOnly();
}
protectedvirtualicollection\u somesing{get;set;}
公共静态只读表达式Expression=p=>p.\u something;
公共iReadOnly收集某物
{
返回_sumething.AsReadOnly();
}
并在DbContext类中的OnModelCreating方法中使用它来映射受保护的属性

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Model>().HasMany<Something>(Model.Expression);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasMany(Model.Expression);
}

需要存储库工作吗?我想你和我一样也有类似的问题: