是否需要在插入时自动同步? [英] Does Auto Sync need to be On Insert ?

查看:70
本文介绍了是否需要在插入时自动同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


1-我怕了dataContext,并且在dbml中的所有类中都遇到了此错误.
我应该在插入时将自动同步"设置为吗?

Hi ,
1- I creared a dataContext and i get this error for all classes in dbml.
should i set Auto Sync to on insert?

DBML1060: The AutoSync attribute of the Column element 'Id' of the Type element 'Term' needs to be 'OnInsert' when IsDbGenerated and IsPrimaryKey are 'True'.	



2-当我想将一个对象插入一个类时,出现此错误:



2- when i want to insert an object to one class i get this error :

Cannot insert the value NULL into column 'Id', table 'HomeWorkSystem.dbo.CourseGroup'; column does not allow nulls. INSERT fails.
The statement has been terminated.




id是自动生成的,但起初另一列是主键,然后我创建了ID列并更改了主键.

谢谢.




id is auto Generate but at first another column was primary key and then i created Id column and change primary key.

thank you.

推荐答案

第一个问题:

如果将DBML文件中的[column]属性设置为IsDbGenerated = true,则我们指定该列为表中的标识列,这意味着无论何时执行插入操作,DataContext都必须获取当前ID.要保存的记录.为了执行此操作,我们必须将AutoSync属性设置为"AutoSync = AutoSync.OnInsert".

对于第二个问题:

将主键从一个属性更改为另一个属性后,将删除以下所述的属性.如果未移除,请移除它们.

For the first problem:

If the [column] property in your DBML file is set as IsDbGenerated = true then we are specifying that the column is an identity column in the table, it means that when ever you are performing insert operation your DataContext has to fetch the current ID for the record to be saved. And to perform this operation we have to set the AutoSync property as "AutoSync=AutoSync.OnInsert".

For the Second problem:

When you have changed the primary key from one property to another property have the properties like mentioned below are removed. If not removed please remove them.

DbType="UniqueIdentifier NOT NULL",
IsPrimaryKey=true



并检查ID列是否如下定义,因为它是自动生成的



And check whether ID column is defined as mentioned below as it is auto generated

[Column(Storage="columnName",
        AutoSync=AutoSync.OnInsert,
        DbType="UniqueIdentifier NOT NULL",
        IsPrimaryKey=true, IsDbGenerated=true)]



希望这可以帮助.如果您仍然有疑虑,请提及我会尽力解决.



Hope this helps. If you still have concern, please mention I''ll try to resolve them.


这篇关于是否需要在插入时自动同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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