LINQ 中的多对多映射实体

发布于 2024-12-19 11:45:37 字数 1719 浏览 2 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‘画卷フ 2024-12-26 11:45:37

您需要具有 ColumnAttribute.IsPrimaryKey 属性:

[Column(IsPrimaryKey = true)]
public int tablea_id { get; set; }
...
[Column(IsPrimaryKey = true)]
public int tableb_id { get; set; }

You need to have the ColumnAttribute.IsPrimaryKey property:

[Column(IsPrimaryKey = true)]
public int tablea_id { get; set; }
...
[Column(IsPrimaryKey = true)]
public int tableb_id { get; set; }
情释 2024-12-26 11:45:37

您可能想尝试使用设计器自动生成表并查看生成的内容。您似乎缺少一些 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文