C
C#17mo 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
Angius17mo ago
Make sure it's not actually the same item, as in, the same reference The change tracker does keep track of it
XANDRO
XANDROOP17mo ago
so what would be the best way to solve it Should I disable change tracker?
Angius
Angius17mo ago
First, what's the relationship supposed to be?
XANDRO
XANDROOP17mo ago
many to many
Angius
Angius17mo ago
Many to many between article and table?
XANDRO
XANDROOP17mo ago
articles can be on multiple tables and tables can have multiple articles
Angius
Angius17mo ago
And, I assume, the quantity is the quantity of a given article in a given table?
XANDRO
XANDROOP17mo ago
That's right
Angius
Angius17mo ago
For example, Table1 can have 10 apples, and Table2 24 apples?
XANDRO
XANDROOP17mo ago
Right
Angius
Angius17mo ago
Well, in that case, place the quantity not on the article, but on the join table
XANDRO
XANDROOP17mo ago
Im not sure if I get you
Angius
Angius17mo ago
Many-to-many requires a join table
XANDRO
XANDROOP17mo ago
Ohh wait
Angius
Angius17mo ago
EF can hide it, but it does exist And you can tap into it
XANDRO
XANDROOP17mo ago
I understand
XANDRO
XANDROOP17mo ago
Angius
Angius17mo ago
Or you can set it up explicitly Yep
XANDRO
XANDROOP17mo ago
You mean to add here quantity right?
Angius
Angius17mo ago
Exactly
XANDRO
XANDROOP17mo 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
Angius17mo ago
Of course you can
XANDRO
XANDROOP17mo 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
Angius17mo 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
XANDROOP17mo ago
I will have to create new model called TableArticles?
Angius
Angius17mo ago
Yep Or you can create a DbSet<TableArticles> and just tap into it directly
XANDRO
XANDROOP17mo 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
Angius17mo 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
XANDROOP17mo ago
Okay one more thing quantity from Article should be removed right?
Angius
Angius17mo ago
yep
XANDRO
XANDROOP17mo ago
Okay
XANDRO
XANDROOP17mo ago
XANDRO
XANDROOP17mo ago
And If I don't have anymore article.Quantity how else should I call it?
XANDRO
XANDROOP17mo 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
mtreit17mo ago
Trusting stack overflow was your first mistake😛
XANDRO
XANDROOP17mo ago
Ahahahahahha Where should I learn new stuff then?
mtreit
mtreit17mo 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
XANDROOP17mo ago
Thank you After creating new model will i still have articleTable join table? Or it will be replaced with new one
Angius
Angius17mo ago
Yeah, it will still exist in the db Just make sure it's named the same and EF should pick it up
XANDRO
XANDROOP17mo ago
So wait I will than have join table and one more?
Angius
Angius17mo 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
XANDROOP17mo ago
How I will access to quantity? I cant do article.quantity anymore right?
Angius
Angius17mo ago
You'll need to know the article and the table first, yes
XANDRO
XANDROOP17mo ago
And add those values to my new model right? And create crud for that model
Angius
Angius17mo 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
XANDROOP17mo ago
Okay and I should have crud options right? For that model of course
Angius
Angius17mo ago
If you need them, yes
XANDRO
XANDROOP17mo ago
One more think This is called junction table right?
Angius
Angius17mo ago
Yep
XANDRO
XANDROOP17mo ago
Okay thank you so much for all of these informations
Angius
Angius17mo ago
Or a "join table", or a "pivot table" I even seen used once or twice
XANDRO
XANDROOP17mo ago
All the best for you
Angius
Angius17mo ago
Anytime Ok
XANDRO
XANDROOP17mo 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
Angius17mo ago
Make it exist Yes
XANDRO
XANDROOP17mo 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
Angius17mo ago
Sure, you can select the results into a dictionary
Accord
Accord17mo 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