4

我正在使用實體框架4.0,C#4.0,.Net 2010和SQL Server 2008 R2。我在我的SQL Server數據庫中創建了以下視圖:實體框架4.0,添加SQL Server視圖,其中不包含主鍵

create view viewGetMember 
as 
    select distinct 
     row_number() over (order by member.Membership_Number) ID, 
     email.Communication_Point_Id id1, 
     member.Membership_Number Number, 
     dvt.Default_Name ParticipationStatus, 
     person.Given_Name GivenName, 
     person.Last_Name LastName, 
     adrs.House_Number HouseNumber, 
     adrs.Street Street, 
     adrs.Post_Code PostCode, 
     email.Email_Address EmailAddress 
    from 
     Participation member 
    inner join 
     Party_Participation pp on member.Participation_Id = pp.Participation_Id 
    inner join 
     Party party on pp.Party_Id = party.Party_Id 
    inner join 
     Individual person on party.Party_Id = person.Party_Id 
    inner join 
     Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id 
    inner join 
     Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id 
    inner join 
     Communication_Point cpemail on party.Party_Id = cpemail.Party_Id 
    inner join 
     [Address] adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id 
    inner join 
     Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id 
    where 
     member.Membership_Number is not null 
go 

select * from viewGetMember 

想在實體框架中添加此視圖。但它不包含主要內容。雖然以下兩個字段可以組成一個複合主鍵(第二和第三列)。

  • email.Communication_Point_Id ID1
  • member.Membership_Number數

我不知道如何將其添加爲實體框架的一部分。即使我已經嘗試將row_number()(第一列)作爲附加列,認爲它將作爲一種主鍵,但沒有用處。實體框架設計器不在.edmx模型文件中添加此視圖。

我已經嘗試完全刪除.edmx文件,並在新項目只爲實體框架,但沒有運氣。有人可以爲我提供解決這個問題的辦法。

+0

當你使用ROW_NUMBER()時,不同的目的是什麼?無論如何,所有的行都是不同的。 – 2011-02-16 15:31:29

+0

@Damien_The_Unbeliever,我甚至在添加row_number()之前添加了不同。我認爲row_number()可以解決實體框架的主鍵要求。但正如我所說,沒有使用它。 – Elangesh 2011-02-16 15:35:58

回答

3

我從Entity Framework and Sql Server view question找到了一個完美的答案。根據對這個問題的回答,上面的SQL查詢(沒有主鍵的查看)必須如下改變。

create view viewGetMember as 
select distinct 
isnull(member.Membership_Number,-1) Number, 
dvt.Default_Name ParticipationStatus, 
person.Given_Name GivenName, 
person.Last_Name LastName, 
adrs.House_Number HouseNumber, 
adrs.Street Street, 
adrs.Post_Code PostCode, 
email.Email_Address EmailAddress 
from Participation member 
inner join Party_Participation pp on member.Participation_Id = pp.Participation_Id 
inner join Party party on pp.Party_Id = party.Party_Id 
inner join Individual person on party.Party_Id = person.Party_Id 
inner join Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id 
inner join Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id 
and cpadrs.Communication_Point_Type in 
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'ADDRESS') 
inner join Communication_Point cpemail on party.Party_Id = cpemail.Party_Id 
and cpemail.Communication_Point_Type in 
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'EMAIL') 
inner join Address adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id 
inner join Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id and cpemail.Is_Preferred = 1 
where 
member.Membership_Number is not null 
go 
select * from viewGetMember 
go 

的THRID線isnull(member.Membership_Number,-1) Number使得該列作爲主鍵,我們可以擺脫row_number() over (order by member.Membership_Number) ID或任何我們不希望作爲主鍵的一部分。

這對我而言效果很好。