How to check duplicates before saving?

I have damn time to figure out how to add entities like this to my db.

public class ThingWithListings { public virtual ICollection<Listing> Listings; } public class Listing { public int Id; public virtual ListingData Data { get; set; } // has a FK to ListingData public DateTime Creation { get; set; } } public class ListingData { public int listing_id; // PK .... } 

I extract "ThingWithLIstings" from another source and write it to my db. The tricky part is that any number of Listings can report the same listing. So when I add or update ThingWithListings, I need to see if the DataData listing exists, and if so, just use it.

I am new to EF, so I used AddOrUpdate from a Vickers article here : Obviously, don't work on this script, and so I tried a day or so to figure out the right way to do this. I will spare you the whole story of my major unsuccessful attempts and hope that someone can just tell me the right way to do this.

+4
source share
3 answers
 if (DatabaseContext.ListingData.Any(l => l.listing_id == myId)) { //already exists } else { //do whatever } 
0
source
 var newArrivals = new ThingWithListings(); newArrivals.Listings = new List<Listing>(); newArrivals.Listings.Add( new Listing() { creation = DateTime.Now, ListingData = new ListingData() { listing_id = 1 } }); //another Listing with the same ListingData listing_id newArrivals.Listings.Add( new Listing() { creation = DateTime.Now, ListingData = new ListingData() { listing_id = 1 } }); //dummy id generator int counter = 1; using (var ctx = new Database1Entities()) { //get the ListingData from the current db context var dbListingData = ctx.ListingData; // the new arrivals foreach (Listing item in newArrivals.Listings) { //get the listing_id of a new arrival ListingData int id = item.ListingData.listing_id; //get the ListingData from the db, if it exists var listingDataFromDb = dbListingData.FirstOrDefault(i => i.listing_id == id); //if not, create it and add it to the db if (listingDataFromDb == null) { listingDataFromDb = new ListingData() { //use the new arrival listing_id listing_id = item.ListingData.listing_id }; ctx.ListingData.Add(listingDataFromDb); ctx.SaveChanges(); } //add the Listing by using the listingDataFromDb, which now references the db ListingData ctx.Listing.Add(new Listing() { id = counter++, creation = item.creation, ListingData = listingDataFromDb }); ctx.SaveChanges(); } } 
0
source

I assume that in addition to referencing the Data object, you also have a primitive foreign key listing_id in your Listing class. If not, I recommend adding it.

You can start by selecting an existing listing_id in a list or array. This will save numerous database trips later.

Then the process is really simple: for each Listing object that comes, check if its listing_id in the pre-selected list:

  • If so, do nothing with ListingData - just add (or update) Listing , including the listing_id property.
  • If not, add Listing and set Listing.Data with the Listing.Data object as the new (added) objects. EF will install the keys.

(Note that this assumes that there are no concurrent users modifying ListData, so you can take a snapshot of the identifier)

0
source

All Articles