2016-03-07 79 views
1

我已經在我的模型稱爲StatusChange一個額外的領域,這是使用存儲過程來從表中還有額外的字段StatusChange所有字段填入實體框架:在插入忽略一個字段只

SELECT New.*, [dbo].[ftn_GetStatusChange](Old.MemberStatusId, New.MemberStatusId) AS StatusChange 
FROM Member New 
LEFT OUTER JOIN Member Old ON New.MemberNo = Old.MemberNo AND Old.ImportHistoryID = @ImportIdToCompare 
WHERE New.ImportHistoryID = @NewestImportId 

下面是函數

CREATE FUNCTION [dbo].[ftn_GetStatusChange] 
(
    @OldStatus char, 
    @NewStatus char 
) 
RETURNS int 
AS 
BEGIN 
    IF (@OldStatus IS NULL) 
    BEGIN 
     RETURN 1 
    END 

    IF (@OldStatus = 'R') 
     BEGIN 
      IF @NewStatus = 'C' RETURN 5 
      IF @NewStatus = 'S' RETURN 4 
     END 

    IF (@OldStatus = 'C') 
     BEGIN 
      IF @NewStatus = 'R' RETURN 3 
      IF @NewStatus = 'S' RETURN 4 
     END 

    IF (@OldStatus = 'S') 
     BEGIN 
      IF @NewStatus = 'C' RETURN 5 
      IF @NewStatus = 'R' RETURN 3 
     END 

    RETURN 0 
END 

這是枚舉的數字地圖

public enum StatusChange 
    { 
     NoChange = 0, 
     New = 1, 
     Current = 2, 
     Resigned = 3, 
     Suspended = 4, 
     Reinstated = 5, 
    } 

該數據庫表沒有現場StatusChange雖然因爲沒有必要,所以我嘗試添加計算的財產在C#中的屬性

這是C#代碼

[DatabaseGenerated(DatabaseGeneratedOption.Computed)] 
public StatusChange StatusChange { get; set; } 

但是我當呼籲的DbContext的SaveChanges()我得到的錯誤

Invalid column name 'StatusChange'. 

反正我只能擁有這件屬性SELECT和忽略它的INSERT?

+1

你可以添加'[NotMapped]'屬性的屬性?或者這也會導致該屬性在加載時不被設置? – Patrick

+1

我試過這個,但是然後執行SELECT的SPROC忽略了StatusChange屬性。 – Coesy

+1

由它的外觀,你將需要改變你的設計,使這項工作。你不能使用[NotMapped]並獲取你需要的信息,然後分別進行StatusChange。就像在選擇Old.MemberStatusId,New.MemberStatusId中包含字段並在運行時轉換爲狀態一樣, – Seabizkit

回答

0

沒有測試不是100%你想作爲其挺難想像你的數據,而無需實際...但什麼也許這有助於...

,請帶一它的一小撮鹽其相當難以可視化你的數據和要求;-)

public class Member 
{ 
    [Key][AutoIncromentCode] 
    public int Id {get; set;} 
    public string MemberNo {get; set;} 
    public string FirstName { get; set;} 
    public int MemberStatusId { get; set;} 

    public int ImportHistortId { get; set;} 

    [NotMapped] 
    public MemberStatus 
    { 
     return (Status)MemberStatusId 
    } 
} 

public enum Status 
{ 
    NoChange = 0, 
    New = 1, 
    Current = 2, 
    Resigned = 3, 
    Suspended = 4, 
    Reinstated = 5, 
} 

public class MemberStatusDto 
{ 
    public Member NewMember {get; set;} 
    public Member OldMember {get; set;} 
} 

public void GetSomeStuff(int importHistortId) 
{ 
    List<MemberStatusDto> result = (from N_Memeber in Content.Member 
             join O_Member in Content.Member on 
             new { N_Memeber.MemberNo, O_Member.ImportHistortId } equals new {MemberNo = O_Member.MemberNo, ImportHistortId = importHistortId } 
             into T_Members 
             from A_Members in T_Members.DefaultIfEmpty() 
             where N_Memeber.ImportHistoryID = importHistortId 
            select new MemberStatusDto() 
            { 
             NewMember = A_Members.N_Memeber, 
             OldMember = A_Members.O_Member 
            }).Tolist();    

    foreach(var item in result) 
    { 
     var NewState = item.NewMember.MemberStatus; 
     var oldState = (item.OldMember == null) ? "nothing" : item.OldMember.MemberStatus 

    } 
} 
+0

個人來說,如果您將所有歷史項目移動到另一個表中,會更好!然後你可以使用EF自動鏈接這個,並按日期安排你回拉一個成員...這將是一個更聰明的設計。 – Seabizkit