EF 6码第一个存储过程 - 只读 [英] EF 6 Code First Stored Procedure - Read Only
问题描述
我寻觅了几个帖子,不过既然来了短。我先用EF6代码试图从一个存储过程已经在数据库中设置的结果。我的应用程序很简单,它由两个不同的服务器获取数据,进行一些业务逻辑,然后显示用户。我可以使用的.edmx
文件罚款,在XML文件中的函数映射。当我使用电动工具对自己表现出的XML文件,我不从我的代码中看到的功能导入下面,所以我要么缺少一个设置或无法使用 ExecuteFunction来()
。
-
我可以使用
ExecuteFunction来()
使用代码第一?我的存储过程只返回记录。我之所以有这样的设置是因为存储过程订阅其他应用程序,我们希望让所有的更改查询在一个地方(SSMS)。我知道我可以使用ExecuteStoredQuery
/ExecureStoredCommand
,但我想坚持到约定如果我要使用的.edmx
模式。 -
如果我可以使用
ExecuteFunction来
,在哪里以及如何配置我的的DbContext
来识别存储过程?随着我的设置下面我收到错误
的FunctionImport {0}不能在容器中找到{1}
块引用>
我可以用流利的API?谢谢
公共类JobContext:的DbContext
{
公共JobContext()
:基地(名称= JobContext)
{
//我上下文将只定义数据库
Database.SetInitializer℃的切片; JobContext>(空);
}
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.ComplexType<人才与GT;();
}
公共虚拟ObjectResult<人才与GT; uspGetJobs(字符串的startDate)
{
VAR startDateParameter =的startDate!= NULL?
新ObjectParameter(的startDate的startDate):
新ObjectParameter(的startDate,typeof运算(字符串));
回报率((IObjectContextAdapter)本).ObjectContext<人才与GT(uspGetJobs,startDateParameter);
}
}
解决方案自。这是一个只读存储过程,我落得这样做,而不是试图致力于模拟生成的模型这个
public虚拟目录<工作与GT ; uspGetJobs(字符串的startDate)
{
VAR startDateParameter =的startDate!= NULL?
新的SqlParameter(的startDate的startDate):
新的SqlParameter(的startDate,typeof运算(字符串));
返回this.Database.SqlQuery< PlannedJobs>(uspGetPlannedJobs @startDate,@locationCode,startDateParameter,locationCodeParameter).ToList();
}
I have searched a few posts, but have come up short. I am using EF6 code first trying to get results from a stored procedure that is already setup in a database. My application is simple, it takes data from two different servers, performs some business logic, and then shows the user. I can use the
.edmx
file fine, which maps the function in the xml file. When I use Power Tools to show myself the XML file I do not see the function import from my code below so I am either missing a setup or I cannot useExecuteFunction()
.
Can I use the
ExecuteFunction()
with code first? My stored procedure only returns records. The reason I have this setup is because the stored procedure feeds another application and we want to keep all the changes to the query in one place (SSMS). I realize I could useExecuteStoredQuery
/ExecureStoredCommand
, but I wanted to stick to the convention if I were to use the.edmx
model.If I can use
ExecuteFunction
, where and how do I configure myDbContext
to recognize the stored procedure? With my setup below I receive the error
The FunctionImport {0} could not be found in the container {1}
Can I use Fluent API? Thanks.
public class JobContext : DbContext { public JobContext() : base("name=JobContext") { // My context will only define a slice of the database Database.SetInitializer<JobContext>(null); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.ComplexType<Job>(); } public virtual ObjectResult<Job> uspGetJobs(string startDate) { var startDateParameter = startDate != null ? new ObjectParameter("startDate", startDate) : new ObjectParameter("startDate", typeof(string)); return ((IObjectContextAdapter)this).ObjectContext.<Job>("uspGetJobs", startDateParameter); } }
解决方案since it was a read only stored procedure I ended up doing this instead of trying to mimick the generated model.
public virtual List<Jobs> uspGetJobs(string startDate) { var startDateParameter = startDate != null ? new SqlParameter("startDate", startDate) : new SqlParameter("startDate", typeof(string)); return this.Database.SqlQuery<PlannedJobs>("uspGetPlannedJobs @startDate, @locationCode", startDateParameter, locationCodeParameter).ToList(); }
这篇关于EF 6码第一个存储过程 - 只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!