简体   繁体   English

SQL Server +实体框架基础知识

[英]SQL Server + Entity Framework basics

I have this task to create a program in C# for managing company's staff. 我有这个任务在C#中创建一个用于管理公司员工的程序。 Only a brief overview - all the information about each employee is to be stored in MS SQL database. 仅简要概述 - 有关每个员工的所有信息都将存储在MS SQL数据库中。 As a presentation layer, I have to use WPF and as a communication with database - LINQ to Entities. 作为表示层,我必须使用WPF并与数据库进行通信 - LINQ to Entities。

The thing is - I managed to learn WPF on my own, but SQL is a serious matter. 事情是 - 我设法自己学习WPF,但SQL是一个严重的问题。 I've done some research, but even after reading many different tutorials I haven't found anything satisfying. 我做了一些研究,但即使阅读了许多不同的教程,我也没有找到任何令人满意的东西。 I don't even understand the mechanics of this model. 我甚至不了解这个模型的机制。 It is my understanding, that creating a database in SQL Server simply does nothing, because after one creates Entity model from the database, it is no longer tied to SQL Server. 我的理解是,在SQL Server中创建数据库根本不做任何事情,因为在从数据库创建实体模型后,它不再与SQL Server绑定。 So updating data in created SQL database is no longer of use to the program itself. 因此,更新已创建的SQL数据库中的数据不再适用于程序本身。 I may be wrong, and that is highly probable, but I just don't get it. 我可能错了,这很有可能,但我只是不明白。

So, can anyone point me to a right direction? 那么,有人能指出我正确的方向吗? What do I even have to know to start? 我甚至需要知道什么才能开始? Maybe some tutorials for such a noob? 也许这样一个菜鸟的教程?

ADO.NET Entity Framework (EF) was first released with Visual Studio 2008 and .NET Framework 3.5 Service Pack 1. So far, many people view EF as just another ORM product from Microsoft, though by design it is supposed to be much more powerful than just an ORM tool.A new data-access provider, EntityClient, is created for this new framework but under the hood, the ADO.NET data providers are still being used to communicate with the databases ADO.NET实体框架(EF)最初是与Visual Studio 2008和.NET Framework 3.5 Service Pack 1一起发布的。到目前为止,许多人将EF视为来自Microsoft的另一个ORM产品,尽管通过设计它应该更强大而不仅仅是一个ORM工具。为这个新框架创建了一个新的数据访问提供程序EntityClient,但在幕后,ADO.NET数据提供程序仍然用于与数据库通信

The application no longer connects directly to a database or sees any database-specific construct; 应用程序不再直接连接到数据库或查看任何特定于数据库的构造; the entire application operates in terms of the higher-level EDM model. 整个应用程序在更高级别的EDM模型方面运行。 This means that you can no longer use the native database query language; 这意味着您不能再使用本机数据库查询语言; not only will the database not understand the EDM model, but also current database query languages do not have the constructs required to deal with the elements introduced by the EDM such as inheritance, relationships, complex-types, etc. 数据库不仅不能理解EDM模型,而且当前的数据库查询语言也没有处理EDM引入的元素所需的构造,如继承,关系,复杂类型等。

. LINQ to Entities allows developers to create flexible, strongly typed queries against the Entity Data Model (EDM) by using LINQ expressions and the LINQ standard query operators LINQ to Entities允许开发人员使用LINQ表达式和LINQ标准查询运算符,针对实体数据模型(EDM)创建灵活的强类型查询

Tutorial can be Found Here 可以在这里找到教程

It's basically the opposite of what you said actually: 它基本上与你实际说的相反:

It is my understanding, that creating a database in SQL Server simply does nothing, because after one creates Entity model from the database, it is no longer tied to SQL Server. 我的理解是,在SQL Server中创建数据库根本不做任何事情,因为在从数据库创建实体模型后,它不再与SQL Server绑定。 So updating data in created SQL database is no longer of use to the program itself. 因此,更新已创建的SQL数据库中的数据不再适用于程序本身。

What happens is the Entity Framework (EF) connects to your database and gets the data from your SQL Server. 实际框架(EF)连接到您的数据库并从您的SQL Server获取数据。 From then, when you make changes in your app, EF operates in 'detached' sort of mode. 从那时起,当您在应用程序中进行更改时,EF将以“分离”模式运行。 This is probably the source of your confusion. 这可能是你困惑的根源。 The piece you are missing, is that you tell EF using ef.SaveChanges() to push all of the changes made in 'detached' mode, back to your database. 您缺少的部分是,您使用ef.SaveChanges()告诉EF将所有在“分离”模式下所做的更改推送回您的数据库。 So you make your changes in memory, then when you are ready, you tell EF to push those changes to the database. 因此,您在内存中进行更改,然后在准备就绪时,告诉EF将这些更改推送到数据库。

Now to your other point, if you make a change directly to SQL Server (which I could see scenarios where this would occur), you certainly would run into issues with your 'detached' mode EF objects. 现在,另一方面,如果您直接对SQL Server进行更改(我可以看到会发生这种情况的情况),您肯定会遇到“分离”模式EF对象的问题。 I'm honestly not sure what would happen here, someone more experienced than me should say, that's a really interesting question that I'd also like to know the answer to. 老实说,我不确定这里会发生什么,比我更有经验的人应该说,这是一个非常有趣的问题,我也想知道答案。

Here are some tutorial infos that I found that seemed good: 以下是一些我发现似乎很好的教程信息:

http://www.microsoft.com/downloads/details.aspx?FamilyID=355c80e9-fde0-4812-98b5-8a03f5874e96&displaylang=en which is actually in this answer http://www.microsoft.com/downloads/details.aspx?FamilyID=355c80e9-fde0-4812-98b5-8a03f5874e96&displaylang=en实际上是这个答案

and also: 并且:

http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B

EDIT: when I talk about detached mode, understand that inside EF there are various modes that you can operate in, one of which is detached mode. 编辑:当我谈到分离模式时,了解在EF内部有各种模式可以操作,其中一种是分离模式。 I hated to use the term 'detatched mode' in my answer b/c I know others will come along and say that term is confusing, and they are correct. 我讨厌在我的回答中使用“detatched mode”这个术语b / c我知道其他人会出现并说这个术语令人困惑,而且它们是正确的。 Just understand that inside EF there is a 'detatched' mode and it is somewhat of an advanced topic. 只是了解EF内部有一个'detatched'模式,它有点像一个高级主题。

Entity Framework is very easy and very powerful ORM. 实体框架非常简单且非常强大的ORM。 My suggestion to you would be to start with http://www.asp.net/entity-framework . 我对你的建议是从http://www.asp.net/entity-framework开始。 Here you can find a lot of Get Started tutorials to help you up and running in a short time. 在这里,您可以找到许多入门教程,以帮助您在短时间内启动和运行。 Although these tutorials are tied with ASP.NET, you can very easily understand them and use them with other platforms. 虽然这些教程与ASP.NET绑定,但您可以非常轻松地理解它们并将其与其他平台一起使用。

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

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