简体   繁体   中英

Can't add table to SQL Server database in Visual Studio 2015

I am learning C# from a book and trying to add 2 tables to a database. I have created the database and I click on the table and select add table. I fill in all the columns and then save the table with a new name. Nothing happened, so I pressed the update button and then the table was added to the database explorer on the left.

The tab at the top says dbo.Table_1.sql

When I create the next table and click the update button I get the following error:

SQL71508 :: The model already has an element that has the same name dbo.Table.

This is happening because you have two tables with the same name: "Table". The name of the script file and the name of the table are not the same. There are a couple ways to see/edit the name of the table. First open the script file that you want to change.

Normally you have the designer on top and the T-SQL tab on the bottom. The T-SQL tab will contain something like this:

CREATE TABLE [dbo].[Table]
(
    [Id] INT NOT NULL PRIMARY KEY, 
    ... other columns ... 
)

You can directly edit the SQL in that pane if you want.

Alternatively, you can click in the designer pane and then press F4. This shows the Properties pane for the table. The very first item in the properties pane is the "(Name)" which specifies the name of the table.

Change your second table to have a different name from your first table and you should be able to save it without getting the error.

我也遇到了这个问题,它是通过NEW QUERY而不是NEW TABLE运行CREATE TABLE语句来解决的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM