Is not a foreign key column and cannot be used here

I am new to 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.

      

Yes I went through http://forums.asp.net/t/1254559.aspx and found one of my errors.

No, I am not using views, so " ... is not a foreign key column and cannot be used here? " Was of little use.

As far as I can tell, it is configured correctly to handle 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 connection looks like this:

SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue

      

Finally, the C # dbml definitions in the respective columns are (heavily relaxed) 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 is working correctly and there is no unrelated data in the data, the data is correct when I view the program. The value of the actual parameter ReportTypeID = 1. The typeValue in the ReportType table is not automatically generated, it is unique and has a primary key. I suspect it has something to do with the dbml definitions. I am still definitely missing something. edit: Add visual attitude. The setting is based on a different relationship in the image that worksVisual Relation Of Tables

The error is specified in the "ForeignKeyRequired_Edit.ascx.cs" Ajax field code template

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;
    }
  }
}

      


Edit I believe I have found the problem, I need the documentation to find the answer. There is a switch statement in the code that needs editing. I get to my template, but not for 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 - Not Sure Which Of These To Use
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;

      


Edit
The "ReportType" table is considered an "invalid object" by SQL Server. Could this be the problem?
edit
I am considering active directory as a possible problem. Edit Not an active folder problem, but the "invalid object" error was an intellesense issue (fixed).

Edit: I have part of the answer. The documentation is not clear. The answer lies in the definition of the dbml table. Fortunately, I renamed each level of this table so that references to each of the "ID" levels in the definition had unique but related names. The partial answer is to use the dbml table name. I don't get this error anymore. I just don't have any data in the dropdown. See: http://www.seekwaytech.com/2011/02/27/asp-net-4-0-dynamic-data-foreign-keys-show-up-in-a-text-box/ and http: / /forums.asp.net/t/1254559.aspx in this sequence. (The first explains the answer to the second)

Edit Finally, getting rid of all the garbage tests and returning to a dynamic object did the job.

<asp:DynamicControl runat="server" DataField="ReportTypeTable" DataValueField="TypeValue" DataTextField="TypeDescr" Mode="Edit" UIHint="ForeignKeyRequired" CssClass="general" /> 

      

+3


source to share


2 answers


The documentation is unclear. The answer lies in the definition of the DBML table. Fortunately, I renamed each level of this table so that references to each of the "ID" levels in the definition had unique but related names. The partial answer is to use the DBML table name. I don't get this error anymore. I just don't have any data in the dropdown. See: http://www.seekwaytech.com/2011/02/27/asp-net-4-0-dynamic-data-foreign-keys-show-up-in-a-text-box/ and http: / /forums.asp.net/t/1254559.aspx in this sequence. (The first explains the answer to the second)

Finally, I went back to the original object definition and added the DataValueField and DataTextField controls. Now it works as needed.



<asp:DynamicControl runat="server" DataField="ReportTypeTable" DataValueField="TypeValue" DataTextField="TypeDescr" Mode="Edit" UIHint="ForeignKeyRequired" CssClass="general" /> 

      

0


source


My guess is your problem ...

 <Association Name="ReportType_Report" Member="Reports" ThisKey="TypeValue" OtherKey="ReportTypeID" Type="Report" />

      



The ReportType does not refer to the report table, it is the other way around, but you have defined both of them.

Kill this association and see if it works.

+1


source







All Articles