简体   繁体   English

SQL 实体框架服务器

[英]SQL Server for Entity Framework

Do I have to install SQL Server to be able to work with a database while I have EntityFramework Core in my project?当我的项目中有 EntityFramework Core 时,是否必须安装 SQL 服务器才能使用数据库? Or does EntityFramework Core cover every need I would have for a database related matter?或者 EntityFramework Core 是否涵盖了我对数据库相关问题的所有需求?

Do I have to install SQL Server to be able to work with a database while I have EntityFramework Core in my project?当我的项目中有 EntityFramework Core 时,是否必须安装 SQL 服务器才能使用数据库?

No. Entity Framework is a device that adapts between C# LINQ statements, and various flavors of SQL in order to download database data and transform it into a tree of bjects you can work with in C#. No. Entity Framework is a device that adapts between C# LINQ statements, and various flavors of SQL in order to download database data and transform it into a tree of bjects you can work with in C#.

The specific SQL required is handled by a database-specific provider library, and there are many different providers.所需的特定 SQL 由特定于数据库的提供程序库处理,并且有许多不同的提供程序。 This enables EF to work with a wide range of different databases.这使 EF 能够处理各种不同的数据库。 You do need a database but it does not have to be SQL Server.您确实需要一个数据库,但它不一定是 SQL 服务器。 One popular alternative is SQLite, a file-based database that doesn't need a standalone server software to be installed;一种流行的替代方法是 SQLite,这是一种基于文件的数据库,不需要安装独立的服务器软件; it's incredibly powerful for what it is with a featureset that rivals enterprise grade databases like SQLServer, MySQL, Oracle and Postgres它的功能集可与 SQLServer、MySQL、Oracle 和 Postgres 等企业级数据库相媲美

Because you seem to want to use EF without necessarily using SQLServer, here is a link to a microsoft EFC tutorial that uses SQLite:因为您似乎想使用 EF 而不必使用 SQLServer,所以这里是一个使用 SQLite 的 microsoft EFC 教程的链接:

https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli https://docs.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli

Feel free to use any database that there is an EF provider for;随意使用任何有 EF 提供程序的数据库; the tutorial linked has a link off to various providers, and a simple web search would no doubt also bring a large list so I wont replicate it here链接的教程有一个链接到各个提供商,一个简单的 web 搜索无疑也会带来一个大列表,所以我不会在这里复制它

Or does EntityFramework Core cover every need I would have for a database related matter?或者 EntityFramework Core 是否涵盖了我对数据库相关问题的所有需求?

As mentioned EFC is purely a devices that sees you use LINQ, makes an SQL, fetches the data and gives you an object.如前所述,EFC 纯粹是一种设备,它会看到您使用 LINQ,生成 SQL,获取数据并为您提供 object。 It's the adapter between your code and the database As a high level overview, you'd perhaps have tables:它是您的代码和数据库之间的适配器作为高级概述,您可能会有表:

Department
DepartmentId, DeptName

Employee
EmployeeId, Name, HireDate, DepartmentId

You'd have classes representing them:你会有代表它们的类:

public class Employee {
  public int Id {get; set;}
  public string Name {get; set;}
  public DateTime {get; set;}

  public int DepartmentId {get; set;}
  public Department Department {get; set;}
}

public class Department {
  public int DepartmentId {get; set;}
  public string DeptName {get; set;}

  public ICollection<Employee> Employees {get; set;}
}

You'd form a LINQ to retrieve them in some way:您将形成一个 LINQ 以某种方式检索它们:

context.Employees.Include(e => e.Department).First(e => e.EmployeeId == 1);

EF would make an SQL (like this, not so readable) and run it: EF会生成一个 SQL (像这样,不太可读)并运行它:

SELECT TOP 1 * 
FROM Employees e INNER JOIN Departments d ON e.DepartmentId = d.DeparmtentId 
WHERE e.EmployeeId == 1

The database would give back some resultset:数据库将返回一些结果集:

1, John, 1970-01-01, 2, 2, Engineering

EF would pull the data part and store it into objects: EF将提取数据部分并将其存储到对象中:

Employee e = new Employee();
e.Id = datareader.GetInt("EmployeeId"); //1
e.Name = datareader.GetString("Name"); //John
e.HireDate = datareader.GetDate("HireDate");  //1970-01-01
e.DepartmentId = datareader.GetInt("DepartmentId"); //2

Department d = new Department();
d.Id = datareader.GetInt("DepartmentId"); //2
d.DeptName = datareader.GetString("DeptName"); //Engineering

e.Department = d;
d.Employees.Add(e);

And you'd get back a C# object with all its fields populated including a related department, with all its fields populated, so you can say eg emp.Department.Name (or even emp.Department.First().Employee.Department.First().Employee... - it's a 2-way linked graph so you can do lots of "dot dot dot" navigating around it.你会得到一个 C# object ,其所有字段都已填充,包括相关部门,所有字段都已填充,因此您可以说例如emp.Department.Name (甚至是 emp.Department.First().Employee.Department。 First().Employee... - it's a 2-way linked graph so you can do lots of “点点点”导航。

So, when you think about it, EF is doing a huge amount of boring work for you;所以,仔细想想,EF 正在为你做大量无聊的工作; making SQL, reading the DB, making objects.. All off the back of one line of C# that said "i want the employee with ID 1, and their department"制作 SQL,读取数据库,制作对象.. 都在 C# 的一行后面,上面写着“我想要 ID 为 1 的员工及其部门”

But it doesn't include the database itself because there are very valid reasons why different people would want different DBs它不包括数据库本身,因为不同的人想要不同的数据库是有充分理由的

EntityFramework Core is orm for database EntityFramework Core 是 orm 用于数据库

if you use EntityFramework you must have sql server to create databse and you must migrat EF and update database如果您使用 EntityFramework,您必须拥有 sql 服务器来创建数据库,并且您必须迁移 EF 并更新数据库

but if you can't install sql you can use docker但如果你不能安装 sql 你可以使用 docker

refrence:参考:


https://docs.microsoft.com/en-us/ef/core/ https://docs.microsoft.com/en-us/ef/core/


https://www.docker.com/ https://www.docker.com/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM