LINQ 中的多对多映射实体
我的 SQL Server 表如下:
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)
以及 C# 中的映射如下:
[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; }
}
}
但是,我得到 System.InvalidOperationException: 成员 'TableAtoB.tablea' 的关联映射无效。 “TableAtoB”不是实体。我的实体映射不正确吗?
我无法更改表架构。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要具有 ColumnAttribute.IsPrimaryKey 属性:
You need to have the ColumnAttribute.IsPrimaryKey property:
您可能想尝试使用设计器自动生成表并查看生成的内容。您似乎缺少一些 EntityRef 的附加构造函数和事件侦听器。此外,您的茶几似乎都没有像设计者中那样对多对多表的典型参考。
另外,您可能想看看 PLINQO 中的实现 http://www.codesmithtools.com/plinqo 因为他们能够实现隐藏中间 (TableAtoB) 表的代码生成方法,并提供更加面向对象的代码吐出,类似于 EF 处理多对多关系的方式。
You may want to try auto-generating the tables using the designer and see what is generated. There are some additional constructor and event listeners for EntityRef's which you appear to be missing. Also, It appears that neither of your end tables have the typical reference to the many to many table as you will find in the designers.
Also, you may want to take a look at the implementation in PLINQO http://www.codesmithtools.com/plinqo as they were able to implement a code generated method of hiding the middle (TableAtoB) table and give a more object oriented code spit similar to how EF handles Many to Many relationships.