实体框架4,使用外键值插入而不是ID

发布于 2024-11-09 11:25:54 字数 388 浏览 0 评论 0原文

我确信这是一个非常基本的问题,但现在让我感到困惑:

我有 2 个表,学生和带有外键约束的课程,许多学生到 1 个课程。

我在学生表中存储了 CourseId_FK 引用,因此当我创建学生时,我如何能够执行类似的操作?

Students student = new Students();
student.Name = Bob;
student.Course.CourseName = "Geography";
db.SaveChanges();

现在,上述方法不起作用,我必须解决

student.CourseId = 22;

任何人都可以帮助我吗?

谢谢

大卫

This is pretty basic question I am sure but it's baffling me now :S

I have 2 tables, Students and Courses with a foreign key constraint, Many Students to 1 Course.

I store in my Students table a CourseId_FK reference, so when I am creating a Student how would I be able to do something like this?

Students student = new Students();
student.Name = Bob;
student.Course.CourseName = "Geography";
db.SaveChanges();

Right now, the above doesn't work and I have to resolve to

student.CourseId = 22;

Can anyone help me pls?

Thanks

David

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-11-16 11:25:54

如果您不想加载完整的课程实体:

Student student = new Student();
student.Name = Bob;
student.CourseId = db.Courses
                     .Where(c => c.CourseName == "Geography")
                     .Select(c => c.CourseId).First();
db.SaveChanges();

这只会从数据库中获取地理课程的Id(如果没有的话会崩溃)。

If you don't want to load the full course entity:

Student student = new Student();
student.Name = Bob;
student.CourseId = db.Courses
                     .Where(c => c.CourseName == "Geography")
                     .Select(c => c.CourseId).First();
db.SaveChanges();

This would only fetch the Id of the Geography course from DB (or crash when there isn't any).

独闯女儿国 2024-11-16 11:25:54

查看您的实体,“学生”是否具有“课程”(实际上:课程)属性?

对于您想要执行的操作,您的实体可能看起来像这样:

public class Student
{
    public string Name {get; set;}

    public virtual ICollection<Course> Courses {get; set;}
}

然后您需要执行以下操作:

Student student = new Student();
student.Name = Bob;
student.Courses.Add(db.Courses.Where(c => c.CourseName == "Geography"));

编辑:如果学生只能上一门课程,则修改 Student< /code> 类,所以它看起来像这样:

public class Student
{
    public int StudentId {get; set;}
    public string Name {get; set;}
    public int CourseId {get; set;}
    public virtual Course Course {get; set;}
}

那么你的代码看起来像这样:

student.Course = db.Courses.Where(c => c.CourseName == "Geography"));

你的代码发生了什么(我期望)是你只有 CourseId 所以当你这样你就可以' t 为您的学生分配实际的课程对象。

Looking at your entities, does "Student" have a "Course" (Really: Courses) property?

Your entity should probably look something like this, for what you want to do:

public class Student
{
    public string Name {get; set;}

    public virtual ICollection<Course> Courses {get; set;}
}

Then you'd do something like:

Student student = new Student();
student.Name = Bob;
student.Courses.Add(db.Courses.Where(c => c.CourseName == "Geography"));

Edit: If the Student can only have one course, you modify the Student class so it looks like this:

public class Student
{
    public int StudentId {get; set;}
    public string Name {get; set;}
    public int CourseId {get; set;}
    public virtual Course Course {get; set;}
}

Then your code looks like this:

student.Course = db.Courses.Where(c => c.CourseName == "Geography"));

What's going on with your code (I expect) is that you only have the CourseId so when you're so you can't assign an actual course object to your student.

落花随流水 2024-11-16 11:25:54

我会做这样的事情:

Student student = new Student();
student.Name = Bob;
student.Course = db.Courses.First(c => c.CourseName == "Geography");
db.SaveChanges();

更新:答案是关于学生有零门或一门课程时的情况(如提供的代码中所示)。

I'd do something like this:

Student student = new Student();
student.Name = Bob;
student.Course = db.Courses.First(c => c.CourseName == "Geography");
db.SaveChanges();

Update: the answer is about the situation when a student has zero or one course (as in the provided code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文