Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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- 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 is auto Generate but at first another column was primary key and then i created Id column and change primary key.

thank you.
Posted

1 solution

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.

C#
DbType="UniqueIdentifier NOT NULL",
IsPrimaryKey=true


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

C#
[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.
 
Share this answer
 
Comments
Prasad_Kulkarni 31-May-12 3:17am    
Nice explanation +5!
Sandeep Mewara 31-May-12 3:53am    
5!
parisa heidari 31-May-12 4:03am    
thanks Rajesh but when i did it the name of dataContext class change to Element automatically why?
and i get this error:
The type or namespace name 'HwMangementDataContext' could not be found (are you missing a using directive or an assembly reference?)
parisa heidari 31-May-12 15:21pm    
thanks Rajesh your suggestions was very useful they solved my problems.At first Saving changes in DB was not permited so i remove my DB and recreate it and Id column turns to identity.
Rajesh Kariyavula 31-May-12 23:39pm    
Its good to hear that your problem got solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900