0

我有一个注册表格,允许学校注册。除了明显的登录名和一般详细信息外,学校还可以从他们拥有的设施和认证列表中进行选择。

我的数据显示可爱并正确绑定。

问题将数据输入链接器表不起作用,它会以我尝试过的两种不同方式引发错误:

方法1:

MembershipUser membershipUser = null;

if (schoolRegisterModel != null)
{
    if (null != DB)
    {
        school SchoolUser = new school();

        SchoolUser.username = schoolRegisterModel.UserName;
        SchoolUser.email = schoolRegisterModel.Email;

        string sPassowrdSalt = Security.Instance().CreateSalt();

        SchoolUser.password = Security.Instance().CreatePasswordHash(schoolRegisterModel.Password, sPassowrdSalt);

        SchoolUser.password_salt = sPassowrdSalt;

        ..More data etc..

        foreach (var item in schoolRegisterModel.Facilities)
        {
            if (item.@checked)
            {
                school_facility sf = new school_facility();
                sf.facility_id = item.facility_id;
                SchoolUser.school_facility.Add(sf);
            }
        }

        foreach (var item in schoolRegisterModel.Accreditations)
        {
            if (item.@checked)
            {
                school_accreditation sa = new school_accreditation();
                sa.accreditation_id = item.accreditation_id;
                SchoolUser.school_accreditation.Add(sa);
            }
        }

        DB.schools.Add(SchoolUser);
        DB.SaveChanges();

错误: {"INSERT 语句与 FOREIGN KEY 约束 \"FK_school_facility_facility\" 冲突。冲突发生在数据库 \"MYDB\"、表 \"dbo.facility\"、列 'facility_id' 中。\r\n该语句有已终止。"}

另外 - 我是否需要手动检索将基于此插入生成的即将成为学校的 ID。此方法避免仅使用主表(学校)将数据直接输入到链接器表中。

方法2:

除了尝试直接更新主表(学校)认证和设施集合之外,再次使用相同的代码,我使用上一个查询生成的最新主键分别手动更新链接器表,代码如下:

MembershipUsermembershipUser = null;

if (schoolRegisterModel != null)
{
    if (null != DB)
    {
        school SchoolUser = new school();

        SchoolUser.username = schoolRegisterModel.UserName;
        SchoolUser.email = schoolRegisterModel.Email;

        string sPassowrdSalt = Security.Instance().CreateSalt();

        SchoolUser.password = Security.Instance().CreatePasswordHash(schoolRegisterModel.Password, sPassowrdSalt);

        SchoolUser.password_salt = sPassowrdSalt;

        ..More data etc..

        // Linker data for facilities and accreditations.
        // Facilities
        foreach (var item in schoolRegisterModel.Facilities)
        {
            if (item.@checked)
            {
                school_facility sf = new school_facility();
                sf.facility_id = item.facility_id;
                sf.school_id = SchoolUser.school_id;
                DB.school_facility.Add(sf);
            }
        }

        // Accreditations
        foreach (var item in schoolRegisterModel.Accreditations)
        {
            if (item.@checked)
            {
                school_accreditation sa = new school_accreditation();
                sa.accreditation_id = item.accreditation_id;
                sa.school_id = SchoolUser.school_id;
                DB.school_accreditation.Add(sa);
            }
        }

        m_DB.SaveChanges(); 

错误: {"INSERT 语句与 FOREIGN KEY 约束 \"FK_school_facility_facility\" 冲突。冲突发生在数据库 \"MYDB\"、表 \"dbo.facility\"、列 'facility_id' 中。\r\n该语句有已终止。"}

如果你们知道我哪里出错了,请告诉我。似乎有更新链接器表日期的示例(无论如何我都会需要它)但找不到我的问题的示例......

提前致谢。

4

1 回答 1

0

看起来我找到了答案:

MVC:INSERT 语句与 FOREIGN KEY 约束冲突

我的数据基本上不包含正确的外键值(0 - 不存在),而且我的数据库正确地抛出了错误。很抱歉浪费时间阅读并感谢您的时间。我希望这可以帮助别人。

于 2013-02-07T11:58:27.847 回答