LINQ-to-SQL: Could not find key member 'x' of key 'x' on type 'y'

Posted by Austin Hyde on Stack Overflow See other posts from Stack Overflow or by Austin Hyde
Published on 2010-06-02T02:33:41Z Indexed on 2010/06/02 2:43 UTC
Read the original article Hit count: 609

Filed under:
|
|

I am trying to connect my application to a SQLite database with LINQ-to-SQL, and so far everything has worked fine. The only hitch was that the SQLite provider I am using does not support code generation (unless I was doing something wrong), so I manually coded the 4 tables in the DB.

The solution builds properly, but will not run, giving me the error message

Could not find key member 'ItemType_Id' of key 'ItemType_Id' on type 'Item'.
The key may be wrong or the field or property on 'Item' has changed names.

I have checked and double checked spellings and field names on the database and in the attribute mappings, but could not find any problems.

The SQL for the table looks like this:

CREATE TABLE [Items] (
    [Id] integer PRIMARY KEY AUTOINCREMENT NOT NULL,
    [Name] text NOT NULL,
    [ItemType_Id] integer NOT NULL
);

And my mapping code:

[Table(Name="Items")]
class Item {

    // [snip]

    [Column(Name = "Id", IsPrimaryKey=true, IsDbGenerated=true)]
    public int Id {
        get;
        set;
    }

    // [snip]

    [Column(Name="ItemType_Id")]
    public int ItemTypeId {
        get;
        set;
    }

    [Association(Storage = "_itemType", ThisKey = "ItemType_Id")]
    public ItemType ItemType {
        get {
            return _itemType.Entity;
        }
        set {
            _itemType.Entity = value;
        }
    }
    private EntityRef<ItemType> _itemType;

    // [snip]
}

This is really my first excursion into LINQ-to-SQL, and am learning as I go, but I cannot seem to get past this seeming simple problem.

Why cannot LINQ see my association?

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql