C
C#13mo ago
Spiritfarer

❔ How can I add multiple services to my quotations?

hiii, I am relatively new to ASP.NET and I have come across this question while working on an assignment, as I need to add multiple services to the same quotation. (I forgot to mention that I am using CodeFirst due to assignment requirements) However, I'm unsure how to proceed. While researching, I realized that I could add an intermediate table between quotations and services to store all the quoted services. The problem is that I don't fully understand how this would work. If I want to add a quotation ID that doesn't exist in the intermediate table, would it give me an error since it conflicts? I mean, it doesn't exist because the quotation hasn't been created as such yet, right? So, I'm not sure how to make this work, and I'm confused about the topic. Additionally, the assignment specifically requires the use of "ASP.NET MVC" implementing the Onion architecture, which is adding to my confusion. I would appreciate any insights on how to achieve this. Thank you in advance for taking the time to read the question.
7 Replies
Spiritfarer
SpiritfarerOP13mo ago
These are my entities Quotations: public class Quote { public int Quote_Id { get; set; } public int serial { get; set; } public int User_Id { get; set; } public User User { get; set; } public int Client_Id { get; set; } public Client Client { get; set; } public int Quantity { get; set; } public string Status { get; set; } public int Sub_Total { get; set; } public int Discount { get; set; } public int ITBS { get; set; } public int TotalGrl { get; set; } public List<SerQuo> serQuos { get; set; } } Services: public class Service { public int Service_Id { get; set; } public string Service_Name { get; set; } public string Service_Description { get; set; } public double Service_Price { get; set; } public List<SerQuo> serQuos { get; set; } } public class SerQuo { public int SerQuo_Id { get; set; } public int Quote_Id { get; set; } public Quote Quote { get; set; } public int Service_Id { get; set; } public Service Service { get; set; } }
Angius
Angius13mo ago
You don't necessarily need the intermediate table anymore
public class Foo
{
public int Id { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
public class Foo
{
public int Id { get; set; }
public List<Bar> Bars { get; set; }
}
public class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
is enough
Spiritfarer
SpiritfarerOP13mo ago
I chose to keep the intermediate table and add a custom pricing field because it allows me to set custom prices for each service within a quotation (it's a requirement set by the professor for the assignment (sigh)). Besides, from what I understand, in relational databases, you can't establish many-to-many relationships as such without an intermediate table.
Angius
Angius13mo ago
Correct EF will create such a table under the hood, still Even if you don't create it explicitly
Spiritfarer
SpiritfarerOP13mo ago
The only solution I see possible is to first insert the data that is not related to the services themselves to generate the ID, and then add the services or something like that.... I'm not entirely sure how good it is from a code quality perspective, but it's the only idea that comes to mind at the moment. That's why I want to see if someone with more knowledge has another idea.
Angius
Angius13mo ago
You can insert everything in one go
var foo = new Foo { ... };
var bar = new Bar { ... };
var fooBar = new FooBar {
Foo = foo,
Bar = bar,
};
_context.FooBars.Add(fooBar);
await _context.SaveChangesAsync();
var foo = new Foo { ... };
var bar = new Bar { ... };
var fooBar = new FooBar {
Foo = foo,
Bar = bar,
};
_context.FooBars.Add(fooBar);
await _context.SaveChangesAsync();
This will insert a new Foo, a new Bar, and a new FooBar that joins those two
Accord
Accord13mo ago
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.
Want results from more Discord servers?
Add your server