Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您自己将使用SqlQuery返回所需的数据。与其他一些选项相比,优势在于: 模型更新时不必更改任何内容 如果直接在生成的类中执行此操作,则不会被覆盖(上面有人提到这一点,好像它是一个选项:) 不必在程序本身中添加任何现在或以后可能产生副作用的内容_C#_Sql_Entity Framework - Fatal编程技术网

C# 您自己将使用SqlQuery返回所需的数据。与其他一些选项相比,优势在于: 模型更新时不必更改任何内容 如果直接在生成的类中执行此操作,则不会被覆盖(上面有人提到这一点,好像它是一个选项:) 不必在程序本身中添加任何现在或以后可能产生副作用的内容

C# 您自己将使用SqlQuery返回所需的数据。与其他一些选项相比,优势在于: 模型更新时不必更改任何内容 如果直接在生成的类中执行此操作,则不会被覆盖(上面有人提到这一点,好像它是一个选项:) 不必在程序本身中添加任何现在或以后可能产生副作用的内容,c#,sql,entity-framework,C#,Sql,Entity Framework,示例代码: public partial class myEntities { public List<MyClass> usp_GetTestRecords(int _p1, int _p2, string _groupId) { // fill out params SqlParameter p1 = new SqlParameter("@p1", _p1); ...

示例代码:

 public partial class myEntities
    {
       public List<MyClass> usp_GetTestRecords(int _p1, int _p2, string _groupId)
       {
          // fill out params
          SqlParameter p1 = new SqlParameter("@p1", _p1);
          ...
          obj[] parameters = new object[] { p1, p2, groupId };

          // call the proc
          return this.Database.SqlQuery<MyClass>(@"EXECUTE usp_GetTestRecords @p1, @p2, @groupId", parameters).ToList();
       }
    }
公共部分类myEntities
{
公共列表usp\U GetTestRecords(int\u p1、int\u p2、string\u groupId)
{
//填写参数
SqlParameter p1=新的SqlParameter(“@p1”,_p1);
...
obj[]参数=新对象[]{p1,p2,groupId};
//调用进程
返回此.Database.SqlQuery(@“EXECUTE usp_getestrecords@p1、@p2、@groupId”、parameters);
}
}
导入过程中的

迅速启动 可用于获取sp架构

如果更改sp并要更新新的sp,则应从edmx文件(从xml)中删除旧定义的函数,因为尽管从模型浏览器中删除sp,但它不会在edmx中删除。比如,

    <FunctionImport Name="GetInvoiceByNumber"     ReturnType="Collection(Model.Invoice_Result)">
       <Parameter Name="InvoiceNumber" Mode="In" Type="Int32" />
    </FunctionImport>


我也遇到了同样的问题,当我完全删除相应sp的FunctionImport标记时,模型正确更新。您可以通过从visual studio中搜索函数名来找到标记。

您可能会打开模型浏览器,然后转到函数导入,双击相关存储过程,然后手动单击“获取列信息”,然后单击“创建新的复杂类型”。这通常能解决问题。

我也遇到过这个问题,但经过数小时的在线搜索,上述方法都无济于事。 最后,我了解到,如果存储过程将某些参数设置为null,并且在查询执行过程中产生任何错误,那么就会发生这种情况。 实体框架将通过定义复杂的实体模型来生成存储过程的方法。由于该空值,存储过程将返回和int值


请检查存储过程,或者检查它是否提供空结果集和空值。它会解决你的问题。希望如此。

我找到的最好的解决办法是稍微作弊

在存储过程中,对所有内容进行注释,在第一行添加
选择[foo]='',[bar]=''
等。。。 现在更新模型,转到映射函数,选择复杂类型并单击
Get Column Information
,然后单击
Create complex type


现在对假存储过程主体进行注释,选择并取消注释真实存储过程主体。

我认为这是数据库权限的问题,我不知道具体可能是什么,但是,在我的工作中,我们使用Active Directory用户授予应用程序连接到数据库的权限,此帐户是专门为应用程序创建的,每个应用程序都有自己的用户帐户,作为开发人员,我有读、写和其他基本功能的权限,没有更改,也没有高级功能,我在使用普通帐户运行Visual Studio时遇到同样的问题,然后,我打开Visual Studio,选择上下文菜单上的“作为不同的用户”选项,我把这个应用程序的广告登入权放进去,瞧!,现在,我的存储过程正在加载我预期的所有字段,在此之前,我的存储过程返回为int。我希望这对某些人有所帮助,如果SQL身份验证已到位,数据库帐户上的视图定义权限可能会起到作用,验证用于将实体框架连接到数据库的用户凭据是否具有从CUSTOMERS表读取的正确权限


当Entity Framework使用SQL身份验证映射复杂对象(即选择多个列的存储过程)时,如果该存储过程中的任何表未设置读取权限,映射将导致返回INT而不是所需的结果集。

您有调用此存储过程的c代码示例吗?是的,发布
usp\u getestrecords()的代码。
为什么将查询存储在变量中然后执行?我错过了什么?为什么不直接执行它呢?可能应该使用。我从数据库生成了模型。它会自动将int作为返回类型,即使他选择了一个复杂的返回类型,EF也不会选择它,因为EF不识别动态SQL。这真是一个好办法。您甚至可以临时执行此操作,但请注意,如果要重新生成EF模型,您可能会陷入困境。EF不接受动态SQL,因此您需要一个临时表或表变量,以便从中读取以生成复杂类型。请您更清楚地解释这一部分:“只需插入表变量,然后从该表变量中进行选择。EF应该接受它。”?我不知道该怎么办。这不起作用,甚至给下面的实际解决方案带来了问题。在模型上,您可以双击函数并设置返回类型。如果您使用的临时表不起作用。EF是一个令人头痛的问题。这里有一个逐步解决这个问题的方法,它对我有效。这个方法解决了我的问题。谢谢你,真是一个傻瓜。每次更新模型时,我都必须这样做。愿上帝帮助任何一个首先使用代码的人。这每天都要花我几个小时。看起来FMTONLY已经被弃用了,所以我们可能不得不坐下来看看他们是如何更改实体框架的。这里要注意的是,如果您更改了模式,您的过程将返回,您需要更新开始部分以使EF获取这些更改。这是因为EF在搜索第一个返回的数据集架构以构建其模型时忽略了流控制逻辑。添加了一个选项以解决架构更新。注意:“go”将导致表变量出现问题,并且表变量将在常规临时表无法工作的情况下工作(至少对于我的
public virtual int usp_GetTestRecords(string groupId)
{
    var groupIdParameter = groupId != null ?
        new ObjectParameter("groupId", groupId) :
        new ObjectParameter("groupId", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetTestRecords", groupIdParameter);
}
insert into #codes (Code, ActionCodes, Description)
exec TreatmentCodes_sps 0
SET FMTONLY OFF
ALTER PROCEDURE [dbo].[usp_GetTestRecords] 
    --@p1 int = 0, 
    --@p2 int = 0
    @groupId nvarchar(10) = 0
AS
BEGIN
    SET NOCOUNT ON;


    SELECT * FROM CUSTOMERS WHERE Id =  @groupId

END
ALTER PROCEDURE [dbo].[usp_GetProducts]

AS
BEGIN

 SET NOCOUNT ON;

 SELECT 
      , p.Id
      , p.Title
      , p.Description AS 'Description'

    FROM dbo.Products AS p
END
SET FMTONLY OFF
when FMTONLY is on: 1) only the schema is returned (no) rows. similar to adding a blanket false statement to your where clause (ie "where 1=0") 2) flow control statements are ignored set fmtonly on if 1=1 begin select 1 a end else begin select 1 a,2 b end while 1=1 select 1 c if 1=0 begin set fmtonly off end set fmtonly off declare @g varchar(30) set @g = 'fmtonly was set to off' if 1=0 begin set fmtonly off set @g = 'fmtonly was set to on' end select @g 1. EF turns FMTONLY on to prevent MUTATING data from executing stored procedures when it executes them during a model update. (from which it follows) 2. setting FMTONLY off in any procedure that EF will attempt to do a schema scan (potentially ANY and EACHONE) introduces the potential to mutate database data whenever *anyone* attempts to update their database model. --where [columnlist] matches the schema you want EF to pick up for your stored procedure if 1=0 begin select [columnlist] from [table list and joins] where 1=0 end declare @tablevar as table ( blah int ,moreblah varchar(20) ) if 1=0 begin select * from @tablevar end ... -- load data into @tablevar select * from @tablevar
 public partial class myEntities
    {
       public List<MyClass> usp_GetTestRecords(int _p1, int _p2, string _groupId)
       {
          // fill out params
          SqlParameter p1 = new SqlParameter("@p1", _p1);
          ...
          obj[] parameters = new object[] { p1, p2, groupId };

          // call the proc
          return this.Database.SqlQuery<MyClass>(@"EXECUTE usp_GetTestRecords @p1, @p2, @groupId", parameters).ToList();
       }
    }
    <FunctionImport Name="GetInvoiceByNumber"     ReturnType="Collection(Model.Invoice_Result)">
       <Parameter Name="InvoiceNumber" Mode="In" Type="Int32" />
    </FunctionImport>