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 开发者_Go百科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?
Try using ThisKey = "ItemTypeId"
I also had this problem and the solution for me was that I has accidentally created a Page with the same name as one of my Tables in the database. Renamed the Page and it worked straight away.
精彩评论