简体   繁体   English

首先从SQL移到Entity Framework代码

[英]Moving from SQL to Entity Framework Code First

I am making the journey from an SQL centric world (where I am reasonably fluent) to EF dDbContect with code first and I am struggling. 我正在以代码为中心,从以SQL为中心的世界(我相当熟练)到EF dDbContect,而我一直在挣扎。 The following is a simplified example; 以下是一个简化的示例; I want to write the following simple SQL query (which took 60 seconds to write): 我想编写以下简单的SQL查询(编写花费了60秒):

SELECT
    HP.HospitalID
FROM
    Hospitals AS HP
    JOIN NHSTrusts AS NT
    ON NT.NHSTrustID = HP.NHSTrust_NHSTrustID

WHERE
    HP.HospitalName + ', ' + NT.NHSTrustName = 'My hospital name including trust'

as an EF style query. 作为EF样式查询。 I cannot see how to do this and I don't want to drop back into SQL everytime I cannot see how to do something. 我看不到该怎么做,也不想每次都看不到该做些什么时就退回到SQL。

Can anyone help: 谁能帮忙:

  • On how to frame the above query in EF dbContent 关于如何在EF dbContent中构建上述查询
  • On a general source of help with 关于帮助的一般来源

Assuming your entities and DB Context are properly set, here's how your query may look like: 假设您的实体和数据库上下文已正确设置,则查询如下所示:

var hospitalIds = 
    from hp in dbContext.Hospitals
    where 
        hp.HospitalName == "..." &&
        hp.Trust.NHSTrustName == "..."
    select hp.HospitalId;

Of course, this query will have to be materialized by iterating through the results (or by using .ToList() / .ToArray() ). 当然,必须通过遍历结果(或使用.ToList() / .ToArray() )来实现此查询。

To define the entities and context properly, refer to this excellent tutorial on MSDN: http://msdn.microsoft.com/en-us/data/jj193542 . 要正确定义实体和上下文,请参考MSDN上的出色教程: http : //msdn.microsoft.com/zh-cn/data/jj193542

Assuming these entity types: 假设这些实体类型:

public class Trust
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Hospital> Hospitals { get; set; }
}

public class Hospital
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int TrustId { get; set; }
    public Trust Trust { get; set; }
}

you'll have this query: 您将得到以下查询:

dbContext
    .Hospitals
    .Where(h => h.Name + h.Trust.Name == "My hospital name including trust")
    .Select(h => h.Id);

First try some Linq. 首先尝试一些Linq。 You can write it as below. 您可以如下编写。 The query might have some mistake, it will be corrected by Intellisense. 该查询可能有一些错误,它将被Intellisense纠正。

var s  = (from c in Context.Hospitals
          from h in Context.NHSTrusts
          where c.NHSTrust_NHSTrustID==h.NHSTrustID 
          && string.Format("{0},{1}",c.HospitalName,h.NHSTrustName).Equals("My hospital name including trust")

          select c.HospitalId).FirstOrDefault();

For a good place to get some examples i would recommend 101 linq samples 为了获得一些示例的好地方,我建议使用101个linq样本
It has everything from using a WHERE to GROUP BY statements. 它具有从使用WHERE到GROUP BY语句的所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架6从代码优先迁移中获取SQL - Entity Framework 6 get the sql from code first migration 从Entity Framework Code First版本5使用SQL视图 - Using an SQL View from an Entity Framework Code First version 5 使用Entity Framework 6 Code First创建SQL视图 - Create SQL View With Entity Framework 6 Code First 外部SQL服务器上的实体框架代码优先 - Entity Framework Code First on external SQL server 实体框架代码第一个来自数据库的种子 - Entity Framework Code First Seed From Database 实体框架如何使用代码优先方法生成此SQL代码? - Entity Framework how to produce this SQL code with code first approach? 实体框架代码前两个导航属性到/来自一个抽象实体 - Entity Framework Code First Two navigationProperty to/from one abstract entity 使用代码优先迁移的Entity Framework从SQL Server到Oracle - From SQL Server to Oracle using Entity Framework with code-first Migrations 将数据库从嵌入式更改为SQL Server Express后,Entity Framework 4.1代码第一个模型兼容性问题 - Entity Framework 4.1 code first model compatibility issue after changing database from embedded to SQL Server Express 从资源执行的SQL脚本会在Entity Framework 6 Code First Migration上引发错误 - SQL Script executed from resource throws errors on Entity Framework 6 Code First Migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM