获取所有引用列到linq中的引用列 [英] Get all referencing columns to a referenced column in linq

查看:167
本文介绍了获取所有引用列到linq中的引用列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个SalesOrderId的销售表。然后你有两个表SalesOrderHeader和SalesReport引用SalesOrderId。从销售表和SalesOrderId开始,有没有什么办法可以找出这两个表引用它使用LINQ?我想添加一个Debug.Assert到我的代码,这样我就可以更新这段代码,每当有一个列的外键约束添加。

解决方案

在数据库方面,这些可能有帮助:

$ p $ c $ SELECT
FROM INFORMATION_SCHEMA
WHERE CONSTRAINT_TYPE ='FOREIGN KEY'

SELECT [name] as FKeyName,
(SELECT o1。[name] from sys.objects o1 where
o1。[作为ParentTableName,
(SELECT o2。[name] from sys.objects o2 where
o2。[object_id] = fk.referenced_object_id)作为ReferencedTableName
FROM sys。 foreign_keys fk


$ b $ p
$ b如果你真的需要列名,请打开 sp_helpconstraint <



在设计者方面,dbml文件是xml ...查找Database.Table.Type.Association


Say you have a Table named Sales with a SalesOrderId. Then you have two tables SalesOrderHeader and SalesReport that reference SalesOrderId. Starting from the Sales Tables and SalesOrderId, is there any way to figure out those two table reference it using linq? I want to add a Debug.Assert to my code so that I can update that piece of code whenever there is another foreign key constraint added against a column.

解决方案

On the database side, these might help:

SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'

SELECT [name] as FKeyName,
  (SELECT o1.[name] from sys.objects o1 where
    o1.[object_id] = fk.parent_object_id) as ParentTableName,
  (SELECT o2.[name] from sys.objects o2 where
    o2.[object_id] = fk.referenced_object_id) as ReferencedTableName
FROM sys.foreign_keys fk

If you really need the column names, pop open sp_helpconstraint in the system stored procedures and extract the logic from there.


On the designer side, the dbml file is xml... look for Database.Table.Type.Association

这篇关于获取所有引用列到linq中的引用列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆