C#开发中发生“服务器无法生成类型为“Char(20)”的主键列。”

来源:互联网 发布:外部js文件怎么写 编辑:程序博客网 时间:2024/05/16 03:52

我使用的是VStudio.net 2008 写C#程序,现在项目的sql2005数据库服务器上有一个表 t_users,其中 userid  char(20) not null 为

主键,现在在c#中通过linq to sql 向数据库插入一条记录,如下:

   /*系统用户信息表_Users*/
create table T_Users (
  UserID              char(20)            not null,
  UserName            char(40)              not null,
  UserPWD              char(32)            null,
  UserState            int  default 1      not null,/*0:禁止,1:正常*/
  CreateTime          datetime    default getdate()      not null,
  constraint PK_T_USERS primary key nonclustered (UserID)
)
go

执行的代码如下:
        T_Users uu;
            uu=new T_Users()
            {
            UserID=textBox1.Text.Trim(),//="liu"
            UserName = textBox2.Text, //="test"
            UserPWD = textBox4.Text,//="123456"
            UserState=1 ,
            CreateTime=System.DateTime.Today};


        pmdc.T_Users.InsertOnSubmit(uu);
               
          pmdc.SubmitChanges();

(其中pmdc为DataContext对象)
执行到 pmdc.SubmitChanges()会弹出如下错误:

 
服务器无法生成类型为“Char(20)”的主键列。
为什么不能生成userid列呢,不知道如何解决!
详细错误信息如下:
未处理 System.NotSupportedException
  Message="服务器无法生成类型为“Char(20)”的主键列。 "
  Source="System.Data.Linq"
  StackTrace:
       在 System.Data.Linq.SqlClient.QueryConverter.GetIdentityExpression(MetaDataMember id, Boolean isOutputFromInsert)
       在 System.Data.Linq.SqlClient.QueryConverter.VisitInsert(Expression item, LambdaExpression resultSelector)
       在 System.Data.Linq.SqlClient.QueryConverter.VisitDataManipulationCall(MethodCallExpression mc)
       在 System.Data.Linq.SqlClient.QueryConverter.VisitInner(Expression node)
       在 System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(Expression node)
       在 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations)
       在 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
       在 System.Data.Linq.ChangeDirector.StandardChangeDirector.DynamicInsert(TrackedObject item)
       在 System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
       在 System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
       在 System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
       在 Zetasoft.Permission.ZetaPermissionController.Insert_Users(T_Users user) 位置 D:/dotnetWork/Zeta.敏捷软件工厂/Zetasoft/Zetasoft/ZetaPermissionController.cs:行号 160
       在 test.Form1.button2_Click(Object sender, EventArgs e) 位置 D:/dotnetWork/Zeta.敏捷软件工厂/Zetasoft/test/Form1.cs:行号 76
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 test.Program.Main() 位置 D:/dotnetWork/Zeta.敏捷软件工厂/Zetasoft/test/Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException:
原创粉丝点击