0
一個關係我有一個父表作爲CustomerRequest具有與貸款表的關係爲1至1-0,這意味着客戶請求可具有1或0貸款在貸款表。我在首先在Entity Framework代碼中獲得正確關係時遇到了問題。一到在實體框架代碼優先
這是我的CustomerRequest表腳本以正確的關係:
CREATE TABLE [Loan].[CustomerRequest]
(
[Id] INT IDENTITY(1,1)
,[CorrelationId] VARCHAR(500)
,[CustomerNumber] BIGINT
....
CONSTRAINT PK_CustomerRequest PRIMARY KEY([Id])
)
這是貸款表:
CREATE TABLE [Loan].[Loan]
(
[Id] INT IDENTITY(1,1)
,[CustomerRequestId] INT
.....
CONSTRAINT PK_Loan PRIMARY KEY([Id])
,CONSTRAINT FK_Loan_CustomerRequestId FOREIGN KEY([CustomerRequestId]) REFERENCES [Loan].[CustomerRequest]([Id])
)
這是我的客戶要求型號:
public class CustomerRequest
{
public int Id { get; set; }
public string CorrelationId { get; set; }
.....
public virtual Loan Loan { get; set; }
}
和貸款模式:
public class Loan
{
public int Id { get; set; }
public int CustomerRequestId { get; set; }
....
public virtual CustomerRequest CustomerRequest { get; set; }
}
我在CustomerRequestMap這種關係:
HasKey(t => t.Id).HasOptional(t => t.Loan).WithRequired(t => t.CustomerRequest);
我得到這個錯誤,當我嘗試插入到貸款表:
{"Cannot insert explicit value for identity column in table 'Loan' when IDENTITY_INSERT is set to OFF."}
好像你的數據庫沒有處於正確的狀態。 檢查這個SO問題http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity –
@AlexanderTaran但我能夠插入我的SQL表在SQL中,但在實體框架內我得到這個錯誤。 – Alma
然後EF不知道你在那裏有身份。 –