CREATE TABLE ConfigurationItem
(
OID BIGINT NOT NULL
,ParentItemOID BIGINT
);
ALTER TABLE ConfigurationItem ADD CONSTRAINT PK_CONFIGURATIONITEM PRIMARY KEY (OID);
ALTER TABLE ConfigurationItem ADD CONSTRAINT FK_CONFIGURATIONITEM_PARENTITEMOID FOREIGN KEY (ParentItemOID ) REFERENCES CONFIGURATIONITEM(OID);每次获取数据ConfigurationItem时,我都希望得到父ConfigurationItem和子ConfigurationItems的列表,并且没有递归。
这是创建的实体
[Table("ConfigurationItem", Schema = "dbo")]
public partial class ConfigurationItem : TaggableItem
{
public Int64 OID { get; set; }
public Int64? ParentItemOID { get; set; }
[ForeignKey("ParentItemOID")]
public ConfigurationItem Parent;
[InverseProperty("ParentItemOID")]
//Not a virtual because it is need to be marshalled via WCF
public List<ConfigurationItem> Children { get; set; }
}我不能让它起作用。
出现以下错误示例:
InnerException: System.Data.SqlClient.SqlException
HResult=-2146232060
Message=Invalid column name 'ConfigurationItem_OID'.
Source=.Net SqlClient Data Provider
ErrorCode=-2146232060
Class=16
LineNumber=32
Number=207
Procedure=""
Server=localhost
State=1在实体框架中工作的正确方法是什么?
发布于 2013-04-02 02:05:56
我认为您最初的异常源于EF使用的默认命名约定。将ConfigurationItem类中的OID属性重命名为Configuration_OID,或使用OID属性上的列批注来指示要覆盖该列的默认命名约定,例如列(“OID”)。
我有一个具有这样的自引用表的模式,并且我发现没有必要使用InverseProperty注释,所以您可以直接去掉它。
https://stackoverflow.com/questions/15085647
复制相似问题