c# - 无法用两个表创建外键

标签 c# sql-server database

我正在为 Linq 构建网站,我需要的方法是使用外键精确设置与我的用户表相同。¨

我已经向我保证,我的 Tabler 有一个主键,因为它必须是使用 grab 的唯一内容。

这是一张布鲁日 table

CREATE TABLE [dbo].[brugere] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [username]    NVARCHAR (255) NOT NULL,
    [password]   NVARCHAR (255) NOT NULL,
    CONSTRAINT [PK_brugere] PRIMARY KEY ([Id]), 
    CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([brugerid]), 
    CONSTRAINT [FK_brugere_ToKunde] FOREIGN KEY ([Id]) REFERENCES [KundeData]([brugerid])
);

重点在这里

CREATE TABLE [dbo].[pointantal] (
    [Id]       INT            IDENTITY (1, 1) NOT NULL,
    [point]    INT            NOT NULL,
    [omrade]   NVARCHAR (255) NOT NULL,
    [datotid]  DATETIME       DEFAULT (getdate()) NOT NULL,
    [brugerid] INT            NOT NULL, 
    CONSTRAINT [PK_pointantal] PRIMARY KEY ([Id])
);

这里是KundeData表

CREATE TABLE [dbo].[KundeData] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [Adresse]       NVARCHAR (255) NOT NULL,
    [Postnr]        INT            NOT NULL,
    [Mobil]         INT            NOT NULL,
    [Byen]          NVARCHAR (255) NOT NULL,
    [abonnementsId] INT            NOT NULL,
    [BuyDate]       DATETIME       DEFAULT (getdate()) NOT NULL,
    [prisid]        INT            NOT NULL,
    [HaevedeId]     NVARCHAR (255) NULL,
    [brugerid]      INT            NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

我尝试更新内容时收到的错误消息在这里

Update cannot proceed due to validation errors.
Please correct the following errors and try again.

SQL71516 :: The referenced table '[dbo].[pointantal]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted. SQL71516 :: The referenced table '[dbo].[KundeData]' contains no primary or candidate keys that match the referencing column list in the foreign key. If the referenced column is a computed column, it should be persisted.

最佳答案

外键只能引用主键或唯一列。您可以向您引用的列添加唯一约束:

CREATE TABLE [dbo].[pointantal] (
    ...
    CONSTRAINT AK_BrugerID UNIQUE(brugerid) 

或者您可以更改约束以实际引用表中的主键:

CONSTRAINT [FK_brugere_ToPoint] FOREIGN KEY ([Id]) REFERENCES [pointantal]([Id])

但是,您似乎真的想要 brugerid pointantal 的专栏和 KundeData访问 Id 的表(这是一个独特的列)在 brugere table 。在这种情况下,您将外键放在这些表上并让它访问 bruger 的主键。 table 。以下代码在我的系统上成功运行:

CREATE TABLE [dbo].[brugere] (
    [Id]            INT            IDENTITY (1, 1) NOT NULL,
    [username]    NVARCHAR (255) NOT NULL,
    [password]   NVARCHAR (255) NOT NULL,
    CONSTRAINT [PK_brugere] PRIMARY KEY ([Id])
);

CREATE TABLE [dbo].[pointantal] (
    [Id]       INT            IDENTITY (1, 1) NOT NULL,
    [point]    INT            NOT NULL,
    [omrade]   NVARCHAR (255) NOT NULL,
    [datotid]  DATETIME       DEFAULT (getdate()) NOT NULL,
    [brugerid] INT            NOT NULL,
    CONSTRAINT [PK_pointantal] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_point_ToBrugere] FOREIGN KEY ([brugerid]) REFERENCES [brugere]([Id])
);

关于c# - 无法用两个表创建外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29582552/

相关文章:

c# - csv文件转xls文件时兼容性检查怎么解决?

sql - 获取几个日期之间的日期列表

sql-server - 每天3000万条记录,SQL Server跟不上,需要其他类型的数据库系统吗?

mysql - 如果最后一条记录包含相同的值则更新,否则插入

c# - 在 C# 中计算中位数

c# - 带有内部连接的 SQL Server NULL 值

sql - 需要在 SQL Server 2012 中自动递增字符串

java - JPA 强制转换异常

sql - 为每一行插入一系列值的最简洁方法?

c# - Silverlight 图片加载问题