我的连接字符串:
<add name="addConnection" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\FLWorkDB.mdf" providerName="System.Data.SqlClient" />
<add name="FLWorkDBEntities" connectionString="metadata=res://*/MyEdmx.csdl|res://*/MyEdmx.ssdl|res://*/MyEdmx.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\FLWorkDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
我的网络API:
[HttpGet]
[Route("api/JobApi/BrowseJobs/")]
public object BrowseJobs()
{
using (var ctx = new FLWorkDBEntities())
{
//Get student name of string type
object studentName = ctx.Database.SqlQuery<string>(@"Select j.JobID,j.JobDetails,j.JobTitle, j.Deadline ,j.Budget,j.Category, emp.FirstName,emp.LastName,Skills = Stuff((Select ',' +SkillName From Skill Where charindex(concat(',',SkillID,','),','+j.ReqSkill+',')>0 For XML Path ('')),1,1,'')
From Job j
join Employeer emp on j.EmployeerID = emp.EmployeerID
WHERE NOT EXISTS
(SELECT *
FROM ClosedJob c
WHERE c.JobID = j.JobID) AND
NOT EXISTS
(SELECT *
FROM AppliedJob apj
join JobOffer jo on apj.AppliedJobID =jo.AppliedJobID
join Contract con on jo.OfferID =con.OfferID
WHERE con.CompletedDate != Null)").ToList();
}
return stu
}
现在我正在使用LINQ。但我想避免使用LINQ并添加直接的sql查询。
RAW Sql:"Select * from A where A.ID =2"
(加入)
我想在我的web api中添加RAW SQL Query,它返回json中的列表。 我可以在没有实体框架的情况下添加sql查询,我的意思是.edmx文件吗?
被修改 ============== 试过这个:
var results = db.Database.SqlQuery<FLWorkDBEntities>(@"Select j.JobID,j.JobDetails,j.JobTitle, j.Deadline ,j.Budget,j.Category, emp.FirstName,emp.LastName,Skills = Stuff((Select ',' +SkillName
From Skill
Where charindex(concat(',',SkillID,','),','+j.ReqSkill+',')>0
For XML Path ('')),1,1,'')
From Job j
join Employeer emp on j.EmployeerID = emp.EmployeerID
WHERE NOT EXISTS
(SELECT *
FROM ClosedJob c
WHERE c.JobID = j.JobID) AND
NOT EXISTS
(SELECT *
FROM AppliedJob apj
join JobOffer jo on apj.AppliedJobID =jo.AppliedJobID
join Contract con on jo.OfferID =con.OfferID
WHERE con.CompletedDate != Null)").ToList<FLWorkDBEntities>();
return JsonConvert.SerializeObject(results);
但回归:
Self referencing loop detected with type 'System.Data.Entity.DynamicProxie
=====的解决 ====================
我创建了一个存储过程(BrowseJobs
),然后使用以下代码附加它
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["addConnection"].ConnectionString);
SqlCommand com = new SqlCommand("BrowseJobs", con);
com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
答案 0 :(得分:1)
是。您可以浏览此链接http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
E.g;
unset($articles[$i])
您可以使用using (var context = new SqlDbContext())
{
//Get command for non entity types
string studentName = context.Database.SqlQuery<string>(@"SELECT StudentName FROM Student WHERE StudentID = 1").FirstOrDefault<string>();
//Get command for entity types. SQL columns returned by SQL query should match the property of the entity type
var students = context.Students.SqlQuery(@"SELECT * FROM Student").ToList<Student>();
//Update command
int noOfRowsUpdated = context.Database.ExecuteSqlCommand(@"UPDATE Student SET StudentName = 'New Name' WHERE StudentID = 1");
//Insert command
int noOfRowsInserted = context.Database.ExecuteSqlCommand(@"INSERT INTO Student(StudentName) VALUES('New Student')");
//Delete command
int noOfRowsDeleted = context.Database.ExecuteSqlCommand(@"DELETE FROM Student WHERE StudentID = 1");
}
以JSON格式返回列表。只需按照以下步骤操作:
Newtonsoft.Json
PM> Install-Package Newtonsoft.Json
using Newtonsoft.Json;
答案 1 :(得分:0)
您可以在实体框架中执行此操作。
文档链接:https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx
示例链接:http://www.entityframeworktutorial.net/EntityFramework4.3/raw-sql-query-in-entity-framework.aspx
To,在没有edmx的情况下执行,然后使用旧的ado.net。