Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ中的多对多映射实体_Linq_Linq To Sql_C# 4.0_Many To Many - Fatal编程技术网

LINQ中的多对多映射实体

LINQ中的多对多映射实体,linq,linq-to-sql,c#-4.0,many-to-many,Linq,Linq To Sql,C# 4.0,Many To Many,我有如下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 Tab

我有如下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)
[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; }
    }
}
和C#中的映射,如下所示:

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)
[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; }
    }
}
[表格]
公共类表格
{
[列(IsPrimaryKey=true,IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
公共int ID{get;set;}
[专栏]
公共字符串名称{get;set;}
}
[附表]
公共类表格B
{
[列(IsPrimaryKey=true,IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
公共int ID{get;set;}
[专栏]
公共字符串数据{get;set;}
}
[附表]
公共类表格
{
[专栏]
公共int表a_id{get;set;}
内部实体参考表a;
[关联(IsForeignKey=true,OtherKey=“ID”,ThisKey=“tablea\u ID”,Storage=“\u tablea”)]
公共表
{
获取{return\u tablea.Entity;}
内部集合{u tablea.Entity=value;}
}
[专栏]
公共int tableb_id{get;set;}
内部实体参考表B;
[关联(IsForeignKey=true,OtherKey=“ID”,ThisKey=“tableb\u ID”,Storage=“\u tableb”)]
公共表格
{
获取{return\u tableb.Entity;}
内部集合{u tableb.Entity=value;}
}
}
但是,我得到System.invalidoOperationException:成员“TableAtoB.tablea”的关联映射无效TableAtoB'不是实体。我的实体映射是否不正确


我无法更改表架构。

您可能需要尝试使用设计器自动生成表,并查看生成的内容。对于EntityRef,您似乎缺少一些额外的构造函数和事件侦听器。此外,两个端点表似乎都没有在设计器中找到的多对多表的典型引用


另外,您可能想看看PLINQO中的实现,因为他们能够实现一种代码生成的方法来隐藏中间(TableAtoB)表,并提供一种更面向对象的代码,类似于EF处理多对多关系的方式。

您需要ColumnAttribute.IsPrimaryKey属性:

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

两个想法:是不是
主键非聚集(tablea\u id,tableb\u id)
需要在
TableAToB
类中表示?
ID
的情况是否重要?