Last post Dec 29, 2016 08:50 AM by Zhi Lv - MSFT
Dec 22, 2016 08:56 PM|gib9898_00|LINK
I have a situation dealing with EntityFramework and I came up with a solution to the problem, but I'm wondering if there's a better way to do it.
Here's the situation:
In my database, I have a lookup table called BowtieDMEquipmentLookup. It consists of two columns: an ID and an equipment description.
I also have a Bowtie table. It consists of several columns, one of which is a reference to the equipment lookup.
The Bowtie table is populated by reading a CSV file. I have an ICsvReader that reads the file, and parses the data therein into objects, one of which is an EntityFramework object called Bowtie.
The trick here is that for the equipment description of the bowtie, the CSV files contains a description string of the equipment itself, not an ID reference. Therefore, when I parse it, I can't just transfer the description string over to Bowtie.BowtieDMEquipmentLookupId
(which is an int value). I have to validate the description I see in the CSV against the lookup table, and if found, grab the ID and assign that to Bowtie.BowtieDMEquipmentLookupId instead.
I have a BowtieMap class that looks like this:
public class BowtieMap : CsvClassMap<Bowtie>
Map(b => b.BowtieDMEquipmentLookupId).ConvertUsing(x => x.ParseBowtieDMEquipment(8));
The ParseBowtieDMEquipment() function is in a ParseHelpers class:
public static int? ParseBowtieDMEquipment(this ICsvReaderRow x, int fieldIndex)
var field = x.GetField(fieldIndex).Trim();
var lookup = BowtieDMEquipmentLookups
.Where(y => y.EquipmentDescription.Equals(field, StringComparison.OrdinalIgnoreCase))
if (lookup != null)
throw new ParsingException("Profiler Import", "Invalid Bowtie Data Mining Equipment on row " + x.Row + ": " + field);
This function gets called when the following line gets run in the importer class:
var bowtie = Reader.ParseEntity<Bowtie>();
The problem I was having is that the EntityFramework class BowtieDMEquipmentLookup is not available in the ParserHelpers class. So in order to make it available, I had to create a static references to it in ParserHelps:
public static DbSet<BowtieDMEquipmentLookup> BowtieDMEquipmentLookups;
...and assign it in the importer class like this:
ParserHelpers.BowtieDMEquipmentLookups = Context.BowtieDMEquipmentLookups;
But this seems like a hack to me. There must be a better way of making Context.BowtieDMEquipmentLookups available to ParserHelpers. Is there?
Dec 23, 2016 06:48 AM|Zhi Lv - MSFT|LINK
There must be a better way of making Context.BowtieDMEquipmentLookups available to ParserHelpers. Is there?
As for this issue, you could try to create a global context instance. Such as using the Global file and the following code:
public sealed class Global
public static NorthwindEntities Context
string ocKey = "key_" + HttpContext.Current.GetHashCode().ToString("x");
HttpContext.Current.Items.Add(ocKey, new NorthwindEntities());
return HttpContext.Current.Items[ocKey] as NorthwindEntities;
More details, see:
Dec 28, 2016 03:31 PM|gib9898_00|LINK
Ok, so this will make specific Entities in the context available from anywhere, huh?
Dec 29, 2016 08:50 AM|Zhi Lv - MSFT|LINK
so this will make specific Entities in the context available from anywhere, huh?
Yes, when you call it, you could use the following code: