【问题标题】:C# code for Insert data to a table with ReferenceKey using Entity Mode [closed]使用实体模式将数据插入带有 ReferenceKey 的表的 C# 代码
【发布时间】:2013-03-26 14:26:54
【问题描述】:

谁能告诉我如何插入数据到

Table Student has columns {stuId Name Age}
Table School has columns {classId Year sName}

sName”是学生表中的引用键

我想使用实体模型 C# 插入数据

【问题讨论】:

  • 您的数据库设计看起来不太好。您在“学校”表中有“sName”吗?
  • 你是说sName 是从Student 链接到Name 的吗?

标签: c# wpf entity-framework-4 entity


【解决方案1】:

试试这个添加学生:

Student student = new Student(){studId="", Name="", Age=""};
myEntities.Students.AddObject(student);
myEntities.SaveChanges();

添加学校:

School school = new School(){classId="", Year = year, sName=""};
myEntities.Schools.AddObject(school);
myEntities.SaveChanges();

我希望这会有所帮助。

【讨论】:

  • 其实我需要在学校表中添加数据?哪个有学生表的参考?
【解决方案2】:
                String SchooID = getNewID();
                Schools schl = new Schools();
                schl.school_reference = SchooID;
                schl.school_name = "Ananda Collage";

                schl.StudentReference.Value = cecbContext.Students.First(i => i.stud_name == "Josh");


                cecbContext.AddToSchools(schl);
                cecbContext.SaveChanges();

我通过这种方式找到了答案。这对我很成功。非常感谢你们的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 2012-02-08
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多