Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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
使用PostgreSQL在C#LINQ中引用查找表_C#_.net_Linq_Postgresql_Mono - Fatal编程技术网

使用PostgreSQL在C#LINQ中引用查找表

使用PostgreSQL在C#LINQ中引用查找表,c#,.net,linq,postgresql,mono,C#,.net,Linq,Postgresql,Mono,大家好 我在PostgreSql中有几个表 主表 CREATE TABLE main_table ( main_table_id serial NOT NULL, ... some data fields ... table_type integer, <--- Foreign key CONSTRAINT main_table_id PRIMARY KEY (duty_plan_id), CONSTRAINT main_table_to_table_type

大家好

我在PostgreSql中有几个表

主表

CREATE TABLE main_table
(
  main_table_id serial NOT NULL,
  ... some data fields ...
  table_type integer,      <--- Foreign key
  CONSTRAINT main_table_id PRIMARY KEY (duty_plan_id),
  CONSTRAINT main_table_to_table_type FOREIGN KEY (table_type)
      REFERENCES table_type(table_type) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
和映射到此表的C#类:

[Table(Name="main_table")]
    public class MainTable
    {
        [Column(Name="main_table_id", IsPrimaryKey=true,
         IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
        internal int Id { get; set; }

        .......

        [Column(Name = "table_type")]
        internal int TableTypeId { get; set; }

        private EntityRef<TableType> _TableType;

        [Association(IsForeignKey = true, ThisKey = "TableTypeId ", 
         Storage = "_TableType", Name = "main_table_to_table_type")]
        public TableType TableType{ 
            get { return _TableType.Entity; }
            set { _TableType.Entity = value; TableTypeId = value.Id; } 
        }

    }

[Table(Name="table_type")]
public class TableType
{
    [Column(Name="table_type", IsPrimaryKey=true)]
    public int Id { get; set; }

    [Column(Name = "value")]
    public String Value { get; set; }
}
[Table(Name=“main_Table”)]
公共类主表
{
[Column(Name=“main\u table\u id”,IsPrimaryKey=true,
IsDbGenerated=true,AutoSync=AutoSync.OnInsert)]
内部int Id{get;set;}
.......
[列(Name=“table_type”)]
内部int TableTypeId{get;set;}
私有EntityRef_表类型;
[关联(IsForeignKey=true,ThisKey=“TableTypeId”,
Storage=“\u TableType”,Name=“main\u table\u to\u table\u type”)]
公共表类型表类型{
获取{return\u TableType.Entity;}
set{{u TableType.Entity=value;TableTypeId=value.Id;}
}
}
[表格(Name=“表格类型”)]
公共类表类型
{
[列(Name=“table_type”,IsPrimaryKey=true)]
公共int Id{get;set;}
[列(Name=“value”)]
公共字符串值{get;set;}
}
当我尝试获取所有MainTable实体的列表时

(new DataContext(connectionString)).GetTable<MainTable>().MainTables;
(新的DataContext(connectionString)).GetTable().MainTables;
从数据库中,我得到一个错误:

由于对象的当前状态,操作无效

即使我没有调用TableType属性,也会出现此错误。但当我在TableType之前注释Assotation字符串时,它就消失了

我哪里出错了


我使用的是.NET3.5、Mono2.10和PostgreSql 9.2.4,所以问题在下一步得到了解决。我将
TableTypeId
字段
public
和修改后的TableType添加回MainTable的引用:

[Table(Name="table_type")]
public class TableType
{
    [Column(Name="table_type", IsPrimaryKey=true)]
    public int Id { get; set; }

    [Column(Name = "value")]
    public String Value { get; set; }

    private EntitySet<MainTable> _MainTable = new EntitySet<MainTable>();
    [Association(OtherKey = "TableTypeId", ThisKey = "Id", 
     Storage = "_MainTable")]
    public ICollection<MainTable> MainTable
    {
        get { return _MainTable; }
        set { _MainTable.Assign(value); }
    }
}
[表格(Name=“表格类型”)]
公共类表类型
{
[列(Name=“table_type”,IsPrimaryKey=true)]
公共int Id{get;set;}
[列(Name=“value”)]
公共字符串值{get;set;}
私有EntitySet _MainTable=新EntitySet();
[关联(OtherKey=“TableTypeId”,ThisKey=“Id”,
存储=“\u MainTable”)]
公共ICollection主表
{
获取{return\u MainTable;}
设置{u MainTable.Assign(value);}
}
}
不确定它是否正确,但它对我有效

[Table(Name="table_type")]
public class TableType
{
    [Column(Name="table_type", IsPrimaryKey=true)]
    public int Id { get; set; }

    [Column(Name = "value")]
    public String Value { get; set; }

    private EntitySet<MainTable> _MainTable = new EntitySet<MainTable>();
    [Association(OtherKey = "TableTypeId", ThisKey = "Id", 
     Storage = "_MainTable")]
    public ICollection<MainTable> MainTable
    {
        get { return _MainTable; }
        set { _MainTable.Assign(value); }
    }
}