Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

Commented Unassigned: Many-to-many not saving [1225]

$
0
0
I have created a many to many relationship between 3 tables (1 containing only the 2 foreign keys which is both primary keys).

In EF of course the link table does not show up as an entity which is as expected. The Contact and Client entities have a virtual list added to each of one another as per the many-to-many relationship.

However when I add a client to a contact via the Client list on the Contact entity and save it, the relationship is never added.

Sample:

var contact = await _unitOfWorkDataEntities.Repository<Contact>()
.Query()
.Filter(c => c.Id == contactId)
.Include(c => c.Clients)
.Get().FirstAsync();


var client = await _unitOfWorkDataEntities.Repository<Client>()
.Query()
.Filter(c => c.Id == clientId)
.Include(c => c.Contacts)
.Get().FirstAsync();


Once loaded I then try to add them to each other (have tried many ways)

contact.Clients.Add(client);
client.Contacts.Add(contact);

And then calling Save

await _unitOfWorkDataEntities.SaveAsync();

This does not save the relationship.

Even after modifying the ObjectState on the entities, still end up with the same result:

client.ObjectState = ObjectState.Modified;
contact.Clients.Add(client);
_contactService.Update(contact);

contact.ObjectState = ObjectState.Modified;
client.Contacts.Add(contact);
_clientService.Update(client);


If there is something obvious that I'm missing, please let me know.
Comments: I did notice that after setting Configuration.AutoDetectChangesEnabled = true; ... the changes happened. I'm curious the impact it might have by leaving this as true

Viewing all articles
Browse latest Browse all 1539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>