惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - LutzMark

charles4抓https请求的注意事项,补充iphone7(ios10系统)无法解密ssl问题 XCode设置自己windows习惯的快捷键(比如Home、End键) MVC项目中ExecutionTimeout不生效的解决方案 Delegate与Event的区别 坑爹的MSN登录错误80072745 Linq的sum函数InvalidOperationException异常解决办法 LINQ to SQL自定义映射表关系(1:N or 1:1) Flex中的 for in 与 for each in - LutzMark Flex的DataGrid将水平分隔线设为虚线 - LutzMark - 博客园 解决Flex的DataGrid控件中ItemRender随Scrollbar的滚动发生UI重绘问题 - LutzMark - 博客园 去除ColumnChart自带的阴影效果 - LutzMark - 博客园 IIS7中配置WebOrb支持RTMPT 使用DataAdpater自动批量更新DataSet中的数据到数据库 委托的Invoke 和 BeginInvoke 与Control的Invoke和BeginInvoke(转-因为写得很好) 控制台测试异步委托 一篇好文,以在迷茫时阅读 驳《IT开发工程师的悲哀》 钢板与表针 不用临时变量,只用11个字符交换两个变量的值——窥视C#编译原理的冰山一角
LINQ to SQL 外键约束的插入及获取主表标识列等问题
LutzMark · 2010-03-08 · via 博客园 - LutzMark

主表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());
            }

        }