UPDATE
I just had a thought that may very well be related to this problem. I am using the first code approach with this project. Initially, my ZoneMapping class was defined, as you can see below, however, there was only one PrimaryKey field in the database in the database. I believe that EF did not correctly interpret the data.
At this point, I made changes to my migration SQL script output to add an additional primary key that I applied to the database. I just updated the migration:
CreateTable( "dbo.NetC_EF_ZoneMapping", c => new { PostcodeKey = c.String(nullable: false, maxLength: 128), Zone_ID = c.Int(), }) .PrimaryKey(t => t.PostcodeKey) .ForeignKey("dbo.NetC_EF_Zone", t => t.Zone_ID) .Index(t => t.Zone_ID);
I just tried adding an additional PrimaryKey manually in the migration after defining PostcodeKey.
.PrimaryKey(t => t.Zone_ID)
Unfortunately, I still get my error - I assume that this migration is not used to create an EF model in the code, but I wonder if it believes that there can be only one record with any given PostcodeKey that can explain the situation?
I am posting a new question based on Linq. In addition, it does not function properly - duplicate elements , because I feel that it has been found enough that the question is invalid, and Also, this is not a problem.
The problem is that I have a Linq Where clause that seems to be returning invalid data. The data in my database is as follows:

The class that represents this data has a composite key:
/// <summary> /// Represents a mapping between a postcode and a zone /// </summary> [Table("NetC_EF_ZoneMapping")] public class ZoneMapping { /// <summary> /// Gets or sets the postcode identifier /// </summary> [Key] public String PostcodeKey { get; set; } /// <summary> /// Gets or sets the Zone identifier /// </summary> [Key] public Zone Zone { get; set; } }
So, I am executing the following code, which results in different identifiers:
var result = this.context.ZoneMappings.Include("Zone").Where(z => z.Zone.ID == 257 && z.PostcodeKey == "2214"); var result2 = new FreightContext().ZoneMappings.Include("Zone").Where(z => z.Zone.ID == 257 && z.PostcodeKey == "2214"); if (result.First().Zone.ID != result2.First().Zone.ID) throw new InvalidOperationException();

SQL (or ToString() for these two elements is identical). Therefore, the only difference is that this is a new context, while the other has been conveyed and used for some other things. Code that creates a context that returns an incorrect result:
// Copy the contents of the posted file to a memory stream using (StreamReader sr = new StreamReader(fileUpload.PostedFile.InputStream)) using (FreightContext context = new FreightContext()) { // Attempt to run the import ZoneMappingCSVImporter importer = new ZoneMappingCSVImporter(sr, context, System.Globalization.CultureInfo.CurrentUICulture); var items = importer.GetItems().ToList(); importer.SaveItems(items); this.successBox.Value = "Import completed and added " + items.Count() + " zones mappings."; }
Then ClassMap written in the library in which I use:
public ZoneMappingCSVImporter(TextReader textReader, FreightContext context, CultureInfo culture) : base(textReader, context, culture) { this.reader.Configuration.RegisterClassMap(new ZoneMappingMap(this.context)); }
I am viewing the context:
/// <summary> /// Initializes a new instance of the <see cref="ZoneMap"/> class. /// </summary> public ZoneMappingMap(FreightContext context) { if (context == null) throw new ArgumentNullException("context"); Map(m => m.PostcodeKey); Map(m => m.Zone).ConvertUsing(row => { // Grab the name of the zone then go find this in the database String name = row.GetField<String>("Zone"); return context.Zones.Where(z => String.Compare(z.Name, name, true) == 0).FirstOrDefault(); }); }
I don’t see anything strange here, I checked the SQL generated by the Entity Framework, confirmed that the database is the same - I can’t understand why the wrong record will be returned. Can anyone shed some light on this?