zoukankan      html  css  js  c++  java
  • LINQ to SQL 外键约束的插入及获取主表标识列等问题

    主表YearJobCenter,从表YearJobIndex

    插入部分代码,3个按钮事件分别为单独的插入主表、从表,和级联插入中从表:

    值得注意的是,我开始的代码犯了一个低级错误

    从表获取主表标识列ID不能用jobindex.Id = jdc.YearJobCenters.Select(c => c.Id).Single();

    表中并非1条数据,当然会抛异常sequence contains more than one element。

    正确:jobindex.Id = jdc.YearJobCenters.Max(p => p.Id);

      protected void btnInsertJobindex_Click(object sender, EventArgs e)
            {                
                try
                {
                    using (StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("logIndex.txt"), false))
                    {                   
                        sw.WriteLine(DateTime.Now);
                        sw.WriteLine("---job index---");
                        jdc.Log = sw;
                      
                        YearJobIndex jobindex = new YearJobIndex();
                        /*jobindex.Id = jdc.YearJobCenters.Select(c => c.Id).Single();will throw exception: sequence contains more than one element */
                        jobindex.Id = jdc.YearJobCenters.Max(p => p.Id);
                        ScriptManager.RegisterClientScriptBlock(this.Button1, this.Button1.GetType(), "asfasfxjs", string.Format("alert('Select for DB\\r\\nLast JobCenter ID :{0}');", jobindex.Id), true);
                        jobindex.SchoolId =int.Parse( txtSchoolId.Text);
                        jobindex.Uid = Guid.NewGuid();
                        jobindex.Status = 0;
                        jobindex.IsLocked = false;
                        jdc.YearJobIndexes.InsertOnSubmit(jobindex);
                        jdc.SubmitChanges();
                        BindIndexData();
                    }               
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
    
            protected void btnInsertJobcenter_Click(object sender, EventArgs e)
            {
    
    
                try
                {
                    using (StreamWriter sw = new StreamWriter(Server.MapPath("logCenter.txt"), false))
                    {
                        sw.WriteLine(DateTime.Now);
                        sw.WriteLine("---job center---");
                        jdc.Log = sw;
    
                        YearJobCenter jobcenter = new YearJobCenter();
                        
                    
                        jobcenter.SchoolId = int.Parse(txtSchoolId.Text);
                        jobcenter.JobNumber = txtJobNumber.Text;
                        jobcenter.BookTypeEstimatated = 1;
                        jobcenter.OnlineImageCounter = 1;
                        jobcenter.OfflineImageCounter = 0;
    
                        jdc.YearJobCenters.InsertOnSubmit(jobcenter);
    
                        jdc.SubmitChanges();
    
                        string alertText = string.Format("alert('Increase Successful! \\r\\nLast JobCenter ID :{0}');", jobcenter.Id);
    
                        ScriptManager.RegisterClientScriptBlock(this.Button1, this.Button1.GetType(), "asfasfxjs", alertText, true);
                        BindCenterData();
    
                    }
                   
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
    
            protected void btnInsertCascade(object sender, EventArgs e)
            {
                try
                {
                    using (StreamWriter sw = new StreamWriter(Server.MapPath("logCenter.txt"), false))
                    {
                        sw.WriteLine(DateTime.Now);
                        sw.WriteLine("---job center and job index---");
                        jdc.Log = sw;
    
                        using (TransactionScope scope = new TransactionScope())
                        {
                            YearJobCenter jobcenter = new YearJobCenter();
                            jobcenter.SchoolId = int.Parse(txtSchoolId.Text);
                            jobcenter.JobNumber = txtJobNumber.Text;
                            jobcenter.BookTypeEstimatated = 1;
                            jobcenter.OnlineImageCounter = 1;
                            jobcenter.OfflineImageCounter = 0;
    
                            YearJobIndex jobindex = new YearJobIndex();
                            jobindex.SchoolId = jobcenter.SchoolId;
                            jobindex.Uid = Guid.NewGuid();
                            jobindex.Status = 0;
                            jobindex.IsLocked = false;
                            jobcenter.YearJobIndexes=jobindex;
    
                            jdc.YearJobCenters.InsertOnSubmit(jobcenter);
                            jdc.SubmitChanges();
                            
                            /*
                            YearJobIndex jobindex = new YearJobIndex();
                            jobindex.Id = jobcenter.Id;              
                            jobindex.SchoolId = jobcenter.SchoolId;
                            jobindex.Uid = Guid.NewGuid();
                            jobindex.Status = 0;
                            jobindex.IsLocked = false;
                            jdc.YearJobIndexes.InsertOnSubmit(jobindex);
                            jdc.SubmitChanges();
                            */
    
    
                            string alertText = string.Format("alert('Insert Cascade Successful!');");
                            ScriptManager.RegisterClientScriptBlock(this.Button1, this.Button1.GetType(), "asfasfxjs", alertText, true);
                            this.GridView1.DataSource = jdc.YearJobCenters.OrderBy(p => p.Id);
                            this.GridView1.DataBind();
                            this.GridView2.DataSource = jdc.YearJobIndexes.OrderBy(p => p.Id);
                            this.GridView2.DataBind();
                        }
                        
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
    
            }
    
  • 相关阅读:
    redis参数AOF参数的bug
    tidb损坏tikv节点怎么恢复集群
    mysql主从延时临时解决办法
    python脚本批量杀死redis链接
    pt-online-schema-change 脚本化
    mysql查看锁等信息SQL
    mongo复制集脑裂问题如何处理
    日志收集及网络包收集方案
    各浏览器下载文件名不乱码的解决办法
    java 中 byte[]、File、InputStream 互相转换
  • 原文地址:https://www.cnblogs.com/lutzmark/p/1680676.html
Copyright © 2011-2022 走看看