Page 1 of 1

Linq for Oracle: Value cannot be null in Hierarchy table

Posted: Mon 01 Dec 2008 11:15
by dqminh
Hi,

When call the delete method in hierarchy table, it will throw an exception Value cannot be null. Step for reproduce error:

1. Create table Dealer

Code: Select all

CREATE TABLE v2_p_Dealer(
    DealerCode      VARCHAR2(30)      NOT NULL,
    ParentCode      VARCHAR2(30),
    DealerName      NVARCHAR2(256)    NOT NULL,
    CONSTRAINT v2_PK05 PRIMARY KEY (DealerCode)
)
;
Note that ParentCode is nullable.

2. Use Entity Developer to generate linq file (C#)

3. Insert to database a new row

3. Call the delete method

Code: Select all

PartDataContext db = new PartDataContext();
Dealer d = db.Dealers.SingleOrDefault(p => p.Code == Code);
if (d != null)
{
	db.Dealers.DeleteOnSubmit(d);
	db.SubmitChanges();
}
That will throw exception

4. In generated file, make a small change

Code: Select all

[Association(Name = "Dealer_Dealer", Storage = "_Dealer1", ThisKey = "ParentCode", IsForeignKey = true)]
public Dealer Dealer1
{
	get
	{
		return this._Dealer1.Entity;
	}
	set
	{
	...
	}
}
become

Code: Select all

[Association(Name = "Dealer_Dealer", Storage = "_Dealer1", ThisKey = "ParentCode", IsForeignKey = true)]
public Dealer Dealer1
{
	get
	{
		if (!string.IsNullOrEmpty(this.ParentCode)) return this._Dealer1.Entity;
		return null;
	}
	set
	{
	...
	}
}
then it works.

Posted: Mon 01 Dec 2008 12:38
by AndreyR
Please post definitions of the foreign key constraint and the table taking part in the reference.

Posted: Tue 02 Dec 2008 02:05
by dqminh
The reference here:

Code: Select all

ALTER TABLE v2_p_Dealer ADD CONSTRAINT Refv2_p_Dealer41 
    FOREIGN KEY (ParentCode)
    REFERENCES v2_p_Dealer(DealerCode)
;

Posted: Tue 02 Dec 2008 07:19
by AndreyR
Please send me (andreyr * devart * com) an e-mail with your forum nickname, and I will send you the workaround.