简体   繁体   English

如何从c#列表中验证是否插入关系记录

[英]How to verify relational record inserted or not from c# list

This is testing approach task,这是测试方法任务,

I have List<Customers> , let's assume customer list have 10 records and all customer have 3 orders, ( please note in real requirement, there can be n records and it may be x orders).我有List<Customers> ,假设客户列表有 10 条记录,所有客户有 3 个订单,(请注意实际需求,可以有n条记录,可能是x 个订单)。

public class Customers
{
  public int CustomerId {get;set;}
  public int CustomerTableXYZUniqueId {get;set;}
  public List<orders> orders{ get; set; }
}

public class Orders
{
  public int OrderId {get;set;}
  public int CustomerId {get;set;}
  public int OrderTableXYZUniqueId {get;set;}
  public List<orders> orders{ get; set; }
}

so total 30 rows will be enter across 2 tables.因此总共将在 2 个表中输入 30 行。 parent Table : customer child Table : order父表:客户子表:订单

Now in C#, I need to execute a SQL query, which will guarantee me that all 30 rows records have been entered in.现在在 C# 中,我需要执行一个 SQL 查询,这将保证我输入了所有 30 行记录。

What query should I write in SQL to verify that all records are inserted in 2 tables?我应该用 SQL 编写什么查询来验证所有记录都插入到 2 个表中?

parent Table : customer
child Table : orders

Maybe something like this, but not sure:也许是这样的,但不确定:

select * from customer as poh  join
orders as poL customer.customerId  = orders.customerId 
where customer.CustomerTableXYZUniqueId in ( "172772,18282881,28282818")
and orders.orderTableXYZUniqueId in ('37371', "182882");

do you have suggestion please?你有什么建议吗?

Edit1 :编辑1:

What i am trying to do is, i'm sending List to some POST API call, when API call returning me success, i want to run some kind of sql script via c# which will make sure that records are entered with exact fields value.我想要做的是,我将 List 发送到一些 POST API 调用,当 API 调用返回我成功时,我想通过 c# 运行某种 sql 脚本,这将确保输入的记录具有精确的字段值。

here in this example, i have chosen fields property are Customer/OderXYZUniqueId,在这个例子中,我选择的字段属性是 Customer/OderXYZUniqueId,

So i will read Customer/OderXYZUniqueId values and include in SQL query and see if records are inserted or not.所以我将读取 Customer/OderXYZUniqueId 值并包含在 SQL 查询中,看看是否插入了记录。

Edit 2 :编辑2:

var count1 = SQL query with in/join statement.
var count2 = ( how do we calculate the count here)

if(count1 == count2)
{
 // test pass here
}

You can find all records by some unique column:您可以通过一些唯一的列找到所有记录:

select * from customer where CustomerTableXYZUniqueId in ( "172772,18282881,28282818")

and the second:第二个:

select * from orders 
where orderTableXYZUniqueId in ('37371', "182882");

Or try to add some column like DateCreated and write C# code when you insert Date.Now或者尝试添加一些像DateCreated这样的列,并在插入Date.Now时编写 C# 代码

If you just want to ensure there are inserted 30 rows, and all customers are new then a modified version of your query could look like如果您只想确保插入了 30 行,并且所有客户都是新客户,那么您的查询的修改版本可能如下所示

select * from customer as poh 
inner join orders as poL on poh.customerId  = pol.customerId 
where poh.CustomerTableXYZUniqueId in ( '172772','18282881','28282818')

assuming your unique id's are strings, otherwise loose the '假设您的唯一 ID 是字符串,否则会丢失 '

this will give you a set with all orders for the three customer id's这将为您提供一个包含三个客户 ID 的所有订单的集合

if you need to be absolutly sure the order ids are matching as well, then you need to add that criteria as well如果您需要绝对确定订单 ID 也匹配,那么您还需要添加该条件

select * from customer as poh 
inner join orders as poL on poh.customerId  = pol.customerId 
where poh.CustomerTableXYZUniqueId in ( '172772','18282881','28282818')
and pol.orderTableXYZUniqueId in ('37371', '182882')

if you just need to ensure 40 rows are inserted in your test, and no one else is inserting data at the same time as your test is running then another approach could be to get a total count before running the test, and compare it with the total amount after.如果您只需要确保在您的测试中插入 40 行,并且没有其他人在您的测试运行的同时插入数据,那么另一种方法可能是在运行测试之前获取总数,并将其与后的总金额。

select count(*) from customer as poh 
inner join orders as poL on poh.customerId  = pol.customerId 

if you want a total count for new customers and orders, then i suggest something like如果您想统计新客户和订单的总数,那么我建议您这样做

select count(*) as cnt from  from customer as poh 
where poh.CustomerTableXYZUniqueId in ( '172772','18282881','28282818')
union all 
select count(*) as cnt from orders as pol where pol.orderTableXYZUniqueId in ('37371', '182882')

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

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