Entity Framework CTP4 Code First: Mapping protected properties

吃可爱长大的小学妹 提交于 2019-12-05 10:01:11

You can use readonly static Expression for access to protected property like this

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();
}

And use it in OnModelCreating method in DbContext class for mapping protected property

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Model>().HasMany<Something>(Model.Expression);
}

I suppose if you declare the configuration class (inheriting EntityConfiguration) inside your Model class, it could work. It's not a nice solution, since subclassing is generally discouraged, but it's the only thing I can think of.

Ive heard this only can be done using EDMX file way.. not code first.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!