简体   繁体   English

如何使用C#中的对象初始化程序初始化包装在getter / setter中的EntitySet?

[英]How do I initialize an EntitySet wrapped in a getter/setter using object initializers in C#?

I am adapting the Windows Phone Database Example using MVVM for my own app. 我正在为自己的应用程序改编使用MVVMWindows Phone数据库示例 Here it suggests wrapping the EntitySet properties on the model objects with a getter/setter combination that allows assignment using an IEnumerable (using the Assign method) eg 在这里,建议使用允许使用IEnumerable (使用Assign方法)进行分配的getter / setter组合将EntitySet属性包装在模型对象上。

// Example method from a model class 'SomeModelObject'
[Association(Storage = "_todos", OtherKey = "_categoryId", ThisKey = "Id")]
public EntitySet<ToDoItem> ToDos
{
    get { return this._todos; }
    set { this._todos.Assign(value); }
}

However when I try and instantiate an object that has an EntitySet property it will not allow it, eg 但是,当我尝试实例化具有EntitySet属性的对象时,它将不允许它,例如

SomeModelObject myModelObject = new SomeModelObject() {
    Property1 = "foo",
    Property2 = true,
    // Following raises an error, even though setter should allow assignment
    // from an IEnumerable (because of the use of 'Assign' in the setter)
    ToDos = new List<ToDoItem>() {
        new ToDoItem(),
    },
};

The error is as follows, 错误如下,

Error    1   Cannot implicitly convert type 
'System.Collections.Generic.List<SomeApp.ToDoItem>' to 
'System.Data.Linq.EntitySet<SomeApp.ToDoItem>'

How do I instantiate objects referenced from an EntitySet ? 如何实例化从EntitySet引用的对象?

Well, there are two problems with the code you've posted. 好了,您发布的代码有两个问题。

I suspect the main problem is that you're trying to set the value of the property to a List<ToDoItem> when the type of the property is EntitySet<ToDoItem> . 我怀疑主要问题是当属性的类型为EntitySet<ToDoItem>时,您正在尝试将属性的值设置为List<ToDoItem> EntitySet<ToDoItem> (The error message you've posted doesn't match the code you've posted, but never mind.) You say it "should allow assignment from an IEnumerable , but I don't know why you'd expect that to be the case, given the declaration of the property: (您发布的错误消息与您发布的代码不匹配,但是没关系。)您说它“应该允许IEnumerable进行赋值,但是我不知道为什么您希望它成为鉴于属性的声明:

public EntitySet<ToDoItem> ToDos { ... }

The property is not of type IEnumerable<ToDoItem> . 该属性不是 IEnumerable<ToDoItem>类型。

You're also using new SomeModelObject twice for some reason: 由于某种原因,您两次使用了new SomeModelObject

SomeModelObject myModelObject = new SomeModelObject() {
    new SomeModelObject() {
        Property1 = "foo",
        ...
    }
};

I'll assume your real code looks more like this: 我假设您的真实代码看起来像这样:

SomeModelObject myModelObject = new SomeModelObject() {
  Property1 = "foo",
  ... 
};

Please be more careful in future - it's very hard to diagnose code that isn't posted. 以后请多加小心-诊断未发布的代码非常困难。

There are three options around the EntitySet . EntitySet周围有三个选项。

If you want to add new items to an existing EntitySet<ToDoItem> you can do that within the object initializer: 如果要将新项目添加到现有的 EntitySet<ToDoItem> ,可以在对象初始化器中进行以下操作:

SomeModelObject myModelObject = new SomeModelObject {
  Property1 = "foo",
  Property2 = true,
  ToDos = {
    new ToDoItem(),
  }
};

Alternatively, if you want to set the value of the property itself, you need to create a new EntitySet<ToDoItem> : 另外,如果要设置属性本身的值,则需要创建一个新的EntitySet<ToDoItem>

SomeModelObject myModelObject = new SomeModelObject {
  Property1 = "foo",
  Property2 = true,
  ToDos = new EntitySet<ToDoItem> {
    new ToDoItem(),
  }
};

This seems unlikely to be a good idea though, and I'd actually suggest making the setter private. 不过,这似乎不太可能是个好主意,我实际上建议将设置者设为私有。

Alternatively, if you really want to be able to assign any IEnumerable<ToDoItem> , you can just change the type of the property: 另外,如果您确实希望能够分配任何IEnumerable<ToDoItem> ,则只需更改属性的类型即可:

[Association(Storage = "_todos", OtherKey = "_categoryId", ThisKey = "Id")]
public IEnumerable<ToDoItem> ToDos
{
    get { return this._todos; }
    set { this._todos.Assign(value); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM