转:http://www.cnblogs.com/LingzhiSun/archive/2011/06/13/EFPowerTool_2.html
上次为大家介绍EF Power Tool之后,不少朋友在使用的时候碰到了一些问题曾像我提问。我自己以及同事在使用这个工具时,其实也碰到了一些问题。今天我将和大家一起分享其中2个问题以及相应的原因。
1. EF Power Tool帮助我们生成Code First POCO class时为何只生成了部分代码,甚至所使用的程序集都没有导入?
在输入了相应的数据库Named Instance和登录信息后,EF Power Tool开始为我们创建相应的class。此时使用SQL Server Profiler同时观察SQL Server在本地的Named Instance和SQL Express Instance,我们会发现一系列数据库访问在Named Instance上执行了。
1) 首先是这样一个查询。以上篇文章中的Parent和Child类为例子,会返回这些用户级别表字段的数据。其中每个字段分别表示
C1: 每个字段在表中的序号
CatalogName: 数据库名
SchemaName:表的schema名
Name:表名
C2:字段名
C3:是否为NULL
C4:字段类型
C5:字段最大长度
C6:字段精度
C7:DateTime的精度
C8:字段刻度
C9:是否为Identity
C10:是否为数据库自动生成
C11:是否为主键
SELECT
[Project6].[C2] AS [C1],
[Project6].[CatalogName] AS [CatalogName],
[Project6].[SchemaName] AS [SchemaName],
[Project6].[Name] AS [Name],
[Project6].[C1] AS [C2],
[Project6].[C3] AS [C3],
[Project6].[C4] AS [C4],
[Project6].[C5] AS [C5],
[Project6].[C6] AS [C6],
[Project6].[C7] AS [C7],
[Project6].[C8] AS [C8],
[Project6].[C9] AS [C9],
[Project6].[C10] AS [C10],
[Project6].[C11] AS [C11]
FROM ( SELECT
[Extent1].[CatalogName] AS [CatalogName],
[Extent1].[SchemaName] AS [SchemaName],
[Extent1].[Name] AS [Name],
[UnionAll1].[Name] AS [C1],
[UnionAll1].[Ordinal] AS [C2],
[UnionAll1].[IsNullable] AS [C3],
[UnionAll1].[TypeName] AS [C4],
[UnionAll1].[MaxLength] AS [C5],
[UnionAll1].[Precision] AS [C6],
[UnionAll1].[DateTimePrecision] AS [C7],
[UnionAll1].[Scale] AS [C8],
[UnionAll1].[IsIdentity] AS [C9],
[UnionAll1].[IsStoreGenerated] AS [C10],
CASE WHEN ([Project5].[C2] IS NULL) THEN cast(0 as bit) ELSE [Project5].[C2] END AS [C11]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
) AS [Extent1]
INNER JOIN (SELECT
[Extent2].[Id] AS [Id],
[Extent2].[Name] AS [Name],
[Extent2].[Ordinal] AS [Ordinal],
[Extent2].[IsNullable] AS [IsNullable],
[Extent2].[TypeName] AS [TypeName],
[Extent2].[MaxLength] AS [MaxLength],
[Extent2].[Precision] AS [Precision],
[Extent2].[DateTimePrecision] AS [DateTimePrecision],
[Extent2].[Scale] AS [Scale],
[Extent2].[IsIdentity] AS [IsIdentity],
[Extent2].[IsStoreGenerated] AS [IsStoreGenerated],
0 AS [C1],
[Extent2].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent2]
UNION ALL
SELECT
[Extent3].[Id] AS [Id],
[Extent3].[Name] AS [Name],
[Extent3].[Ordinal] AS [Ordinal],
[Extent3].[IsNullable] AS [IsNullable],
[Extent3].[TypeName] AS [TypeName],
[Extent3].[MaxLength] AS [MaxLength],
[Extent3].[Precision] AS [Precision],
[Extent3].[DateTimePrecision] AS [DateTimePrecision],
[Extent3].[Scale] AS [Scale],
[Extent3].[IsIdentity] AS [IsIdentity],
[Extent3].[IsStoreGenerated] AS [IsStoreGenerated],
6 AS [C1],
[Extent3].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent3]) AS [UnionAll1] ON (0 = [UnionAll1].[C1]) AND ([Extent1].[Id] = [UnionAll1].[ParentId])
LEFT OUTER JOIN (SELECT
[UnionAll2].[Id] AS [C1],
cast(1 as bit) AS [C2]
FROM (
SELECT
quotename(tc.CONSTRAINT_SCHEMA) + quotename(tc.CONSTRAINT_NAME) [Id]
, quotename(tc.TABLE_SCHEMA) + quotename(tc.TABLE_NAME) [ParentId]
, tc.CONSTRAINT_NAME [Name]
, tc.CONSTRAINT_TYPE [ConstraintType]
, CAST(CASE tc.IS_DEFERRABLE WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsDeferrable]
, CAST(CASE tc.INITIALLY_DEFERRED WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsInitiallyDeferred]
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
WHERE tc.TABLE_NAME IS NOT NULL
) AS [Extent4]
INNER JOIN (SELECT
7 AS [C1],
[Extent5].[ConstraintId] AS [ConstraintId],
[Extent6].[Id] AS [Id]
FROM (
SELECT
quotename(CONSTRAINT_SCHEMA) + quotename(CONSTRAINT_NAME) [ConstraintId]
, quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) + quotename(COLUMN_NAME) [ColumnId]
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
) AS [Extent5]
INNER JOIN (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent6] ON [Extent6].[Id] = [Extent5].[ColumnId]
UNION ALL
SELECT
11 AS [C1],
[Extent7].[ConstraintId] AS [ConstraintId],
[Extent8].[Id] AS [Id]
FROM (
SELECT
CAST(NULL as nvarchar(1)) [ConstraintId]
, CAST(NULL as nvarchar(max)) [ColumnId]
WHERE 1=2
) AS [Extent7]
INNER JOIN (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent8] ON [Extent8].[Id] = [Extent7].[ColumnId]) AS [UnionAll2] ON (7 = [UnionAll2].[C1]) AND ([Extent4].[Id] = [UnionAll2].[ConstraintId])
WHERE [Extent4].[ConstraintType] = 'PRIMARY KEY' ) AS [Project5] ON [UnionAll1].[Id] = [Project5].[C1]
) AS [Project6]
ORDER BY [Project6].[SchemaName] ASC, [Project6].[Name] ASC, [Project6].[C2] ASC
2) 然后这个查询返回系统级表的字段信息。
SELECT
[Project6].[C2] AS [C1],
[Project6].[CatalogName] AS [CatalogName],
[Project6].[SchemaName] AS [SchemaName],
[Project6].[Name] AS [Name],
[Project6].[C1] AS [C2],
[Project6].[C3] AS [C3],
[Project6].[C4] AS [C4],
[Project6].[C5] AS [C5],
[Project6].[C6] AS [C6],
[Project6].[C7] AS [C7],
[Project6].[C8] AS [C8],
[Project6].[C9] AS [C9],
[Project6].[C10] AS [C10],
[Project6].[C11] AS [C11]
FROM ( SELECT
[Extent1].[CatalogName] AS [CatalogName],
[Extent1].[SchemaName] AS [SchemaName],
[Extent1].[Name] AS [Name],
[UnionAll1].[Name] AS [C1],
[UnionAll1].[Ordinal] AS [C2],
[UnionAll1].[IsNullable] AS [C3],
[UnionAll1].[TypeName] AS [C4],
[UnionAll1].[MaxLength] AS [C5],
[UnionAll1].[Precision] AS [C6],
[UnionAll1].[DateTimePrecision] AS [C7],
[UnionAll1].[Scale] AS [C8],
[UnionAll1].[IsIdentity] AS [C9],
[UnionAll1].[IsStoreGenerated] AS [C10],
CASE WHEN ([Project5].[C2] IS NULL) THEN cast(0 as bit) ELSE [Project5].[C2] END AS [C11]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
, VIEW_DEFINITION [ViewDefinition]
, CAST( CASE IS_UPDATABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsUpdatable]
FROM
INFORMATION_SCHEMA.VIEWS
WHERE
NOT (TABLE_SCHEMA = 'dbo'
AND TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent1]
INNER JOIN (SELECT
[Extent2].[Id] AS [Id],
[Extent2].[Name] AS [Name],
[Extent2].[Ordinal] AS [Ordinal],
[Extent2].[IsNullable] AS [IsNullable],
[Extent2].[TypeName] AS [TypeName],
[Extent2].[MaxLength] AS [MaxLength],
[Extent2].[Precision] AS [Precision],
[Extent2].[DateTimePrecision] AS [DateTimePrecision],
[Extent2].[Scale] AS [Scale],
[Extent2].[IsIdentity] AS [IsIdentity],
[Extent2].[IsStoreGenerated] AS [IsStoreGenerated],
4 AS [C1],
[Extent2].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent2]
UNION ALL
SELECT
[Extent3].[Id] AS [Id],
[Extent3].[Name] AS [Name],
[Extent3].[Ordinal] AS [Ordinal],
[Extent3].[IsNullable] AS [IsNullable],
[Extent3].[TypeName] AS [TypeName],
[Extent3].[MaxLength] AS [MaxLength],
[Extent3].[Precision] AS [Precision],
[Extent3].[DateTimePrecision] AS [DateTimePrecision],
[Extent3].[Scale] AS [Scale],
[Extent3].[IsIdentity] AS [IsIdentity],
[Extent3].[IsStoreGenerated] AS [IsStoreGenerated],
0 AS [C1],
[Extent3].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent3]) AS [UnionAll1] ON (0 = [UnionAll1].[C1]) AND ([Extent1].[Id] = [UnionAll1].[ParentId])
LEFT OUTER JOIN (SELECT
[UnionAll2].[Id] AS [C1],
cast(1 as bit) AS [C2]
FROM (
SELECT
CAST(NULL as nvarchar(1)) [Id]
, CAST(NULL as nvarchar(256)) [ParentId]
, CAST(NULL as nvarchar(256)) [Name]
, CAST(NULL as nvarchar(256)) [ConstraintType]
, CAST(0 as bit) [IsDeferrable]
, CAST(0 as bit) [IsInitiallyDeferred]
, CAST(NULL as nvarchar(max)) [Expression]
, CAST(NULL as nvarchar(11)) [UpdateRule]
, CAST(NULL as nvarchar(11)) [DeleteRule]
WHERE 1=2
) AS [Extent4]
INNER JOIN (SELECT
10 AS [C1],
[Extent5].[ConstraintId] AS [ConstraintId],
[Extent6].[Id] AS [Id]
FROM (
SELECT
quotename(CONSTRAINT_SCHEMA) + quotename(CONSTRAINT_NAME) [ConstraintId]
, quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) + quotename(COLUMN_NAME) [ColumnId]
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
) AS [Extent5]
INNER JOIN (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent6] ON [Extent6].[Id] = [Extent5].[ColumnId]
UNION ALL
SELECT
7 AS [C1],
[Extent7].[ConstraintId] AS [ConstraintId],
[Extent8].[Id] AS [Id]
FROM (
SELECT
CAST(NULL as nvarchar(1)) [ConstraintId]
, CAST(NULL as nvarchar(max)) [ColumnId]
WHERE 1=2
) AS [Extent7]
INNER JOIN (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent8] ON [Extent8].[Id] = [Extent7].[ColumnId]) AS [UnionAll2] ON (7 = [UnionAll2].[C1]) AND ([Extent4].[Id] = [UnionAll2].[ConstraintId])
WHERE [Extent4].[ConstraintType] = 'PRIMARY KEY' ) AS [Project5] ON [UnionAll1].[Id] = [Project5].[C1]
) AS [Project6]
ORDER BY [Project6].[SchemaName] ASC, [Project6].[Name] ASC, [Project6].[C2] ASC
3) 此查询返回数据库中所有的外键关系。以Parent和Child为例子,返回这样一个表格:
SELECT
[Project11].[C1] AS [C1],
[Project11].[C5] AS [C2],
[Project11].[C6] AS [C3],
[Project11].[C4] AS [C4],
[Project11].[C2] AS [C5],
[Project11].[C8] AS [C6],
[Project11].[C9] AS [C7],
[Project11].[C7] AS [C8],
[Project11].[C3] AS [C9],
[Project11].[Name] AS [Name],
[Project11].[Id] AS [Id],
[Project11].[C10] AS [C10]
FROM ( SELECT
[Extent1].[Id] AS [Id],
[Extent2].[Name] AS [Name],
[Join5].[Ordinal] AS [C1],
[Join5].[Name1] AS [C2],
[Join5].[Name2] AS [C3],
[UnionAll4].[Name] AS [C4],
[UnionAll4].[CatalogName] AS [C5],
[UnionAll4].[SchemaName] AS [C6],
[UnionAll5].[Name] AS [C7],
[UnionAll5].[CatalogName] AS [C8],
[UnionAll5].[SchemaName] AS [C9],
CASE WHEN ([Extent1].[DeleteRule] = 'CASCADE') THEN cast(1 as bit) WHEN ([Extent1].[DeleteRule] <> 'CASCADE') THEN cast(0 as bit) END AS [C10]
FROM (
SELECT
quotename(rc.CONSTRAINT_SCHEMA) + quotename(rc.CONSTRAINT_NAME) [Id]
, CAST(rc.UPDATE_RULE as nvarchar(11)) [UpdateRule]
, CAST(rc.DELETE_RULE as nvarchar(11)) [DeleteRule]
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
) AS [Extent1]
INNER JOIN (
SELECT
quotename(tc.CONSTRAINT_SCHEMA) + quotename(tc.CONSTRAINT_NAME) [Id]
, quotename(tc.TABLE_SCHEMA) + quotename(tc.TABLE_NAME) [ParentId]
, tc.CONSTRAINT_NAME [Name]
, tc.CONSTRAINT_TYPE [ConstraintType]
, CAST(CASE tc.IS_DEFERRABLE WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsDeferrable]
, CAST(CASE tc.INITIALLY_DEFERRED WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsInitiallyDeferred]
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
WHERE tc.TABLE_NAME IS NOT NULL
) AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
INNER JOIN (SELECT [UnionAll1].[Ordinal] AS [Ordinal], [UnionAll1].[C1] AS [C11], [UnionAll1].[ConstraintId] AS [ConstraintId], [Join2].[Id1], [UnionAll2].[Name] AS [Name1], [UnionAll2].[C2] AS [C21], [UnionAll2].[ParentId] AS [ParentId1], [UnionAll3].[Name] AS [Name2]
FROM (SELECT
[Extent3].[Ordinal] AS [Ordinal],
0 AS [C1],
[Extent3].[ConstraintId] AS [ConstraintId],
6 AS [C2],
[Extent3].[FromColumnId] AS [FromColumnId],
6 AS [C3],
[Extent3].[ToColumnId] AS [ToColumnId]
FROM (
SELECT
quotename(FC.CONSTRAINT_SCHEMA) + quotename(FC.CONSTRAINT_NAME) + quotename(cast(FC.ORDINAL_POSITION as nvarchar(30))) [Id]
, quotename(PC.TABLE_SCHEMA) + quotename(PC.TABLE_NAME) + quotename(PC.COLUMN_NAME) [ToColumnId]
, quotename(FC.TABLE_SCHEMA) + quotename(FC.TABLE_NAME) + quotename(FC.COLUMN_NAME) [FromColumnId]
, quotename(FC.CONSTRAINT_SCHEMA) + quotename(FC.CONSTRAINT_NAME) [ConstraintId]
, FC.ORDINAL_POSITION [Ordinal]
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS PC /* PRIMARY KEY COLS*/
ON RC.UNIQUE_CONSTRAINT_SCHEMA = PC.CONSTRAINT_SCHEMA
AND RC.UNIQUE_CONSTRAINT_NAME = PC.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS FC /* FOREIGN KEY COLS*/
ON RC.CONSTRAINT_SCHEMA = FC.CONSTRAINT_SCHEMA
AND RC.CONSTRAINT_NAME = FC.CONSTRAINT_NAME
AND PC.ORDINAL_POSITION = FC.ORDINAL_POSITION
) AS [Extent3]
UNION ALL
SELECT
[Extent4].[Ordinal] AS [Ordinal],
9 AS [C1],
[Extent4].[ConstraintId] AS [ConstraintId],
10 AS [C2],
[Extent4].[FromColumnId] AS [FromColumnId],
10 AS [C3],
[Extent4].[ToColumnId] AS [ToColumnId]
FROM (
SELECT
CAST(NULL as nvarchar(1)) [Id]
, CAST(NULL as nvarchar(max)) [ToColumnId]
, CAST(NULL as nvarchar(max)) [FromColumnId]
, CAST(NULL as nvarchar(1)) [ConstraintId]
, 0 [Ordinal]
WHERE 1=2
) AS [Extent4]) AS [UnionAll1]
INNER JOIN (SELECT [Extent5].[Id] AS [Id1]
FROM (
SELECT
quotename(rc.CONSTRAINT_SCHEMA) + quotename(rc.CONSTRAINT_NAME) [Id]
, CAST(rc.UPDATE_RULE as nvarchar(11)) [UpdateRule]
, CAST(rc.DELETE_RULE as nvarchar(11)) [DeleteRule]
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
) AS [Extent5]
INNER JOIN (
SELECT
quotename(tc.CONSTRAINT_SCHEMA) + quotename(tc.CONSTRAINT_NAME) [Id]
, quotename(tc.TABLE_SCHEMA) + quotename(tc.TABLE_NAME) [ParentId]
, tc.CONSTRAINT_NAME [Name]
, tc.CONSTRAINT_TYPE [ConstraintType]
, CAST(CASE tc.IS_DEFERRABLE WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsDeferrable]
, CAST(CASE tc.INITIALLY_DEFERRED WHEN 'NO' THEN 0 ELSE 1 END as bit) [IsInitiallyDeferred]
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
WHERE tc.TABLE_NAME IS NOT NULL
) AS [Extent6] ON [Extent5].[Id] = [Extent6].[Id] ) AS [Join2] ON ([UnionAll1].[C1] = 0) AND ([UnionAll1].[ConstraintId] = [Join2].[Id1])
LEFT OUTER JOIN (SELECT
6 AS [C1],
[Extent7].[Id] AS [Id],
[Extent7].[Name] AS [Name],
3 AS [C2],
[Extent7].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent7]
UNION ALL
SELECT
10 AS [C1],
[Extent8].[Id] AS [Id],
[Extent8].[Name] AS [Name],
12 AS [C2],
[Extent8].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent8]) AS [UnionAll2] ON ([UnionAll1].[C3] = [UnionAll2].[C1]) AND ([UnionAll1].[ToColumnId] = [UnionAll2].[Id])
LEFT OUTER JOIN (SELECT
6 AS [C1],
[Extent9].[Id] AS [Id],
[Extent9].[Name] AS [Name]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer)[DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT as [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.TABLES t ON
c.TABLE_CATALOG = t.TABLE_CATALOG AND
c.TABLE_SCHEMA = t.TABLE_SCHEMA AND
c.TABLE_NAME = t.TABLE_NAME AND
t.TABLE_TYPE = 'BASE TABLE'
) AS [Extent9]
UNION ALL
SELECT
10 AS [C1],
[Extent10].[Id] AS [Id],
[Extent10].[Name] AS [Name]
FROM (
SELECT
quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) + quotename(c.COLUMN_NAME) [Id]
, quotename(c.TABLE_SCHEMA) + quotename(c.TABLE_NAME) [ParentId]
, c.COLUMN_NAME [Name]
, c.ORDINAL_POSITION [Ordinal]
, CAST( CASE c.IS_NULLABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsNullable]
, CASE
WHEN c.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
c.CHARACTER_MAXIMUM_LENGTH = -1 THEN
c.DATA_TYPE + '(max)'
ELSE
c.DATA_TYPE
END
as [TypeName]
, c.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(c.NUMERIC_PRECISION as integer) [Precision]
, CAST(c.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(c.NUMERIC_SCALE as integer) [Scale]
, c.COLLATION_CATALOG [CollationCatalog]
, c.COLLATION_SCHEMA [CollationSchema]
, c.COLLATION_NAME [CollationName]
, c.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, c.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, c.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsIdentity' ) as bit) as [IsIdentity]
, CAST(columnproperty( object_id(quotename(c.TABLE_SCHEMA) + '.' + quotename(c.TABLE_NAME)), c.COLUMN_NAME, 'IsComputed' ) | CASE WHEN c.DATA_TYPE = 'timestamp' THEN 1 ELSE 0 END as bit) as [IsStoreGenerated]
, c.COLUMN_DEFAULT [Default]
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
INFORMATION_SCHEMA.VIEWS v ON
c.TABLE_CATALOG = v.TABLE_CATALOG AND
c.TABLE_SCHEMA = v.TABLE_SCHEMA AND
c.TABLE_NAME = v.TABLE_NAME
WHERE
NOT (v.TABLE_SCHEMA = 'dbo'
AND v.TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent10]) AS [UnionAll3] ON ([UnionAll1].[C2] = [UnionAll3].[C1]) AND ([UnionAll1].[FromColumnId] = [UnionAll3].[Id]) ) AS [Join5] ON (0 = [Join5].[C11]) AND ([Extent1].[Id] = [Join5].[ConstraintId]) AND ([Join5].[Id1] = [Extent1].[Id])
LEFT OUTER JOIN (SELECT
3 AS [C1],
[Extent11].[Id] AS [Id],
[Extent11].[Name] AS [Name],
[Extent11].[CatalogName] AS [CatalogName],
[Extent11].[SchemaName] AS [SchemaName]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
) AS [Extent11]
UNION ALL
SELECT
12 AS [C1],
[Extent12].[Id] AS [Id],
[Extent12].[Name] AS [Name],
[Extent12].[CatalogName] AS [CatalogName],
[Extent12].[SchemaName] AS [SchemaName]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
, VIEW_DEFINITION [ViewDefinition]
, CAST( CASE IS_UPDATABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsUpdatable]
FROM
INFORMATION_SCHEMA.VIEWS
WHERE
NOT (TABLE_SCHEMA = 'dbo'
AND TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent12]) AS [UnionAll4] ON ([Join5].[C21] = [UnionAll4].[C1]) AND ([Join5].[ParentId1] = [UnionAll4].[Id])
LEFT OUTER JOIN (SELECT
3 AS [C1],
[Extent13].[Id] AS [Id],
[Extent13].[Name] AS [Name],
[Extent13].[CatalogName] AS [CatalogName],
[Extent13].[SchemaName] AS [SchemaName]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
) AS [Extent13]
UNION ALL
SELECT
12 AS [C1],
[Extent14].[Id] AS [Id],
[Extent14].[Name] AS [Name],
[Extent14].[CatalogName] AS [CatalogName],
[Extent14].[SchemaName] AS [SchemaName]
FROM (
SELECT
quotename(TABLE_SCHEMA) + quotename(TABLE_NAME) [Id]
, TABLE_CATALOG [CatalogName]
, TABLE_SCHEMA [SchemaName]
, TABLE_NAME [Name]
, VIEW_DEFINITION [ViewDefinition]
, CAST( CASE IS_UPDATABLE WHEN 'YES' THEN 1 WHEN 'NO' THEN 0 ELSE 0 END as bit) [IsUpdatable]
FROM
INFORMATION_SCHEMA.VIEWS
WHERE
NOT (TABLE_SCHEMA = 'dbo'
AND TABLE_NAME in('syssegments', 'sysconstraints')
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
) AS [Extent14]) AS [UnionAll5] ON (3 = [UnionAll5].[C1]) AND ([Extent2].[ParentId] = [UnionAll5].[Id])
) AS [Project11]
ORDER BY [Project11].[Name] ASC, [Project11].[Id] ASC, [Project11].[C1] ASC
4) 最后这个查询返回数据库中存储过程和函数的相关信息。
SELECT
[Project7].[C12] AS [C1],
[Project7].[C1] AS [C2],
[Project7].[C2] AS [C3],
[Project7].[C3] AS [C4],
[Project7].[C4] AS [C5],
[Project7].[C5] AS [C6],
[Project7].[C6] AS [C7],
[Project7].[C7] AS [C8],
[Project7].[C8] AS [C9],
[Project7].[C9] AS [C10],
[Project7].[C10] AS [C11]
FROM ( SELECT
[UnionAll3].[SchemaName] AS [C1],
[UnionAll3].[Name] AS [C2],
[UnionAll3].[ReturnTypeName] AS [C3],
[UnionAll3].[IsAggregate] AS [C4],
[UnionAll3].[C1] AS [C5],
[UnionAll3].[IsBuiltIn] AS [C6],
[UnionAll3].[IsNiladic] AS [C7],
[UnionAll3].[C2] AS [C8],
[UnionAll3].[C3] AS [C9],
[UnionAll3].[C4] AS [C10],
[UnionAll3].[C5] AS [C11],
1 AS [C12]
FROM (SELECT
[Extent1].[SchemaName] AS [SchemaName],
[Extent1].[Name] AS [Name],
[Extent1].[ReturnTypeName] AS [ReturnTypeName],
[Extent1].[IsAggregate] AS [IsAggregate],
cast(1 as bit) AS [C1],
[Extent1].[IsBuiltIn] AS [IsBuiltIn],
[Extent1].[IsNiladic] AS [IsNiladic],
[UnionAll1].[Name] AS [C2],
[UnionAll1].[TypeName] AS [C3],
[UnionAll1].[Mode] AS [C4],
[UnionAll1].[Ordinal] AS [C5]
FROM (
SELECT
quotename(SPECIFIC_SCHEMA) + quotename(SPECIFIC_NAME) [Id]
, SPECIFIC_CATALOG [CatalogName]
, SPECIFIC_SCHEMA [SchemaName]
, SPECIFIC_NAME [Name]
, CASE
WHEN DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
CHARACTER_MAXIMUM_LENGTH = -1 THEN
DATA_TYPE + '(max)'
ELSE
DATA_TYPE
END [ReturnTypeName]
, CHARACTER_MAXIMUM_LENGTH [ReturnMaxLength]
, CAST(NUMERIC_PRECISION as integer) [ReturnPrecision]
, CAST(DATETIME_PRECISION as integer) [ReturnDateTimePrecision]
, CAST(NUMERIC_SCALE as integer) [ReturnScale]
, COLLATION_CATALOG [ReturnCollationCatalog]
, COLLATION_SCHEMA [ReturnCollationSchema]
, COLLATION_NAME [ReturnCollationName]
, CHARACTER_SET_CATALOG [ReturnCharacterSetCatalog]
, CHARACTER_SET_SCHEMA [ReturnCharacterSetSchema]
, CHARACTER_SET_NAME [ReturnCharacterSetName]
, CAST(0 as bit) as [ReturnIsMultiSet]
, CAST(0 as bit) as [IsAggregate]
, CAST(0 as bit) as [IsBuiltIn]
, CAST(0 as bit) as [IsNiladic]
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
NOT (ROUTINE_SCHEMA = 'dbo'
AND ROUTINE_NAME LIKE 'dt[_]%'
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
AND (DATA_TYPE != 'TABLE' OR DATA_TYPE is null)
AND ROUTINE_TYPE = 'FUNCTION'
) AS [Extent1]
LEFT OUTER JOIN (SELECT
[Extent2].[Name] AS [Name],
[Extent2].[Ordinal] AS [Ordinal],
[Extent2].[TypeName] AS [TypeName],
[Extent2].[Mode] AS [Mode],
0 AS [C1],
[Extent2].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(f.SPECIFIC_SCHEMA) + quotename(f.SPECIFIC_NAME) + quotename(f.PARAMETER_NAME) [Id]
, quotename(f.SPECIFIC_SCHEMA) + quotename(f.SPECIFIC_NAME) [ParentId]
, CASE -- trim off the @ symbol
WHEN f.PARAMETER_NAME is null THEN NULL
ELSE SUBSTRING(f.PARAMETER_NAME, 2, LEN(f.PARAMETER_NAME) )
END [Name]
, f.ORDINAL_POSITION [Ordinal]
, CASE
WHEN f.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
f.CHARACTER_MAXIMUM_LENGTH = -1 THEN
f.DATA_TYPE + '(max)'
ELSE
f.DATA_TYPE
END [TypeName]
, f.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(f.NUMERIC_PRECISION as integer) [Precision]
, CAST(f.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(f.NUMERIC_SCALE as integer) [Scale]
, f.COLLATION_CATALOG [CollationCatalog]
, f.COLLATION_SCHEMA [CollationSchema]
, f.COLLATION_NAME [CollationName]
, f.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, f.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, f.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, f.PARAMETER_MODE [Mode]
, CAST(NULL as nvarchar(max)) [Default]
FROM
INFORMATION_SCHEMA.PARAMETERS f
INNER JOIN INFORMATION_SCHEMA.ROUTINES r ON
f.SPECIFIC_SCHEMA = r.SPECIFIC_SCHEMA AND
f.SPECIFIC_NAME = r.SPECIFIC_NAME AND
r.ROUTINE_TYPE = 'FUNCTION'
WHERE
f.IS_RESULT = 'NO'
) AS [Extent2]
UNION ALL
SELECT
[Extent3].[Name] AS [Name],
[Extent3].[Ordinal] AS [Ordinal],
[Extent3].[TypeName] AS [TypeName],
[Extent3].[Mode] AS [Mode],
6 AS [C1],
[Extent3].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(p.SPECIFIC_SCHEMA) + quotename(p.SPECIFIC_NAME) + quotename(p.PARAMETER_NAME) [Id]
, quotename(p.SPECIFIC_SCHEMA) + quotename(p.SPECIFIC_NAME) [ParentId]
, CASE -- trim off the @ symbol
WHEN p.PARAMETER_NAME is null THEN NULL
ELSE SUBSTRING(p.PARAMETER_NAME, 2, LEN(p.PARAMETER_NAME) )
END [Name]
, p.ORDINAL_POSITION [Ordinal]
, CASE
WHEN p.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
p.CHARACTER_MAXIMUM_LENGTH = -1 THEN
p.DATA_TYPE + '(max)'
ELSE
p.DATA_TYPE
END [TypeName]
, p.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(p.NUMERIC_PRECISION as integer) [Precision]
, CAST(p.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(p.NUMERIC_SCALE as integer) [Scale]
, p.COLLATION_CATALOG [CollationCatalog]
, p.COLLATION_SCHEMA [CollationSchema]
, p.COLLATION_NAME [CollationName]
, p.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, p.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, p.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, p.PARAMETER_MODE [Mode]
, CAST(NULL as nvarchar(max)) [Default]
FROM
INFORMATION_SCHEMA.PARAMETERS p
INNER JOIN INFORMATION_SCHEMA.ROUTINES r ON
p.SPECIFIC_SCHEMA = r.SPECIFIC_SCHEMA AND
p.SPECIFIC_NAME = r.SPECIFIC_NAME AND
r.ROUTINE_TYPE = 'PROCEDURE'
WHERE
p.IS_RESULT = 'NO'
) AS [Extent3]) AS [UnionAll1] ON (0 = [UnionAll1].[C1]) AND ([Extent1].[Id] = [UnionAll1].[ParentId])
UNION ALL
SELECT
[Extent4].[SchemaName] AS [SchemaName],
[Extent4].[Name] AS [Name],
CAST(NULL AS varchar(1)) AS [C1],
cast(0 as bit) AS [C2],
cast(0 as bit) AS [C3],
cast(0 as bit) AS [C4],
cast(0 as bit) AS [C5],
[UnionAll2].[Name] AS [C6],
[UnionAll2].[TypeName] AS [C7],
[UnionAll2].[Mode] AS [C8],
[UnionAll2].[Ordinal] AS [C9]
FROM (
SELECT
quotename(SPECIFIC_SCHEMA) + quotename(SPECIFIC_NAME) [Id]
, SPECIFIC_CATALOG [CatalogName]
, SPECIFIC_SCHEMA [SchemaName]
, SPECIFIC_NAME [Name]
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
NOT (ROUTINE_SCHEMA = 'dbo'
AND ROUTINE_NAME LIKE 'dt[_]%'
AND SUBSTRING(CAST(SERVERPROPERTY('productversion') as varchar(20)),1,1) = 8)
AND (DATA_TYPE != 'TABLE' OR DATA_TYPE is null)
AND ROUTINE_TYPE = 'PROCEDURE'
) AS [Extent4]
LEFT OUTER JOIN (SELECT
[Extent5].[Name] AS [Name],
[Extent5].[Ordinal] AS [Ordinal],
[Extent5].[TypeName] AS [TypeName],
[Extent5].[Mode] AS [Mode],
0 AS [C1],
[Extent5].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(f.SPECIFIC_SCHEMA) + quotename(f.SPECIFIC_NAME) + quotename(f.PARAMETER_NAME) [Id]
, quotename(f.SPECIFIC_SCHEMA) + quotename(f.SPECIFIC_NAME) [ParentId]
, CASE -- trim off the @ symbol
WHEN f.PARAMETER_NAME is null THEN NULL
ELSE SUBSTRING(f.PARAMETER_NAME, 2, LEN(f.PARAMETER_NAME) )
END [Name]
, f.ORDINAL_POSITION [Ordinal]
, CASE
WHEN f.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
f.CHARACTER_MAXIMUM_LENGTH = -1 THEN
f.DATA_TYPE + '(max)'
ELSE
f.DATA_TYPE
END [TypeName]
, f.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(f.NUMERIC_PRECISION as integer) [Precision]
, CAST(f.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(f.NUMERIC_SCALE as integer) [Scale]
, f.COLLATION_CATALOG [CollationCatalog]
, f.COLLATION_SCHEMA [CollationSchema]
, f.COLLATION_NAME [CollationName]
, f.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, f.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, f.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, f.PARAMETER_MODE [Mode]
, CAST(NULL as nvarchar(max)) [Default]
FROM
INFORMATION_SCHEMA.PARAMETERS f
INNER JOIN INFORMATION_SCHEMA.ROUTINES r ON
f.SPECIFIC_SCHEMA = r.SPECIFIC_SCHEMA AND
f.SPECIFIC_NAME = r.SPECIFIC_NAME AND
r.ROUTINE_TYPE = 'FUNCTION'
WHERE
f.IS_RESULT = 'NO'
) AS [Extent5]
UNION ALL
SELECT
[Extent6].[Name] AS [Name],
[Extent6].[Ordinal] AS [Ordinal],
[Extent6].[TypeName] AS [TypeName],
[Extent6].[Mode] AS [Mode],
6 AS [C1],
[Extent6].[ParentId] AS [ParentId]
FROM (
SELECT
quotename(p.SPECIFIC_SCHEMA) + quotename(p.SPECIFIC_NAME) + quotename(p.PARAMETER_NAME) [Id]
, quotename(p.SPECIFIC_SCHEMA) + quotename(p.SPECIFIC_NAME) [ParentId]
, CASE -- trim off the @ symbol
WHEN p.PARAMETER_NAME is null THEN NULL
ELSE SUBSTRING(p.PARAMETER_NAME, 2, LEN(p.PARAMETER_NAME) )
END [Name]
, p.ORDINAL_POSITION [Ordinal]
, CASE
WHEN p.DATA_TYPE in ('varchar', 'nvarchar', 'varbinary') and
p.CHARACTER_MAXIMUM_LENGTH = -1 THEN
p.DATA_TYPE + '(max)'
ELSE
p.DATA_TYPE
END [TypeName]
, p.CHARACTER_MAXIMUM_LENGTH [MaxLength]
, CAST(p.NUMERIC_PRECISION as integer) [Precision]
, CAST(p.DATETIME_PRECISION as integer) as [DateTimePrecision]
, CAST(p.NUMERIC_SCALE as integer) [Scale]
, p.COLLATION_CATALOG [CollationCatalog]
, p.COLLATION_SCHEMA [CollationSchema]
, p.COLLATION_NAME [CollationName]
, p.CHARACTER_SET_CATALOG [CharacterSetCatalog]
, p.CHARACTER_SET_SCHEMA [CharacterSetSchema]
, p.CHARACTER_SET_NAME [CharacterSetName]
, CAST(0 as bit) as [IsMultiSet]
, p.PARAMETER_MODE [Mode]
, CAST(NULL as nvarchar(max)) [Default]
FROM
INFORMATION_SCHEMA.PARAMETERS p
INNER JOIN INFORMATION_SCHEMA.ROUTINES r ON
p.SPECIFIC_SCHEMA = r.SPECIFIC_SCHEMA AND
p.SPECIFIC_NAME = r.SPECIFIC_NAME AND
r.ROUTINE_TYPE = 'PROCEDURE'
WHERE
p.IS_RESULT = 'NO'
) AS [Extent6]) AS [UnionAll2] ON (6 = [UnionAll2].[C1]) AND ([Extent4].[Id] = [UnionAll2].[ParentId])) AS [UnionAll3]
) AS [Project7]
ORDER BY [Project7].[C1] ASC, [Project7].[C2] ASC, [Project7].[C11] ASC
通过以上四个查询所得到的信息,EF Power Tool便可以地生成对应的class。具体的实现就比较复杂了,今天就不详细探究。(之后会为大家奉上)。
看到这里大家是否想到这样一个问题?如果我们访问数据库所使用的用户没有执行这些查询的权限会怎么样呢?这样生成的代码自然会很不完全。许多代码只生成了一部分,有些命名空间和程序集都没有被自动导入。这就产生了类似上一篇文章中,用户@Zero0420所碰到的问题:“为什么我老是生成的时候,DbModelBuilder 找不到命名空间?”
经过一番研究与试验后,我们使用的用户对于所要操作的数据库必须要符合以下权限中的一个:db_datareader, db_datawriter, db_ddladmin, db_owner以及db_securityadmin。有关这些权限更详细的说明,请参看:http://msdn.microsoft.com/en-us/library/ms189121(v=SQL.105).aspx。
2. EF Power Tool在生成Entity Data Model (.edmx和.XML)、Entity Data Model DDL以及生成Generated View的代码时为何会报ProviderIncompatibleException的错误?
接下来我们讨论另一个EF Power Tool的问题。在使用它为我们生成Entity Data Model (.edmx和.XML)、Entity Data Model DDL以及生成Generated View的代码时,
我的一个同事以及其他的一些用户碰到了这个错误信息:System.Data.ProviderIncompatibleException: The provider did not return a ProviderManifestToken string。从这个错误我们并不能得到太多有用的信息,所以我们就进一步查看了Visual Studio Output Window中更详细的错误信息:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)。熟悉SQL Server的用户肯定对这个错误信息不陌生。似乎我们的用户对相应的SQL Server数据库访问不了或权限受限? 这怎么可能呢?之前Code First POCO class都生成得很好啊。我们此时又用SQL Server Management Studio和Visual Studio连接了相应的数据库,发现运行都很正常。太奇怪了。。。
public class EFToolContext : DbContext
{
Catalog=EFTool;Integrated Security=True;Pooling=False")
{
}
static EFToolContext()
{
Database.SetInitializer<EFToolContext>(null);
}
public DbSet<Child> Children { get; set; }
public DbSet<Parent> Parents { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new ChildMap());
modelBuilder.Configurations.Add(new ParentMap());
}
}
相同的情况在我自己的机器上就能运行正常。难道是同事项目里的App.config中的connection string有问题?我们又重新检查了一下,发现connection string没有问题。另一位同事@Alan_chen此时对相应的DbContext class做了很小的修改,将connection string直接写入了class中:
此时,EF Power Tool能运行正常。果然是哪里传递connection string出了问题。之后我发现同事的机器上并不存在.SQLExpress实例,这个发现顿时让我茅塞顿开。难道EF Power Tool会去尝试访问.SQLExpress吗?用SQL Server Profiler观察我机器上的.SQLExpress实例后,发现果然如此。EF Power Tool会尝试去连接.SQLExpress但是并不会做任何数据查询,而真正的Named Instance则无需访问,因为所有数据库端得信息已经可以从DbContext,POCO以及mapping class中得到。
为什么要去访问.SQLExpress实例?在我看来,这是EF Power Tool代码中的一个缺陷。为此,我也在询问有关的产品组。不过EF Power Tool也才CTP1,有些瑕疵也在所难免吧。
如需转发,请注明出处,谢谢! http://www.cnblogs.com/LingzhiSun/archive/2011/06/13/EFPowerTool_2.html