表不接受外键 [英] Table not accepting foreign key

查看:56
本文介绍了表不接受外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

s_reg,s_don是我的数据库中的两个表reg_id是s_reg的主键,而don_id是s_don

i已经使用两个表的reg_id加入了两个表

但是在s_don表中,reg_id列只取零并且没有显示s_reg的实际reg_id



我尝试了什么:



s_reg,s_don are two tables in my database reg_id is primary key of s_reg and don_id is of s_don
i have joined two tables using reg_id of both the tables
but in s_don table the reg_id column taking only zero and not showing the actual reg_id of s_reg

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
{
    using (studentDataContext dt = new studentDataContext())
    {
        var log = (from s in db.s_dons
                   join m in db.s_regs on s.reg_id equals m.reg_id
                   select new
                   {
                       s,m
                   }).FirstOrDefault();
        try
        {
            s_reg reg = new s_reg();
            s_don ureg = new s_don();
            ureg.dtype = DropDownList2.SelectedItem.Text;
            ureg.dtitle = DropDownList1.SelectedItem.Text;
            ureg.dnmame = TextBox1.Text;
            ureg.ddate = TextBox2.Text;
            ureg.demail = TextBox5.Text;
            ureg.dmob = TextBox6.Text;
            ureg.dadd = TextBox7.Text;
            ureg.ddepart = ddlTest.SelectedItem.Text;
            ureg.damt = TextBox3.Text;
            ureg.dcom = TextBox4.Text;
            dt.s_dons.InsertOnSubmit(ureg);
            dt.SubmitChanges();


            var frps = dt.s_dons.Where(w => w.dnmame == TextBox1.Text);//update column
            foreach (var ti in frps)
            {
                ureg.reg_id = reg.reg_id;
                dt.SubmitChanges();
            }

            /*reg.smobile = TextBox6.Text;
            dt.s_regs.InsertOnSubmit(reg);
            dt.SubmitChanges();
            Response.Write("<script>alert('Submited successfuly');</script>");
            */

        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.Message);
        }
    }
     }





s_reg:



s_reg:

CREATE TABLE [dbo].[s_reg](
[reg_id] [int] IDENTITY(2000,1) NOT NULL,
[pass_id] [int] NULL,
[sname] [nvarchar](50) NULL,
[smobile] [nvarchar](50) NULL,
[semail] [nvarchar](50) NULL,
[sdept] [nvarchar](50) NULL,
[scateg] [nvarchar](50) NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_reg] PRIMARY KEY CLUSTERED
(
[reg_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]



s_log:


s_log:

CREATE TABLE [dbo].[s_log](
[log_id] [int] IDENTITY(1000,1) NOT NULL,
[reg_id] [int] NULL,
[susnm] [nvarchar](50) NULL,
[spass] [nvarchar](50) NULL,
CONSTRAINT [PK_s_log] PRIMARY KEY CLUSTERED
(
[log_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

推荐答案

据我所知 reg_id on s_log 未定义为外键。最有可能的原因是,尚未为您的模型创建适当的属性。另一件事是,如果我没记错的话,s_reg应该有一个包含子实体的集合,以便在插入时可以使用新生成的密钥。



所以尝试将外键定义到数据库中并更新模型。另请参阅 LINQ to SQL对象模型| Microsoft Docs [ ^ ]
As far as I can see the reg_id on s_log is not defined as a foreign key. Most likely because of this, proper attributes have not been created for your model. Another thing is, if I recall correctly, the s_reg should have a collection containing the child entities so that upon insert the newly generated key can be used.

So try to define the foreign key into the database and update the model. Also have a look at The LINQ to SQL Object Model | Microsoft Docs[^]


这篇关于表不接受外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆