ā Get an Id from a table and save it to other table
Hello, I have a
Customer
table that serves as a masterlist for my customers. I also have a Orders
table. Now that the Orders
table has a CustomerId
column, it is referred to as the Customer
table.
Initially, I had a GetAllActiveCustomers
api that will collect all the customers and match the customer's name from the incoming orders if they match, I will obtain the customerId and save it to the Orders
table. Is there any other approach for this? Any suggestions would be greatly appreciated.18 Replies
For the record, you can use `single backticks` to have
inline code blocks
in your message
Also, do you use plain SQL? EF Core? Something else?Hi, thanks i'll edit my question
Im using EF Core
Okay, so... You have a
Customers
table, and an Orders
table that has a foreign key and a navigation property to Customers
to set up the relationship. And you want to get the names of all customers that currently have an active order?
So in case of
you want to get Matthew, Bob, Martha
?For example
Customer1
orders a item. I need to validate if that customer is available on my Customer
table. If it is available I save save the order including the Id
of that customer in the Orders
table.Well, check if the customer exists with
.AnyAsync()
and if they do, add the orderWhat about the id? How do I get it. Since orders have only Customer name?
Why do orders only have customer name and not the ID?
Relationships are established with IDs
And, in case of EF, nav properties
will create
Or something of the sort
If customer order a Item. I will get
CustomerName
, ItemName
, Location
. etc. So it doesnt have a CustomerId in the order itself. But In my Orders
table I have a CustomerId
.What if multiple customers have same name...
You're talking about getting the resulting data?
That's why the order needs customer id
What you return from the API?
Because you seem to refer to two different things as
order
The table, and whatever it is that has no customer ID but has their name
Is the issue that you get the customer name as a parameter to your API, and you need to create an order for that user?
In that case, yeah, what if two customers have the same name?Okay so let me explain more.
So this is the sample body i get from a request. As you can see it doesn't have any customerId in it. So I made a api it return the customer info including the Id, I matched the name from the body into the response of the API. If it matched I will get the Id and save to the
Orders
table.
Orders are usually coming from Excel files. Customers are usually using their Names since they dont know their customerId,
And for the Customer name. It is impossible that we will have a same customer name because I have a Customer table. I have also validation for adding customer if it is already exist or not.So what happens when John Smith (number 2) wants to join?
š¤
You can continue with unique name assumption if you want. Its completely unrealistic, though
Well john smith will be add to my Customer table
not all user can order. I have only certain customers
Like Supplier and Customer
You just said before, it's not possible for two John Smiths. Now you say John Smith (2nd) will be added.
Which is it?
Oh sorry for the confusion, there will be no John Smith number 2. Customer are shops. If ever it has one more John Smith it will have 2 in the name like Branch 2 (Location)
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.