C# - 带联结表的 Linq 多对多

发布于 2024-11-05 08:39:09 字数 1948 浏览 0 评论 0原文

我刚刚开始使用 Linq,目前我已经准备好数据库并用 C# 制作了一些实体类。

现在,我的对象“Person”与“Profession”具有多对多关系。但每当我尝试调用下一个语句时,它都不起作用:

Context ctx = new Context(ConfigurationManager.ConnectionStrings["myContext"].ConnectionString);
var Julian = (from j in ctx.persons
                         where j.Name == "Julian"
                         select j).Single();
Profession proftest = new Profession() { Name = "Programmer", Accepted = true };
Julian.Professesions.Add(proftest);
ctx.SubmitChanges();

现在我将向您展示我正在使用的类:

Person 得到了接下来的几件事(类变量的其余部分有效,所以我将展示你是坏的部分):

// Data (for mapping)
private EntitySet<PersonProfession> personProfession;

public Person()
        {
            personProfession = new EntitySet<PersonProfession>();
        }


        [Association(Name = "FK_j_Person_Profession_Person", Storage = "personProfession", OtherKey = "personId", ThisKey = "Id")]
        internal ICollection<PersonProfession> PersonProfessions
        {
            get { return personProfession; }
            set { personProfession.Assign(value); }
        }
public ICollection<Profession> Professions
        {
            get { return (from prof in PersonProfessions where prof.Profession.Accepted==true select prof.Profession).ToList(); }
        }

现在我可以在“Person”类中成功调用以下方法:

public void AddProfession(Profession profession, Person person)
        {
            PersonProfession pp = new PersonProfession();
            pp.Person = person;
            pp.Profession = profession;
            personProfession.Add(pp);
        }

但是当然这不是我想要的。

数据关系是这样的:

Person --> j_Person_Profession <-- Profession

我也遇到了同样的问题(.Add() 不起作用),Person 和 Alias 之间的另一种关系(这是一个只有一个 int 的 PersonId 和一个 String 的别名表。


最后一点问题如下: 我的“职业”类也与“人”有相同的链接,所以每当我询问一个人的背景时,我可以查看他/她的职业,并且在该职业内有更多具有相同职业的人。对于大型数据库来说,这不是很慢吗?也许我应该删除反向链接?

I've just started using Linq and at the moment I've got a database ready and made some entity classes in C#.

Now my object called "Person" has a many-to-many relationship with "Profession." But whenever I try to call out the next statement it won't work:

Context ctx = new Context(ConfigurationManager.ConnectionStrings["myContext"].ConnectionString);
var Julian = (from j in ctx.persons
                         where j.Name == "Julian"
                         select j).Single();
Profession proftest = new Profession() { Name = "Programmer", Accepted = true };
Julian.Professesions.Add(proftest);
ctx.SubmitChanges();

Now I'll show you the classes that I'm working with:

Person got the next few things (the rest of the class variable works so I'll show you the bad part):

// Data (for mapping)
private EntitySet<PersonProfession> personProfession;

public Person()
        {
            personProfession = new EntitySet<PersonProfession>();
        }


        [Association(Name = "FK_j_Person_Profession_Person", Storage = "personProfession", OtherKey = "personId", ThisKey = "Id")]
        internal ICollection<PersonProfession> PersonProfessions
        {
            get { return personProfession; }
            set { personProfession.Assign(value); }
        }
public ICollection<Profession> Professions
        {
            get { return (from prof in PersonProfessions where prof.Profession.Accepted==true select prof.Profession).ToList(); }
        }

Now I am able to call the following method in the "Person" class with success:

public void AddProfession(Profession profession, Person person)
        {
            PersonProfession pp = new PersonProfession();
            pp.Person = person;
            pp.Profession = profession;
            personProfession.Add(pp);
        }

But ofcourse that's not what I want.

The data relation is like this:

Person --> j_Person_Profession <-- Profession

I've also got the same problem (that the .Add() wont work) with another relation between Person and Alias (which is a table with only an int for PersonId and a String for alias.


Last little question is the following:
My "Profession" class also got the same link with "Person", so whenever I ask the context for a person, I can look at his/her profession and within the profession there are more Persons with the same profession. Isn't this very slow with big databases? Maybe I should remove the backward link?

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

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

发布评论

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

评论(1

拥有 2024-11-12 08:39:09

Yakimych 的评论,我得到了我的回答:

您使用的是 Linq-to-SQL,不是吗? AFAIK,L2S 将联结表映射为实体,因此您必须像在工作示例中一样使用 PersonProfession 对象。在实体框架而不是 L2S 的情况下,纯联结表(仅包含两个外键)不会映射到单独的实体,并且您可以访问 Person.ProfessionsProfessions .直接与人

From Yakimych's comment, I got my answer:

it's Linq-to-SQL you are using, aren't you? AFAIK, L2S maps the junction table as an entity, so you have to use the PersonProfession object as you do in your working example. In case of Entity Framework as opposed to L2S, a pure junction table (containing only the two foreign keys) would not be mapped to a separate entity, and you could access Person.Professions and Professions.People directly.

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