I'm a newbie at linq, Ajax and c#. I am not new to SQL Server, or VB. I am getting the error:
'ReportTypeID' is not a foreign key column and cannot be used here.
As far as I can tell, it is correctly configured to handle the foreign key.
The two tables are configured as follows:
CREATE TABLE [dbo].[Report](
[Id] [INT] IDENTITY(1,1) NOT NULL,
[ReportTypeID] [INT] NOT NULL,
CONSTRAINT [PK_Report] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Report] ADD CONSTRAINT [DF_Report_ReportTypeID] DEFAULT ((1)) FOR [ReportTypeID]
GO
ALTER TABLE [dbo].[Report] WITH CHECK ADD CONSTRAINT [FK_Report_ReportType] FOREIGN KEY([ReportTypeID])
REFERENCES [dbo].[ReportType] ([TypeValue])
GO
ALTER TABLE [dbo].[Report] CHECK CONSTRAINT [FK_Report_ReportType]
GO
CREATE TABLE [dbo].[ReportType](
[TypeValue] [INT] NOT NULL,
[TypeDescr] [VARCHAR](50) NOT NULL,
CONSTRAINT [PK_ReportType] PRIMARY KEY CLUSTERED
(
[TypeValue] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
The SQL join is as follows:
SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue
Finally, the C# dbml definitions on the appropriate columns (much attenuated) are as follows:
<Table Name="dbo.Report" Member="Reports"><Type Name="Report"><Column Name="ReportTypeID" Type="System.Int32" DbType="INT NOT NULL" CanBeNull="false" /><Association Name="ReportType_Report" Member="ReportType" ThisKey="ReportTypeID" OtherKey="TypeValue" Type="ReportType" IsForeignKey="true" /></Type></Table><Table Name="dbo.ReportType" Member="ReportTypes"><Type Name="ReportType"><Column Name="TypeValue" Type="System.Int32" DbType="INT NOT NULL" IsPrimaryKey="true" CanBeNull="false" /><Association Name="ReportType_Report" Member="Reports" ThisKey="TypeValue" OtherKey="ReportTypeID" Type="Report" /></Type></Table>
The SQL works correctly and there are no dis-joins in the data, the data is correct when I step through the program.
The value of the ReportTypeID=1 which is valid.
The TypeValue in the ReportType table is not auto generated, it is unique and a primary key.
The error is reported in the Ajax field template code "ForeignKeyRequired_Edit.ascx.cs"
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
if (Mode == DataBoundControlMode.Edit)
{
string foreignkey = ForeignKeyColumn.GetForeignKeyString(Row); // Error On This Line
ListItem item = DropDownList1.Items.FindByValue(foreignkey);
if (item != null)
{
DropDownList1.SelectedValue = foreignkey;
}
}
}
There is a switch statement in the code that requires editing. I'm getting to my template, but not to my field.
case "Source": // This Works
items = StaticCache.Sources.AsQueryable().Where(att.WhereClause).Select(r => new ListItem { Text = r.Name, Value = r.Id.ToString() }).ToArray();
break;
// Problem Code - This One Doesn't Work - It Never Gets Here. The Loop that is supposed to pick up this statement has a null definition of the table.
case "ReportTypeTable": // Based On The Table Definition -- Internal Doc Points To This One
items = StaticCache.ReportTypes.AsQueryable().Where(att.WhereClause).Select(r => new ListItem { Text = r.TypeDescr, Value = r.TypeValue.ToString() }).ToArray();
break;
I suspect this has something to do with the dbml definitions. I am still definitely missing something.
See http://stackoverflow.com/questions/14409788/is-not-a-foreign-key-column-and-cannot-be-used-here for some discussion.