简体   繁体   English

在C#中类之间的引用?

[英]Reference between classes in C#?

I'm taking an introduction course in databases, and am working on the first hand-in. 我正在上数据库入门课程,并且正在进行首次上手学习。 One of the questions is to "add a reference in the class User so it can acces a users Organization" I have a User class that looks like this: 问题之一是“在User类中添加引用,这样它就可以访问用户组织”我有一个User类,如下所示:

 public class User
    {
        [Key]
        public string Username { get; set; }
        public string DisplayName { get; set; }
    }

And my organization class looks like this: 我的组织类如下所示:

public class Organization
    {
        public int OrganizationId { get; set; }
        public string OrganizationName { get; set; }
    }

I don't understand the question stated by my teacher -- what does it mean to add a reference, when both classes are declared in the same .cs file? 我不明白老师提出的问题-当两个类都在同一个.cs文件中声明时,添加引用是什么意思?

I think the question refers to foreign key. 我认为该问题涉及外键。 It is something like "User is part of Organization". 它类似于“用户是组织的一部分”。

Example: 例:

 public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }

    //Foreign key for Standard
    public int StandardId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }

    public ICollection<Student> Students { get; set; }
}

Please refer to the following page for more information about foreign key in Entity framework: http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx 有关实体框架中外键的更多信息,请参考以下页面: http : //www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

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

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