在Entity Framework 4.1中映射受保护的属性 [英] Mapping protected properties in Entity Framework 4.1

查看:93
本文介绍了在Entity Framework 4.1中映射受保护的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个类中有一个受保护的列表,可通过方法访问. 在本主题中,建议我可以在模型类中创建一个配置类,虽然我不喜欢它,但是只要它能正常工作,就可以了.

public class Person
    {
        public int Id { set; get; }
        public string Name { set; get; }

        List<string> _address = new List<string>();

        protected virtual ICollection<string> address { get { return this._address; } }

        [NotMapped]
        public string[] Address { get { return this.address.ToArray(); } }  // I know it's not efficient.. I'll cache it.

        public void AddAddress(string address)
        {
            // some business logic
            this.address.Add(address);
        }

        public class OneMapping : EntityTypeConfiguration<Person>
        {
            public OneMapping()
            {
                this.HasMany(x => x.address);
            }
        }
    }

和在数据库上下文中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add<Person>(new Person.OneMapping());
   base.OnModelCreating(modelBuilder);
}

当我尝试添加person实例时,db上下文引发此异常:

导航属性地址"不是类型上的声明属性 '人'.确认没有明确排除在外 模型,并且它是有效的导航属性.

所以实际上不起作用. 尝试过以下示例:解决方案

您的导航属性没有设置器,因此无法映射.所有映射的属性都必须具有可访问的getter和setter,因为EF可以在实体实现过程中对其进行分配.

如果您想使用映射属性的可访问性,通常首先使用EF代码不是很好的工具.

顺便说一句.您的地址不是实体集合,而是字符串集合-也不受支持

I want to have a protected list in a class, accessible via methods. In this topic it's suggested that I could create a configuration class inside my model class, which I don't really like, but as long as it works.. So I have something along those lines:

public class Person
    {
        public int Id { set; get; }
        public string Name { set; get; }

        List<string> _address = new List<string>();

        protected virtual ICollection<string> address { get { return this._address; } }

        [NotMapped]
        public string[] Address { get { return this.address.ToArray(); } }  // I know it's not efficient.. I'll cache it.

        public void AddAddress(string address)
        {
            // some business logic
            this.address.Add(address);
        }

        public class OneMapping : EntityTypeConfiguration<Person>
        {
            public OneMapping()
            {
                this.HasMany(x => x.address);
            }
        }
    }

and in db context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Configurations.Add<Person>(new Person.OneMapping());
   base.OnModelCreating(modelBuilder);
}

The db context throws this exception when I'm trying to add an instance of person:

The navigation property 'address' is not a declared property on type 'Person'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.

So doesn't really work. Tried this sample: Mapping private properties with EF 4.1 Failed with same error.

Another solution would be to use ObservableCollection, and wire the business logic in events, but this logic doesn't need to run when objects are constructed by EF - only when they're constructed by user.. which isn't possible.

So I'm somewhat stuck here.. I hope someone out there already ran into this and solved it..

Thanks !

解决方案

Your navigation property doesn't have setter so it cannot be mapped. All mapped properties must have accessible getter and setter because EF can assign them during entity materialization. That was true for EDMX where at least private setter was necessary but in case of code first it works even without setter.

Generally EF code first is not very good tool if you want to play with accessibility of mapped properties.

Edit:

Btw. Your address is not collection of entities but collection of strings - that is also not supported

这篇关于在Entity Framework 4.1中映射受保护的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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