SQL71501 ::外键:[dbo]。[FK_AddressBook_Country]有对列[dbo]。[AddressBook]的未解析引用。[CountryID]

时间:2019-04-17 23:03:33

标签: sql asp.net-mvc database web localdb

希望有人可以帮助我。我有一个错误 “ SQL71501 :: ::外键:[dbo]。[FK_AddressBook_Country]对列[dbo]。[AddressBook]。[CountryID]有未解决的引用。 红线标记为[[CountryID)]。我找不到它的错。实际上,我关注了该站点http://demo.dotnetawesome.com/mvc/mycontactbook/part2,只是更改了表格。希望有人可以帮助我。非常感谢!

CREATE TABLE [dbo].[AddressBook] (
    [Id]           INT           NOT NULL,
    [Name]         VARCHAR (50)  NOT NULL,
    [Surname]      VARCHAR (50)  NOT NULL,
    [Address1]     VARCHAR (200) NOT NULL,
    [Address2]     VARCHAR (100) NULL,
    [Postcode]     VARCHAR (6)   NOT NULL,
    [Town]         VARCHAR (20)  NOT NULL,
    [Country]      VARCHAR (50)  NOT NULL,
    [Email]        VARCHAR (50)  NOT NULL,
    [MobileNumber] VARCHAR (20)  NOT NULL,
    [PictureUser]  VARCHAR (200) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC), 
    CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([CountryID]) REFERENCES [Country]([CountryID])
);
CREATE TABLE [dbo].[Country] (
    [CountryID]   INT IDENTITY (1, 1) NOT NULL,
    [CountryName] VARCHAR (100) NOT NULL,
    PRIMARY KEY CLUSTERED ([CountryID] ASC)
);

1 个答案:

答案 0 :(得分:0)

这应该很好用,但是无论如何首先在“ FOREIGN KEY([CountryID])”中出现两个错误,您在AdresseBook中都没有名为“ CountryId”的字段,我认为您希望将“ Country”字段作为外键adressBook中的“国家/地区”字段必须与表“国家/地区”的CountryID具有相同的类型

 CREATE TABLE [dbo].[AddressBook] (
        [Id]           INT           NOT NULL,
        [Name]         VARCHAR (50)  NOT NULL,
        [Surname]      VARCHAR (50)  NOT NULL,
        [Address1]     VARCHAR (200) NOT NULL,
        [Address2]     VARCHAR (100) NULL,
        [Postcode]     VARCHAR (6)   NOT NULL,
        [Town]         VARCHAR (20)  NOT NULL,
        [Country]      INT  NOT NULL,
        [Email]        VARCHAR (50)  NOT NULL,
        [MobileNumber] VARCHAR (20)  NOT NULL,
        [PictureUser]  VARCHAR (200) NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC), 
        CONSTRAINT [FK_AddressBook_Country] FOREIGN KEY ([Country]) REFERENCES [Country]([CountryID])
    );
    CREATE TABLE [dbo].[Country] (
        [CountryID]   INT IDENTITY (1, 1) NOT NULL,
        [CountryName] VARCHAR (100) NOT NULL,
        PRIMARY KEY CLUSTERED ([CountryID] ASC)
    );