写入 DataContext
我有一个功能。此函数采用 IEnumerable
(Customer
是一个实体)。该函数需要做的是告诉 DataContext
(它有一个 Customers
集合作为属性)它的 Customers
属性需要被覆盖将此值传入 IEnumerable
。我无法使用分配,因为无法分配 DomainContext.Customers
,因为它是只读的。
我想我不清楚我在问什么,所以我想我应该说......我该怎么做?因此,我们有 DataContext.Customers(类型为 System.Data.Linq.Table),希望将其替换为 System.Collections.Generic.IEnumerable。我不能只将后者分配给前者,因为 DataContext 的属性是只读的。但一定有办法。
编辑:这是一个图像:
进一步编辑 :是的,此图像不包含“客户”类型的集合,而是“连接”类型的集合。不过没关系,它们都是从链接的 SQL 数据库中的表创建的。所以有一个 dc.Connections、一个 dc.Customers、一个 dc.Media 等等。
提前致谢。
I have a function. This function takes an IEnumerable<Customer>
(Customer
being an entity). What the function needs to do is tell the DataContext
(which has a collection of Customers
as a property) that its Customers
property needs to be overwritten with this passed in IEnumerable<Customer>
. I can't use assignment because DomainContext.Customers
cannot be assigned to, as it is read only.
I guess it's not clear what I'm asking, so I suppose I should say... how do I do that? So we have DataContext.Customers (of type System.Data.Linq.Table) which wants to be replaced with a System.Collections.Generic.IEnumerable. I can't just assign the latter to the former because DataContext's properties are read only. But there must be a way.
Edit: here's an image:
Further edit: Yes, this image does not feature a collection of the type 'Customer' but rather 'Connection'. It doesn't matter though, they are both created from tables within the linked SQL database. So there is a dc.Connections, a dc.Customers, a dc.Media and so on.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的收藏是只读的。当您的视图状态中有一个接口或序列化集合的其他位置时,可能会发生这种情况。如果它被反序列化,它是只读的,因为它没有接口及其功能的实现。
Your collection IsReadonly. This can happen when you have an Interface in you viewstate, or an other place which serializes the collection. If it is deserialized it is readonly because it does not have a implementation of the interface its functions.
我假设您想要更新上下文中与 IEnumerable 中的实体具有相同
ID
的所有Customer
实体,您可以使用类似的方法这个答案:https://stackoverflow.com/a/4212581/559144
I assume you want to
UPDATE
allCustomer
entities in the context having sameID
of the entities in your IEnumerable, you can use an approach like in this answer:https://stackoverflow.com/a/4212581/559144
这很正常,大多数时候 Collections 应该是只读的。
您只需清除集合,然后使用 foreach 添加每个新项目:
It's normal, Collections should be readonly most of the time.
You just have to clear the collection, then add every new item using a foreach:
不是
DataContext.SubmitChanges()
吗?答案版本 (对于那些努力看出这个反问句是建议答案的人):
它是
DataContext.SubmitChanges()
Is it not
DataContext.SubmitChanges()
??Answer version (for those struggling to see that this rhetorical question was a proposed answer):
It's
DataContext.SubmitChanges()