写入 DataContext

发布于 2024-12-19 17:26:53 字数 765 浏览 2 评论 0原文

我有一个功能。此函数采用 IEnumerableCustomer 是一个实体)。该函数需要做的是告诉 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小糖芽 2024-12-26 17:26:53

您的收藏是只读的。当您的视图状态中有一个接口或序列化集合的其他位置时,可能会发生这种情况。如果它被反序列化,它是只读的,因为它没有接口及其功能的实现。

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.

甜是你 2024-12-26 17:26:53

我假设您想要更新上下文中与 IEnumerable 中的实体具有相同 ID 的所有 Customer 实体,您可以使用类似的方法这个答案:

https://stackoverflow.com/a/4212581/559144

I assume you want to UPDATE all Customer entities in the context having same ID of the entities in your IEnumerable, you can use an approach like in this answer:

https://stackoverflow.com/a/4212581/559144

梦年海沫深 2024-12-26 17:26:53

这很正常,大多数时候 Collections 应该是只读的。

您只需清除集合,然后使用 foreach 添加每个新项目:

private void SetItems(IEnumerable<MyObject> _NewItems)
{
    myCollection.Clear();
    foreach (MyObject newItem in _NewItems)
    {
        myCollection.Add(newItem);
    }
}

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:

private void SetItems(IEnumerable<MyObject> _NewItems)
{
    myCollection.Clear();
    foreach (MyObject newItem in _NewItems)
    {
        myCollection.Add(newItem);
    }
}
栀子花开つ 2024-12-26 17:26:53

不是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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文