LINQ2SQL: How to let a column accept null values as zero (0) in Self-Relation table

Posted by Remon on Stack Overflow See other posts from Stack Overflow or by Remon
Published on 2011-01-11T10:49:17Z Indexed on 2011/01/11 10:53 UTC
Read the original article Hit count: 179

alt text

As described in the img, I got a parent-Children relation and since the ParentID not accepting null values (and I can't change to nullabel due to some restriction in the UI I have), how can I remove an existence relation between ReportDataSources in order to change the parent for them (here i want to set the parentId for one of them = 0) how could i do that since i cant change the ParentID directly and setting Parent = null is not valid

public void SetReportDataSourceAsMaster(ReportDataSource reportDataSource)
{
//Some logic - not necessarily for this scenario 
            //Reset Master
        this.ReportDataSources.ToList().ForEach(rds => rds.IsMaster = false);
        //Set Master
        reportDataSource.IsMaster = true;
        //Set Parent ID for the rest of the Reports data sources
        this.ReportDataSources.Where(rds => rds.ID != reportDataSource.ID).ToList().ForEach(rds =>
            {
                //Change Parent ID
                rds.Parent = reportDataSource;
                //Remove filttering data
                rds.FilteringDataMembers.Clear();
                //Remove Grouping Data
                rds.GroupingDataMembers.Clear();
            });

            //Delete parent HERE THE EXCEPTION THROWN AFTER CALLING SUBMITCHANGES()
            reportDataSource.Parent = null;

            //Other logic
        }

Exception thrown after calling submitChanges An attempt was made to remove a relationship between a ReportDataSource and a ReportDataSource. However, one of the relationship's foreign keys (ReportDataSource.ParentID) cannot be set to null.

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql