简体   繁体   English

Linq to SQL数据库管理

[英]Linq to SQL database management

So i am having a real issue with this. 所以我对此有一个真正的问题。 I wan add a foreign key to a row i am adding to a database. 我要在要添加到数据库的行中添加外键。 I have all my references setup so they all map up perfectly fine. 我设置了所有参考,因此它们都映射得很好。

The issue is that i want to add the key without running the query until all rows are submitted. 问题是我要添加键而不运行查询,直到提交所有行。

example tables 示例表

**FirstNameTable**    **LastNameTable**        **People-Table**
Id | Value            Id | Value               Id | FirstNameID | LastNameID 
----------------      ---------------          ------------------------------
1  | Jeff             1  | jones               1  | 1           | 3          
2  | Joe              2  | Richards            2  | 2           | 1          
3  | John             3  | bobson              3  | 3           | 2          

So Table3 has 3 foreign keys (FirstNameId links to FirstNameTable, LastNameId links to LastNameTableand ParentId links to People-Table). 因此,Table3具有3个外键(FirstNameId链接到FirstNameTable,LastNameId链接到LastNameTable,ParentId链接到People-Table)。 For the sake of this example lets just say that there will never be a person that has the same first and last name. 出于这个示例的原因,我们只能说永远不会有一个姓氏和名字相同的人。

I want to add a new entry for table 3 but i want it to either use an existing entry if it is there or create a new one. 我想为表3添加一个新条目,但我希望它使用现有的条目(如果有)或创建一个新条目。

Now doing 现在在做

People-Tabletb3 = new People-Table()
{
    FirstNameTable = new FirstNameTable(){ Value = Jeff};
    LastNameTable= new LastNameTable(){ Value = jones};
}

Will always create a new entry in my other two tables. 将始终在其他两个表中创建一个新条目。 Doing the following for each row in each table 对每个表中的每一行执行以下操作

if ((from u in dc.Users where u.Name == name select u).Count() > 0)
    // just grab the id
else
    // make it then grab the id

Isn't really an option because you are then doing more queries and slowing down the upload. 并不是真正的选择,因为您随后会执行更多查询并减慢上传速度。

So my question is how do i create a new People-Table row using LINQ that will use an existing row for the first and last name or create a new one if it doesn't exist. 所以我的问题是我如何使用LINQ创建一个新的People-Table行,该行将使用现有行作为名字和姓氏,或者如果不存在则创建一个新行。 Without doing the if-else stuff. 不做if-else东西。

  1. Create a list of all the distinct names in your list of entries that you're trying to insert. 在您要插入的条目列表中创建所有唯一名称的列表。
  2. Create a query that pulls all of the values from the Users table, whose names are found in the list you just created. 创建一个查询,从“用户”表中提取所有值,这些表的名称在您刚创建的列表中找到。 Put these values into a dictionary, keyed by their name. 将这些值放入字典中,并以其名称为关键字。
  3. Loop through the entries you want to insert, and for each of those, either associate them with a value from the dictionary you created, or create a new value and add it to the dictionary. 循环浏览要插入的条目,对于每个条目,将它们与您创建的字典中的值相关联,或创建一个新值并将其添加到字典中。

This should do what you're asking, for any number of new entries, in two round-trips total. 对于任何数量的新条目,这应该可以满足您的要求,总共两个往返。

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

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