C# - 带联结表的 Linq 多对多
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Yakimych 的评论,我得到了我的回答:
From Yakimych's comment, I got my answer: