如何在表 A 中创建新行,同时使用表 A 中的 ID 在表 B 中创建新行

问题描述 投票:0回答:1

Jobs 表与 JobDates 表具有 PK/FK 1-1 关系。 如何更改“创建作业”后调用,以便还为具有 JobID 的 JobDates 表生成新行(提交期间未知,因为它尚不存在)

public async Task<RedirectToPageResult> OnPost() 
{

    var Job = new Job()
    {
        JobNum = AddJobRequest.JobNum,
        CustomerId = AddJobRequest.CustomerId,
        JobName = AddJobRequest.JobName,
        JobDescription = AddJobRequest.JobDescription,
        JobType = AddJobRequest.JobType,
        JobStatusId = AddJobRequest.JobStatus,
        JobPM = AddJobRequest.JobPM,
        JobSM = AddJobRequest.JobSM,
        JobQty = AddJobRequest.JobQty
    };
    _context.Jobs.Add(Job);

    await _context.SaveChangesAsync();

    CustomerList = await _context.Customers.ToListAsync();
    return new RedirectToPageResult("/Index");
    
}

我已经查找了所有其他示例,也许我没有正确地措辞来找到答案。我觉得这一定是很常规的事情。

.net entity-framework razor-pages
1个回答
0
投票

想通了。

public async Task<RedirectToPageResult> OnPost() 
{

    var Job = new Job()
    {
        JobNum = AddJobRequest.JobNum,
        CustomerId = AddJobRequest.CustomerId,
        JobName = AddJobRequest.JobName,
        JobDescription = AddJobRequest.JobDescription,
        JobType = AddJobRequest.JobType,
        JobStatusId = AddJobRequest.JobStatus,
        JobPM = AddJobRequest.JobPM,
        JobSM = AddJobRequest.JobSM,
        JobQty = AddJobRequest.JobQty,
        **JobDate = new JobDate()**
    };
    _context.Jobs.Add(Job);

    await _context.SaveChangesAsync();

    CustomerList = await _context.Customers.ToListAsync();
    return new RedirectToPageResult("/Index");
    
}
© www.soinside.com 2019 - 2024. All rights reserved.