实体框架:数据阅读器不兼容

时间:2015-07-08 18:43:45

标签: c# entity-framework

  

数据阅读器与指定的内容不兼容   'FurnitureStoreDataModel.Companies_GetFromUsernameWithValidationToken_Result'。   类型为“Id”的成员中没有相应的列   具有相同名称的数据阅读器。

仅当数据库中没有合适的条目时才会出现这种情况。我是否应该通过围绕呼叫的尝试捕获来处理这个问题,或者有什么方法可以防止这种情况发生?

编辑:

public Companies_GetFromUsernameWithValidationToken_Result GetFromUsernameWithValidationToken(string username)
{
            if (!string.IsNullOrWhiteSpace(username))
            {
                return context.Companies_GetFromUsernameWithValidationToken(username).FirstOrDefault();
            }
            return null;
}  

EF生成的代码:

 public virtual ObjectResult<Companies_GetFromUsernameWithValidationToken_Result> Companies_GetFromUsernameWithValidationToken(string username)
{
            var usernameParameter = username != null ?
                new ObjectParameter("Username", username) :
                new ObjectParameter("Username", typeof(string));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Companies_GetFromUsernameWithValidationToken_Result>("Companies_GetFromUsernameWithValidationToken", usernameParameter);
}   

存储过程:

ALTER PROCEDURE [dbo].[Companies_GetFromUsernameWithValidationToken]    
  @Username nvarchar(50)         
AS          
BEGIN    
IF(@Username IS NOT NULL)    
BEGIN    
 DECLARE @companyId uniqueidentifier = (SELECT TOP 1 Id FROM Companies WHERE Username = @Username)  
 IF(@companyId IS NOT NULL)  
 BEGIN  
  DECLARE @validationToken uniqueidentifier = NEWID();  
  INSERT INTO ValidationTokens(Id, CompanyId) VALUES(@validationToken, @companyId)  
   SELECT Id, Name, Username, @validationToken as [ValidationToken] FROM Companies WHERE Id = @companyId   
 END  
END   
END 

0 个答案:

没有答案