此問題被問到on MSDN,但從未真正回答過。如何在AdventureWorks2016中使用BusinessIdentityId插入新記錄
AdventureWorks2016使用「BusinessEntityId」作爲多個表的主鍵。它不是一個標識列,但它在使用它的所有表中都是唯一的,例如Sales.Store。
所以問題是,在保存新記錄時ID從哪裏來?必須調用存儲過程才能獲取下一個有效的ID似乎很笨拙。例如,如果我正在使用實體框架,但我沒有看到它是如何工作的,但它本來是專門用來展示EF中工作的繼承。
我發現這種類型的結構很有用,但沒有看到如何使它與EF Core一起工作。
CREATE TABLE [Sales].[Store](
[BusinessEntityID] [int] NOT NULL,
[Name] [dbo].[Name] NOT NULL,
[SalesPersonID] [int] NULL,
[Demographics] [xml](CONTENT [Sales].[StoreSurveySchemaCollection]) NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_Store_BusinessEntityID] PRIMARY KEY CLUSTERED
CREATE TABLE [Person].[Person](
[BusinessEntityID] [int] NOT NULL,
[PersonType] [nchar](2) NOT NULL,
[NameStyle] [dbo].[NameStyle] NOT NULL,
[Title] [nvarchar](8) NULL,
[FirstName] [dbo].[Name] NOT NULL,
[MiddleName] [dbo].[Name] NULL,
[LastName] [dbo].[Name] NOT NULL,
[Suffix] [nvarchar](10) NULL,
[EmailPromotion] [int] NOT NULL,
[AdditionalContactInfo] [xml](CONTENT [Person].[AdditionalContactInfoSchemaCollection]) NULL,
[Demographics] [xml](CONTENT [Person].[IndividualSurveySchemaCollection]) NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_Person_BusinessEntityID] PRIMARY KEY CLUSTERED
(
[BusinessEntityID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
請提供一個tabla作爲示例。 –
@ H.Herzl表結構添加 –
Sales.Store表包含一個名爲FK_Store_BusinessEntity_BusinessEntityID的外鍵,表Person.BusinessEntity包含一個標識列,所以我猜你需要在插入子表之前保存該表,這是有道理的? –