C
C#β€’2y ago
XANDRO

❔ Problem while adding same article with different quantity on 2 different tables

I'm using EF Core and WPF. Once I add same article with different quantity on 2 different tables,quantity is replaced on all tables.
58 Replies
Angius
Angiusβ€’2y ago
Make sure it's not actually the same item, as in, the same reference The change tracker does keep track of it
XANDRO
XANDROOPβ€’2y ago
so what would be the best way to solve it Should I disable change tracker?
Angius
Angiusβ€’2y ago
First, what's the relationship supposed to be?
XANDRO
XANDROOPβ€’2y ago
many to many
Angius
Angiusβ€’2y ago
Many to many between article and table?
XANDRO
XANDROOPβ€’2y ago
articles can be on multiple tables and tables can have multiple articles
Angius
Angiusβ€’2y ago
And, I assume, the quantity is the quantity of a given article in a given table?
XANDRO
XANDROOPβ€’2y ago
That's right
Angius
Angiusβ€’2y ago
For example, Table1 can have 10 apples, and Table2 24 apples?
XANDRO
XANDROOPβ€’2y ago
Right
Angius
Angiusβ€’2y ago
Well, in that case, place the quantity not on the article, but on the join table
XANDRO
XANDROOPβ€’2y ago
Im not sure if I get you
Angius
Angiusβ€’2y ago
Many-to-many requires a join table
XANDRO
XANDROOPβ€’2y ago
Ohh wait
Angius
Angiusβ€’2y ago
EF can hide it, but it does exist And you can tap into it
XANDRO
XANDROOPβ€’2y ago
I understand
XANDRO
XANDROOPβ€’2y ago
Angius
Angiusβ€’2y ago
Or you can set it up explicitly Yep
XANDRO
XANDROOPβ€’2y ago
You mean to add here quantity right?
Angius
Angiusβ€’2y ago
Exactly
XANDRO
XANDROOPβ€’2y ago
I actually thought about that but someone said it's not possible on stackoverflow you can't add custom columns in join table
Angius
Angiusβ€’2y ago
Of course you can
XANDRO
XANDROOPβ€’2y ago
I was really thinking that would be the best solution until I found someone said that and I gived up on that idea.
Angius
Angiusβ€’2y ago
public class TableArticles
{
public Article Article { get; set; }
public int ArticleId { get; set; }
public Table Table { get; set; }
public int TableId { get; set; }
public int Quantity { get; set; }
}
public class TableArticles
{
public Article Article { get; set; }
public int ArticleId { get; set; }
public Table Table { get; set; }
public int TableId { get; set; }
public int Quantity { get; set; }
}
builder.Entity<Table>()
.HasMany(t => t.Articles)
.WithMany(a => a.Tables)
.WithJoinEntity<TableArticles>();
builder.Entity<Table>()
.HasMany(t => t.Articles)
.WithMany(a => a.Tables)
.WithJoinEntity<TableArticles>();
And on your Article you can have a List<Table> and a List<TableArticles> And vice versa
XANDRO
XANDROOPβ€’2y ago
I will have to create new model called TableArticles?
Angius
Angiusβ€’2y ago
Yep Or you can create a DbSet<TableArticles> and just tap into it directly
XANDRO
XANDROOPβ€’2y ago
Okay pal I will try to do what you said and I really hope it will work πŸ˜„ I will fill you in I just need couple of minutes
Angius
Angiusβ€’2y ago
I have something like that set up in my project, so I can dig for that code if need be to show you an example I'll be back in an hour or so
XANDRO
XANDROOPβ€’2y ago
Okay one more thing quantity from Article should be removed right?
Angius
Angiusβ€’2y ago
yep
XANDRO
XANDROOPβ€’2y ago
Okay
XANDRO
XANDROOPβ€’2y ago
XANDRO
XANDROOPβ€’2y ago
And If I don't have anymore article.Quantity how else should I call it?
XANDRO
XANDROOPβ€’2y ago
Can I do it like this? Do I have to create method to save this table or EF will do this for me since this is many to many
mtreit
mtreitβ€’2y ago
Trusting stack overflow was your first mistakeπŸ˜›
XANDRO
XANDROOPβ€’2y ago
Ahahahahahha Where should I learn new stuff then?
mtreit
mtreitβ€’2y ago
Stack Overflow has lots of good answers and lots of garbage answers and if you don't have a lot of experience it's hard to know which is which. This Discord is a good resource to leverage, as evidenced by this discussion.
XANDRO
XANDROOPβ€’2y ago
Thank you After creating new model will i still have articleTable join table? Or it will be replaced with new one
Angius
Angiusβ€’2y ago
Yeah, it will still exist in the db Just make sure it's named the same and EF should pick it up
XANDRO
XANDROOPβ€’2y ago
So wait I will than have join table and one more?
Angius
Angiusβ€’2y ago
No You'll only have one table You might need to provide a default value for the Count though, since the data that exists in the database right now doesn't have that After that, in the next migration, you'll be able to just remove the default value though
XANDRO
XANDROOPβ€’2y ago
How I will access to quantity? I cant do article.quantity anymore right?
Angius
Angiusβ€’2y ago
You'll need to know the article and the table first, yes
XANDRO
XANDROOPβ€’2y ago
And add those values to my new model right? And create crud for that model
Angius
Angiusβ€’2y ago
Then you can do
article.TableArticles.Where(ta => ta.TableId == tableId).First().Quantity;
article.TableArticles.Where(ta => ta.TableId == tableId).First().Quantity;
or
table.TableArticles.Where(ta => ta.ArticleId == articleId).First().Quantity;
table.TableArticles.Where(ta => ta.ArticleId == articleId).First().Quantity;
or
var quantity = await _context.TableArticles.
.Where(ta => ta.TableId == tableId && ta.ArticleId == articleId)
.Select(ta => ta.Quantity)
.FirstOrDefaultAsync();
var quantity = await _context.TableArticles.
.Where(ta => ta.TableId == tableId && ta.ArticleId == articleId)
.Select(ta => ta.Quantity)
.FirstOrDefaultAsync();
XANDRO
XANDROOPβ€’2y ago
Okay and I should have crud options right? For that model of course
Angius
Angiusβ€’2y ago
If you need them, yes
XANDRO
XANDROOPβ€’2y ago
One more think This is called junction table right?
Angius
Angiusβ€’2y ago
Yep
XANDRO
XANDROOPβ€’2y ago
Okay thank you so much for all of these informations
Angius
Angiusβ€’2y ago
Or a "join table", or a "pivot table" I even seen used once or twice
XANDRO
XANDROOPβ€’2y ago
All the best for you
Angius
Angiusβ€’2y ago
Anytime Ok
XANDRO
XANDROOPβ€’2y ago
I can't create Crud for ArticleTable because I didn't do DBset Also I can't do article.ArticleTable It doesnt exist And If you do this _context.TableArticles It means you added it to DbSet<>? Right
Angius
Angiusβ€’2y ago
Make it exist Yes
XANDRO
XANDROOPβ€’2y ago
Ok, I made it work with EF Core Is there any way to make it "easier" someone told me I could use dictionary<int,List<Article>> and save tableId in int and all articles from table to that list in dictionary
Angius
Angiusβ€’2y ago
Sure, you can select the results into a dictionary
Accord
Accordβ€’2y 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.

Did you find this page helpful?