简体   繁体   English

检索Linq到SQL中ID为动态的一行

[英]Retrieving a row in Linq to SQL whose ID is dynamic

I have the following code:我有以下代码:

var db = new IpsDBDataContext(connectionString);

var record = new IpsJobQueue();

var config = new IpsJobFileConfig();

record.FileName = file.Name;
record.FileNameConventionID = ....?
record.PickupDate = DateTime.Now;

// hard coded to follow suit with "unprocessed" flag in 
// IpsJobProcessCodes table.
record.ProcessStatus = 1; 
record.CreationTime = file.CreationTime;

record.StartTime = null;
record.EndTime = null;

db.IpsJobQueues.InsertOnSubmit(record);

db.SubmitChanges();

The line I am concerned with is record.FilenameConventionID =...?我关心的行是record.FilenameConventionID =...?

Here, I need to assign the FileNameConventionID to a ID (as a foreign key) in the IpsJobFileConfig table where the PublisherName column = "Undetermined".在这里,我需要将FileNameConventionID分配给IpsJobFileConfig表中的一个ID (作为外键),其中 PublisherName 列 =“未确定”。 Thats easy to do but the issue is the ID that is associated with this record type (Undetermined) will likely change as it gets promoted to production.这很容易做到,但问题是与此记录类型(未确定)关联的ID可能会随着它被提升到生产环境而改变。

So what i perceive I need to do will be in two steps.所以我认为我需要做的将分两步进行。

  1. Find the record whose PublisherName = Undetermined查找PublisherName = Undetermined的记录
  2. Get the ID on that record (as it will be dynamic).获取该记录的ID (因为它将是动态的)。

The question is: How do I reflexively get the ID off the column whose record I am selecting?问题是:我如何反射性地从我选择的记录列中获取 ID?

Right now you're creating a new IpsJobFileConfig record but not using it.现在您正在创建一个新的IpsJobFileConfig记录但没有使用它。 I'm guessing you could get rid of that altogether.我猜你可以完全摆脱它。

You should be able to get away with (assuming any record in the IpsJobFileConfig table with the correct publisher name will work since there could be multiple rows):您应该能够逃脱(假设 IpsJobFileConfig 表中具有正确发布者名称的任何记录都可以工作,因为可能有多行):

var config = db.IpsJobFileConfigs.First(c => c.PublisherName == "Undetermined");
record.FileNameConventionID = config.Id;

You have a JobQueue instance and a related previously existing JobFileConfig instance.您有一个 JobQueue 实例和一个相关的先前存在的 JobFileConfig 实例。

You should load the JobFileConfig from the database, like this:您应该从数据库中加载 JobFileConfig,如下所示:

JobFileConfig config = db.JobFileConfig.Where(c => c.PublisherName == "Undetermined").FirstOrDefault();

Then you should attach the new instance to the object graph like this:然后你应该像这样将新实例附加到 object 图:

config.JobQueues.Add(myJobQueue);

Don't assign the ID.不要分配ID。 Don't InsertOnSubmit the new JobQueue instance.不要InsertOnSubmit新的 JobQueue 实例。 It's connected to the object graph and the DataContext will manage the rest when you call SubmitChanges() .它连接到 object 图,当您调用SubmitChanges()时,DataContext 将管理 rest。 It will save the new instance to the database and populate any IDs back into your instance.它会将新实例保存到数据库并将任何 ID 填充回您的实例。


PS: DataContext implements IDispoable , so you should using it. PS:DataContext 实现IDispoable ,所以你应该using它。

using(IpsDBDataContext db = new IpsDBDataContext(connectionString))
{
  ...
}

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

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