简体   繁体   English

实体框架:无法定义1:1关系

[英]Entity framework: Unable to define 1:1 relationship

I'd like to define relationship where Student can have only one favorite Course . 我想定义一种关系,即学生只能拥有一个喜欢的课程 I expect it would look like this in DB: 我希望它在数据库中看起来像这样:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name

How to achieve this with entity framework? 如何使用实体框架实现这一目标? I'd prefer to specify it just by attributes. 我宁愿仅通过属性来指定它。 I tried: 我试过了:

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

    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseID { get; set; }
}

public class Class
{
    public int ID { get; set; }
    public string Name { get; set; }
}

which gave me this DB model: 这给了我这个数据库模型:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name
    StudentID // how to remove this?

Note, that it may happen that several students have the same favorite class and therefore this is unacceptable solution. 注意,可能有几个学生有相同的最喜欢的课程,因此这是不可接受的解决方案。

Another question: what type of relationship this is? 另一个问题:这是什么类型的关系? (1:1 / 1:N ?) (1:1/1:N?)

To specify 1 to 1 relationship, it is assumed, that primary key for the related entity matches the primary key of first entity. 为了指定一对一关系,假定相关实体的主键与第一个实体的主键匹配。 Also you should specify a virtual property to related entity: 另外,您应该为相关实体指定virtual属性:

public class Student
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }

    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseID { get; set; }
}

public class Class
{
    [Key]
    [ForeignKey("Student")]
    public int ID { get; set; }
    public string Name { get; set; }

    public virtual Student Student { get; set; }
}

And it will be one-to-zero-or-one relationship. 这将是one-to-zero-or-one关系。 Check this tutorial . 检查本教程

If you will mark FavouriteCourse property with RequiredAttribute , it seems, that it will result in strong one to one relationship. 如果将FavouriteCourse属性标记为RequiredAttribute ,那么看起来将形成牢固的一对一关系。

It will result in adequate database structure: 这将导致适当的数据库结构:

STUDENT
    ID    
    Name
    FavoriteCourseID
COURSE
    ID
    Name

However, if many students could have one favourite course, this structure will be a problem, as you want one-to-many instead of one-to-one. 但是,如果许多学生可以选择一门最喜欢的课程,那么这种结构将是一个问题,因为您想要一对多而不是一对一。 And you will have a duplicate records in database, because one course can refer only to one student. 您将在数据库中有重复的记录,因为一门课程只能指代一名学生。 You have to think about your db design. 您必须考虑您的数据库设计。

You can try this: 您可以尝试以下方法:

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

    [ForeignKey("FavoriteCourseId")]
    public Course FavoriteCourse { get; set; }
    public int? FavoriteCourseId { get; set; }
}

Normally, you define one of the following relations: 通常,您定义以下关系之一:

  1. Optional:Optional 可选:可选
  2. Required:Optional 必需:可选
  3. Optional:Many 可选:很多
  4. Required:Many 需要:很多
  5. Many:Many 很多很多

Having Required:Required is not a usual relation, inserting the first entry with such a relation needs special treatment. 具有Required:Required不是通常的关系,插入具有此类关系的第一个条目需要特殊处理。

I Suppose you want Required:Many as in "Each student has one favorite course but many students may chose the same favorite course". 我假设您要求:许多与“每个学生都有一门最喜欢的课程,但许多学生可能选择同一门最喜欢的课程”中的很多相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM