Table association is lost during DBML designer.cs Update
I am working on a .NET 4 project. Recently a column was added to a table "T" and after that all of the associations of other tables with table T are lost (any table that had a foreign key from table T now doesn't have it). I should mention that we use sqlmetal command to update class file related to DBML. And this problem happened suddenly although DB has got updated frequently and there has been no such problem before.
I don't have any clue if the problem is related to SQL Server or LINQ. Can anyone help me please?
Table T:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[T](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Cid] [int] NULL,
[Guid] [uniqueidentifier] NOT NULL
CONSTRAINT [PK_T] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[T] WITH CHECK ADD CONSTRAINT [FK_T_C] FOREIGN KEY([Cid])
REFERENCES [dbo].[C] ([Id])
GO
ALTER TABLE [dbo].[T] CHECK CONSTRAINT [FK_T_C]
GO
ALTER TABLE [dbo].[T] ADD CONSTRAINT [DF_T_Guid] DEFAULT (newid()) FOR [Guid]
GO
Table I which has foreign key from table T:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[I](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](512) NOT NULL,
[TId] [int] NOT NULL,
[Desc] [nvarchar](2000) NULL,
[Guid] [uniqueidentifier] NOT NULL
CONSTRAINT [PK_I] 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].[I] WITH CHECK ADD CONSTRAINT [FK_I_T] FOREIGN KE开发者_StackOverflow中文版Y([TId])
REFERENCES [dbo].[T] ([Id])
GO
ALTER TABLE [dbo].[I] CHECK CONSTRAINT [FK_I_T]
GO
ALTER TABLE [dbo].[I] ADD CONSTRAINT [DF_I_Guid] DEFAULT (newid()) FOR [Guid]
GO
[Not really an answer but too long to write in comments and perhaps it will help]
Since the constraints are present in the DB, i would say the problem is not SQL server.
Perhaps there is something wrong in the internals of your project.
Check you DMBL - is that right or wrong?
If wrong, What I would do first is create a new dummy project in Visual Studio (Express?) and create a new Linq-2-sql DBML file, add all the tables and see if the problem still exists.
精彩评论