日期:2014-05-20  浏览次数:20952 次

linq to sql 插入数据问题
先看看代码
C# code

 private void button1_Click(object sender, EventArgs e)
        {
            UserRolesDataConText urdct = new UserRolesDataConText();
            UserRoles u = new UserRoles() { Name = "菜鸟" };
            urdct.UserRoles.InsertOnSubmit(u);
            urdct.SubmitChanges();
            MessageBox.Show("添加成功");
        }

user类
C# code

 [Table(Name = "UserInfo")]
    public class Users
    {
        private int userId;
        [Column(IsPrimaryKey = true)]
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private string userName;
        [Column]
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
        private string userPsw;
        [Column]
        public string UserPsw
        {
            get { return userPsw; }
            set { userPsw = value; }
        }
        private int userRoleID;
        [Column]
        public int UserRoleID
        {
            get { return userRoleID; }
            set { userRoleID = value; }
        }
        private EntityRef<UserRoles> userRole;
        [Association(Storage = "userRole", ThisKey = "UserRoleID")]
        public UserRoles UserRole
        {
            get { return this.userRole.Entity; }
            set { this.userRole.Entity = value; }
        }

        private string depID;
        [Column]
        public string DepID
        {
            get { return depID; }
            set { depID = value; }
        }


        private string speID;
        [Column]
        public string SpeID
        {
            get { return speID; }
            set { speID = value; }
        }
        private string subID;
        [Column]
        public string SubID
        {
            get { return subID; }
            set { subID = value; }
        }
        public Users() { }
        
    }
    /// <summary>
    /// 强类型DataConText
    /// </summary>
    class UserDataContext : DataContext
    {
        public Table<Users> User;
        public UserDataContext() : base("Data Source=.;Initial Catalog=ExamQuestionsBase;User ID=sa;pwd=123") { }

    }



userRoles类
C# code

 [Table(Name = "UserRoles")]
    public class UserRoles
    {
        private int userRoleID;
        [Column(IsPrimaryKey = true)]
        public int UserRoleID
        {
            get { return userRoleID; }
            set { userRoleID = value; }
        }
        private EntitySet<Users> users;
        [Association(Storage="users",OtherKey="UserRoleID")]
        public EntitySet<Users> Users
        {
            get { return this.users; }
            set { this.users.Assign(value); }
        }
        private string name;
        [Column]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public UserRoles() { }
        public UserRoles(string name)
        {
            this.Name = name;
        }

    }
    /// <summary>
    /// 强类型DataConText
    /// </summary>
    class UserRolesDataConText : DataContext
    {
        public Table<UserRoles> UserRoles;
         public UserRolesDataConText() : base("Data Source=.;Initial Catalog=ExamQuestionsBase;User ID=sa;pwd=123") { }
    }




问题是:执行到 urdct.UserRoles.InsertOnSubmit(u);这句的时候提示错误“未将对象引用设置到对象的实例”,求解决!!!

------解决方案--------------------