试图将数据插入两个表得到错误 [英] trying to insert data to two tables get error

查看:62
本文介绍了试图将数据插入两个表得到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下错误!

I get this following Error!

Entities in 'knowitCVdbEntities.EMPLOYEE_LANGUAGES' participate in the 'FK_EMPLOYEE_LANGUAGES_EMPLOYEES1' relationship. 0 related 'EMPLOYEES' were found. 1 'EMPLOYEES' is expected. 





为了插入表格,我写了以下代码





For inserting in to the tables i have written following code

var db = new knowitCVdbEntities();

            var emp = new EMPLOYEE
            {

                firstname = TextBoxFirstName.Text,
                lastname = TextBoxLastName.Text,
                about = TextBoxAboutYou.Text,
                phone = TextBoxPhone.Text,
                position = TextBoxPosition.Text,
                email = TextBoxEmail.Text,
                username = TextBoxUsername.Text,
                date_of_birth = TextBoxDateOfBirth.Text,
                


                image = FileUploadPicture.FileName

            };

            db.AddToEMPLOYEES(emp);

            var emplanguages = new EMPLOYEE_LANGUAGES();
            {

                emplanguages.language_name = TextBoxLanguage.Text;
                

            }
            db.AddToEMPLOYEE_LANGUAGES(emplanguages);




            
            db.SaveChanges();

推荐答案

确定。

由于您在EMPLOYEES和EMPLOYEE_LANGUAGES之间存在1:N关系,因此语言记录引用了员工,您需要在向其添加语言之前让该员工。如果您的模型中的员工实体中有语言导航枚举(您应该),我建议您通过该语言添加语言,而不是直接添加 - 因此您不必担心引用。



[根据上述建议通过TeamViewer在OP网站上解决]



Ok.
Since you have an 1:N relationship between EMPLOYEES and EMPLOYEE_LANGUAGES, the language record is referencing the employee, you need to have the employee before you add the language to it. If you have the languages navigation enumeration in the employee entity in your model (you should), I suggest you add the language trough that one, and not directly - thus you don''t have to worry about the references.

[Solved according to the above suggestion at OP''s site via TeamViewer]

var emp = new EMPLOYEE
{
   firstname = TextBoxFirstName.Text,
   lastname = TextBoxLastName.Text,
   about = TextBoxAboutYou.Text,
   phone = TextBoxPhone.Text,
   position = TextBoxPosition.Text,
   email = TextBoxEmail.Text,
   username = TextBoxUsername.Text,
   date_of_birth = TextBoxDateOfBirth.Text,
   image = FileUploadPicture.FileName
};

var emplanguages = new EMPLOYEE_LANGUAGES
{
   emplanguages.language_name = TextBoxLanguage.Text;
}

emp.EMPLOYEE_LANGUAGES.Add(emplanguages)

db.AddToEMPLOYEES(emp);

db.SaveChanges();


这篇关于试图将数据插入两个表得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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