我的模型中有一个名为StatusChange的额外字段,这是使用sproc填充来从表中获取所有字段以及额外字段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,因为不需要它,所以我尝试将属性Computed添加到C#中的属性
这是C#代码
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public StatusChange StatusChange { get; set; }
但是当我在DbContext上调用SaveChanges()时,我收到错误
Invalid column name 'StatusChange'.
无论如何,我只能将这个属性用于SELECT并在INSERT上忽略它吗?
答案 0 :(得分: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
}
}