C# 不是外键列,不能在此处使用
我是linq、Ajax和c#的新手。我对SQL Server或VB并不陌生。我得到一个错误:C# 不是外键列,不能在此处使用,c#,ajax,linq,C#,Ajax,Linq,我是linq、Ajax和c#的新手。我对SQL Server或VB并不陌生。我得到一个错误: 'ReportTypeID' is not a foreign key column and cannot be used here. 是的,我看了一下,发现了我的一个错误 不,我没有使用视图,所以“”没有什么用处 据我所知,它已正确配置为处理外键 这两个表的配置如下: CREATE TABLE [dbo].[Report]( [Id] [INT] IDENTITY(1,1) NOT NULL
'ReportTypeID' is not a foreign key column and cannot be used here.
是的,我看了一下,发现了我的一个错误
不,我没有使用视图,所以“”没有什么用处
据我所知,它已正确配置为处理外键
这两个表的配置如下:
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
SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue
<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>
SQL联接如下所示:
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
SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue
<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>
最后,适当列上的C#dbml定义(衰减得多)如下所示:
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
SELECT * FROM Report r INNER JOIN ReportType rt ON r.ReportTypeID = rt.TypeValue
<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>
编辑我相信我找到了问题所在,我需要一些文档来找到答案。 代码中有一个switch语句需要编辑。我要进入我的模板,但不是我的领域
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;
编辑
SQL Server将“ReportType”表视为“无效对象”。这可能是问题所在吗?
编辑
我现在正在研究active directory作为一个可能的问题编辑不是active directory问题,“无效对象”错误是智能感知问题(已修复) 编辑:我有部分答案。文件不清楚。答案在于表的dbml定义。幸运的是,我重命名了该表的每个级别,以便在定义中对“ID”的每个级别的引用都有唯一但相关的名称。部分答案是使用dbml表名。我不再得到这个错误。我只是在下拉列表中没有任何数据。见:和 按这个顺序。(第一个解释了第二个的答案) 编辑最后,清除所有垃圾测试并返回到动态对象完成了任务
<asp:DynamicControl runat="server" DataField="ReportTypeTable" DataValueField="TypeValue" DataTextField="TypeDescr" Mode="Edit" UIHint="ForeignKeyRequired" CssClass="general" />
我猜这是你的问题
<Association Name="ReportType_Report" Member="Reports" ThisKey="TypeValue" OtherKey="ReportTypeID" Type="Report" />
ReportType没有引用报表表,相反,您已经定义了它们
终止此关联并查看它是否有效。文档不清楚。答案在于表的DBML定义。幸运的是,我重命名了该表的每个级别,以便在定义中对“ID”的每个级别的引用都有唯一但相关的名称。部分答案是使用DBML表名。我不再得到这个错误。我只是在下拉列表中没有任何数据。见:和 按这个顺序。(第一个解释了第二个的答案) 最后,我返回到对象的原始定义,并将“DataValueField”和“DataTextField”添加到控件中。现在它可以根据需要工作
<asp:DynamicControl runat="server" DataField="ReportTypeTable" DataValueField="TypeValue" DataTextField="TypeDescr" Mode="Edit" UIHint="ForeignKeyRequired" CssClass="general" />
我也试过了。在我修复成员值之前。谢谢-必须再试一次。Ajax字段模板代码“ForeignKeyRequired\u Edit.ascx.cs”中报告了错误。请参见上面的编辑。dbml是正确的。我可以毫无问题地使用它定义的对象。这意味着您的代码中定义模板属性或填充模板的部分已关闭。我已将ReportTypes更改为ReportTypeTables,以确保获得正确的引用。阅读后,您不是在实际创建表ReportType之前创建外键[FK_Report_ReportType]吗?这应该会导致一个错误,我会说。是的,它会-FK是在SQL中创建的,并且运行正常,但在C#中没有。顺便说一句,这也不是active directory的问题。