[英]Reference to lookup table in C# LINQ using PostgreSQL
今天是個好日子。
我在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
)
在一些查詢表上的參考:
CREATE TABLE table_type
(
table_type integer NOT NULL,
value character varying(100)
CONSTRAINT table_type_id PRIMARY KEY (table_type)
)
並將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; }
}
當我嘗試獲取所有MainTable實體的列表時
(new DataContext(connectionString)).GetTable<MainTable>().MainTables;
從數據庫我得到一個錯誤:
由於對象的當前狀態,操作無效
即使我沒有調用TableType屬性,也會出現此錯誤。 但是當我在TableType之前注釋關聯字符串時,它就消失了。
我哪里出問題了?
我正在使用.Net 3.5,Mono 2.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); }
}
}
不知道它是否正確,但對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.