I have SQL Server tables as follows:
create table TableA (
id int primary key identity,
name varchar(16) not null
)
create table TableB (
id int primary key identity,
data varchar(max) not null
)
create table TableAtoB (
tablea_id int not null foreign key references TableA(id),
tableb_id int not null foreign key references TableB(id),
primary key nonclustered (tablea_id, tableb_id)
)
create unique index idxID on TableAtoB(tableb_id, tablea_id)
and mappings in C# as follows:
[Table]
public class TableA
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int ID { get; set; }
[Column]
public string name { get; set; }
}
[Table]
public class TableB
{
[Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
public int ID { get; set; }
[Column]
public string data { get; set; }
}
[Table]
public class TableAtoB
{
[Column]
public int tablea_id { get; set; }
internal EntityRef<TableA> _tablea;
[Association(IsForeignKey = true, OtherKey = "ID", ThisKey = "tablea_id", Storage = "_tablea")]
public TableA tablea
{
get { return _tablea.Entity; }
internal set { _tablea.Entity = value; }
}
[Column]
public int tableb_id { get; set; }
internal EntityRef<TableB> _tableb;
[Association(IsForeignKey = true, OtherKey = "ID", ThisKey = "tableb_id", Storage = "_tableb")]
public TableB tableb
{
get { return _tableb.Entity; }
internal set { _tableb.Entity = value; }
}
}
However, I get System.InvalidOperationException: Invalid association mapping for member ‘TableAtoB.tablea’. ‘TableAtoB’ is not an entity. Are my entity mappings not correct?
I cannot change the table schema.
You need to have the ColumnAttribute.IsPrimaryKey property: