由于主键,在保存SQL文件时出现问题. [英] Problem in saving in SQL file because of primary key.

查看:129
本文介绍了由于主键,在保存SQL文件时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


使用SQL保存文件存在一个具有挑战性的问题.
我有一张桌子和一张桌子:

Hi,
There is a challenging problem with saving files is SQL.
I have a table and a list:

public Table<TableBuildResource> tableBuildResource = null;
public List<TableBuildResource> listBuildResource = null;


加载sql数据后,我将表转换为列表.
为了保存,我必须将列表转换为表格.但是,主键始终存在问题,因为尽管删除了表记录,但是从列表中再次添加这些记录被认为是错误的. 由于主键,列表中没有冗余.但是保存仍然存在问题.


After I load sql data, I convert table to list.
For saving, I have to convert list to table. But, always there is problem with primary key, because although table records are deleted, adding those records again from list are assumed as error. The is no redundancy in the list due to primary key. but still there is problem with saving.

private void SaveBuildResource()
{
    var DelList = from c in tableBuildResource
                  select c;
    tableBuildResource.DeleteAllOnSubmit(DelList);
    foreach (TableBuildResource t in listBuildResource)
        tableBuildResource.InsertOnSubmit(t);
    SubmitChanges();
}







我在C#中使用SQL-CE和linq-to-sql.

我该怎么办?








I use SQL-CE and linq-to-sql in C#.

What should I do?


Eddy Vluggen写道:
Eddy Vluggen wrote:

您必须迭代表列表,更新所有更改的记录.

You''d have to iterate your Table-List, updating every changed record.


它减慢了程序的速度.表慢于列表.它还需要反射(因为我的程序有许多必须保存在sql文件中的类,因此适用于所有类).同样,它需要嵌套在另一个循环中以检查行是否存在或是否为新行(对于长数据库而言太慢了.至少要进行10 ^ 6次检查!)

我想知道,对于忽略SQL来说,覆盖行是否如此必要吗?
即使SQL同意删除所有行并忘记它们,我也可以解决问题.但是,这似乎会使人烦恼.
当我搜索并没有发现覆盖行的示例时,这更有趣.在所有示例中,程序员仅定义了一个新行并添加了手动数据,然后插入了该行.然后保存并有时使用linq-to-sql的复杂灵活性.但是我没有找到简单的实际样本.好像在开玩笑.

我要找的东西很简单.为什么Linq表具有这种不友好的方式?


It slows down the speed of program. Tables are much slower that Lists. Also it needs reflection(to work for all classes because my program has many classes that has to be saved in sql file). Also, it needs to a loop nested in another to check if a row exists or is new(too much slow for a long database. At least 10^6 checks!)

I wonder, if overwriting a row is so dispensable for SQL to be neglected?
Also I can solve the problem even if SQL consented to delete all rows and forgot them. But as if it is going to annoy people.
It is more funny when I googled and found no example of overwriting a row. In all samples the programmer just defined a new row and added manual data and then inserted the row. then saved and sometimes used complex flexibilities of linq-to-sql. But I found no simple practical sample. It looks like a joke.

The thing I am looking for is so easy. Why Linq Tables have such unfriendly manner?

推荐答案

aasser写道:

用于保存,我必须将列表转换为表格.但是,主键始终存在问题,因为尽管删除了表记录,但仍将从列表中再次添加这些记录视为错误.

For saving, I have to convert list to table. But, always there is problem with primary key, because although table records are deleted, adding those records again from list are assumed as error.


这些记录将被视为新记录",并且与其他记录的所有关联都将丢失-因为它们仍指向原始PK值.

您必须迭代表列表,更新所有更改的记录.


Those would be considered "new records", and all associations with other records would be lost - because they are still pointing to the original PK-value.

You''d have to iterate your Table-List, updating every changed record.


这篇关于由于主键,在保存SQL文件时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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