实体框架4.0,添加不包含主键的SQL Server视图

时间:2011-02-16 15:28:13

标签: c# entity-framework entity-framework-4 linq-to-entities ado.net-entity-data-model

我正在使用Entity Framework 4.0,C#4.0,.Net 2010和SQL Server 2008 R2。我在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 Number

我不知道如何将它们添加为Entity Framework的一部分。即使我已经尝试添加row_number()(第一列)作为附加列,因为它认为它将作为一种主键但没有用处。实体框架设计器未在.edmx模型文件中添加此视图。

我通过完全删除.edmx文件并在新项目中仅针对Entity Framework尝试了这一点,但没有运气。有人可以帮我解决这个问题。

1 个答案:

答案 0 :(得分:3)

我从Entity Framework and Sql Server view question找到了一个完美的答案。根据给出该问题的答案,上面的SQL Query(没有主键的视图)必须改变如下。

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

第三行isnull(member.Membership_Number,-1) Number将该列作为主键,我们可以删除row_number() over (order by member.Membership_Number) ID或我们不希望作为主键的一部分。

这适用于我的情况。