我可以在我的表中使用“Table_Id”或“TableId”的 FK 名称。但是实体框架 SQL 生成器同时使用了这两者。
- 映射关联(独立关联,FK 属性可见):无下划线
- 约束关联(外键关联):下划线
这看起来有点荒谬——从数据库的角度来看,这两种关系之间没有区别,所以这里的一致性似乎很明显。我正在构建模型。
我做错了什么还是有什么办法可以解决这个问题?
编辑:应要求,包括样品。我创建了一个新的实体图并生成了 SQL。它验证。这是图表和从模型脚本生成数据库的快照。请注意 Apples 上的 BasketId 与 Oranges 上的 Basket_Id。唯一的区别是选择了“将外键属性添加到'Oranges'实体”。添加该关联时。
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 04/29/2013 23:38:07
-- Generated from EDMX file: C:\data\web-trunk\TestEntities.Data\TestEntities.edmx
-- --------------------------------------------------
SET QUOTED_IDENTIFIER OFF;
GO
USE [TestEntities];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO
-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------
-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------
-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------
-- Creating table 'Baskets'
CREATE TABLE [dbo].[Baskets] (
[Id] int IDENTITY(1,1) NOT NULL
);
GO
-- Creating table 'Apples'
CREATE TABLE [dbo].[Apples] (
[Id] int IDENTITY(1,1) NOT NULL,
[BasketId] int NOT NULL
);
GO
-- Creating table 'Oranges'
CREATE TABLE [dbo].[Oranges] (
[Id] int IDENTITY(1,1) NOT NULL,
[Basket_Id] int NOT NULL
);
GO
-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------
-- Creating primary key on [Id] in table 'Baskets'
ALTER TABLE [dbo].[Baskets]
ADD CONSTRAINT [PK_Baskets]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- Creating primary key on [Id] in table 'Apples'
ALTER TABLE [dbo].[Apples]
ADD CONSTRAINT [PK_Apples]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- Creating primary key on [Id] in table 'Oranges'
ALTER TABLE [dbo].[Oranges]
ADD CONSTRAINT [PK_Oranges]
PRIMARY KEY CLUSTERED ([Id] ASC);
GO
-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------
-- Creating foreign key on [Basket_Id] in table 'Oranges'
ALTER TABLE [dbo].[Oranges]
ADD CONSTRAINT [FK_BasketOrange]
FOREIGN KEY ([Basket_Id])
REFERENCES [dbo].[Baskets]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_BasketOrange'
CREATE INDEX [IX_FK_BasketOrange]
ON [dbo].[Oranges]
([Basket_Id]);
GO
-- Creating foreign key on [BasketId] in table 'Apples'
ALTER TABLE [dbo].[Apples]
ADD CONSTRAINT [FK_BasketApple]
FOREIGN KEY ([BasketId])
REFERENCES [dbo].[Baskets]
([Id])
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- Creating non-clustered index for FOREIGN KEY 'FK_BasketApple'
CREATE INDEX [IX_FK_BasketApple]
ON [dbo].[Apples]
([BasketId]);
GO
-- --------------------------------------------------
-- Script has ended
-- ------------------------