开发者

How do I to check if a entity is in a many-to-many relationship

I have a 'user' table, a 'phone number' table and a 'user to phone number map' table. The phone number table stores only unique phone numbers. This way I can take a look at a phone number and see who is using it easily. It is also easy to check if a phone number exists when the user is edited.

The question is how I should be checking to see if the phone number should be added to the map. I could loop over the loaded mapped users and check each id. There must be a method开发者_运维技巧 that I can use to check like EdmUser.phone_number_map.contains(PhoneNumber). However I cant find anything like this.


Presuming the entity has a property called PhoneNumber which contains the string of the number...

var pnm = EdmUser.phone_number_map; // to save typing later on...
if !(pnm.IsLoaded) pnm.Load();
var existing = pnm.Where(pn => pn.PhoneNumber.Equals(PhoneNumber, StringComparison.WhateverYouNeed)).FirstOrDefault();
if (existing == null)
{
    pnm.Add(new PhoneNumberEntity { PhoneNumber = PhoneNumber } );
}
else
{
    // do stuff with "existing", if need be.
}
Context.SaveChanges();

Adjust accordingly if I've guessed your metadata incorrectly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜