4

I have the given EF entity.

    [Key]
    [Column("ACTIVITY.ID", Order = 0)]
    public string ActivityId { get; set; }

    [Key]
    [Column("STATISTIC.SECTION", Order = 1)]
    public string StatisticSection { get; set; }

    [Key]
    [Column("STATISTIC.NAME", Order = 2)]
    public string StatisticName { get; set; }

    [Key]
    [Column("LAP.ID", Order = 3)]
    public string LapId { get; set; }

    [Column("STATISTIC.TYPE")]
    public string StatisticType { get; set; }

    [Column("STATISTIC.VALUE")]
    public string StatisticValue { get; set; }

    [Column("STATISTIC.UNIT")]
    public string StatisticUnit { get; set; }

and when doing an insert operation using the method bellow:

    public bool Add(ICollection<RefLapStatistic> entities)
    {
        return HandleInsert(() =>
        {
            using (var context = new ReportingRefDataContext(DataSource))
            {
                var activities = entities.Select(x => x.ActivityId).Distinct();
                var entitiesToDelete = context.LapStatistics
                .Where(x => activities.Any(y => x.ActivityId.Equals(y, System.StringComparison.OrdinalIgnoreCase)));

                context.LapStatistics.RemoveRange(entitiesToDelete);

                entities.ToList().ForEach(x => context.LapStatistics.Add(Mapper.Map< RefLapStatisticEntity>(x)));

                return context.SaveChanges() > 0;
            }
        });
    }

I get the following error when executing the line bellow:

var entitiesToDelete = context.LapStatistics
                .Where(x => activities.Any(y => x.ActivityId.Equals(y, System.StringComparison.OrdinalIgnoreCase)));

Unable to determine composite primary key ordering for type

I have no idea why this is happening, can't see any issues with the mapping and the exception is being raised even before the Save method. This issue has nothing to do with missing the Ordering attribute has I've have that defined.

MeTitus
  • 3,390
  • 2
  • 25
  • 49
  • Are you sure you are using EF6? – Hamid Pourjam Mar 09 '16 at 12:07
  • What .NET framework version are you using? – Hamid Pourjam Mar 09 '16 at 12:08
  • Yes I am sure (6.1.3), 4.5.2. I have many other mappings working just fine. – MeTitus Mar 09 '16 at 12:12
  • Possible duplicate of [Mapping composite keys using EF code first](http://stackoverflow.com/questions/19792295/mapping-composite-keys-using-ef-code-first) – demo Mar 09 '16 at 12:13
  • @demo, my issue is different, I do have the column ordering set. – MeTitus Mar 09 '16 at 12:18
  • Can you check where -> var entitiesToDelete = context.LapStatistics .Where(x => x.ActivityId !=null); or a simpler query working? – Anil Mar 09 '16 at 12:25
  • context.LapStatistics.Add(Mapper.Map< RefLapStatisticEntity>(x) It fails here as well. – MeTitus Mar 09 '16 at 12:28
  • Are the properties of your entity mapped to columns from different tables? – Rik Mar 09 '16 at 12:51
  • No they are all mapped to the same table, but this problem is happening even before the EF tries to execute the code against the database – MeTitus Mar 09 '16 at 12:52
  • Not really. It happens before you call `SaveChanges`, but before that, you're querying the db for `Activities`, which might be where the problem lies. – Rik Mar 09 '16 at 12:57
  • Yes you right, but if I comment that code if fails when adding the tables to the context. – MeTitus Mar 09 '16 at 13:02
  • I'm also confused about your column names, as I don't think SQL server column names can contain `.`'s – Rik Mar 09 '16 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105814/discussion-between-rik-and-marco). – Rik Mar 09 '16 at 13:53
  • @Rik the problem was related to some other entity in the context... thanks for your help. – MeTitus Mar 09 '16 at 15:29
  • Can you explain? I am having similar issues and not finding answers. I have the ColumnAttribute defined with correct orders. – Andrew Hawes Feb 24 '17 at 12:37

0 Answers0