Move Configurations to Separate Class in Code-First: By now, we have configured all the domain classes in OnModelCreating method in the previous sections. When you have a large number of domain classes, then configuring every class in OnModelCreating…
Linq-to-Entities Projection Queries: Here, you will learn how to write LINQ-to-Entities queries and get the result entities. Visit LINQ Tutorials to learn LINQ step by step. Projection is a process of selecting data in a different shape rather than a…
一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个介于牛A与牛C之间的功能,从4.1 开始的Code first使程序员,使软件开发流程进入一个更加方便快捷的时代. Code First是EntityFramework实现ORM的一种有利手段,因为传统编程方式都是先建立数据库,然后根据数据库模型为应用程序建模,再进行开发:CodeFirst代码优先…
Entity Framework Code First 利用一种被称为约定(Conventions)优于配置(Configuration)的编程模式允许你使用自己的 domain classes 来表示 EF 所依赖的模型去执行查询.更改追踪.以及更新功能,这意味着你的 domain classes 必须遵循 EF 所使用的约定.然而,如果你的 domain classes 不能遵循 EF 所使用的约定,此时你就需要有能力去增加一些配置使得你的 classes 能够满足 EF 所需要的信息. C…
------------------------------------------------------------------------------------------------------------ 注意:以下所讨论的功能或 API 等只针对 Entity Framework 6 ,如果你使用早期版本,可能部分或全部功能不起作用! --------------------------------------------------------------------------…
Entity Framework 简言之就是一个ORM(Object-Relational Mapper)框架. Code First 使得你能够通过C#的类来描述一个模型,模型如何被发现/检测就是通过一些约定(Conventions).Conventions 就是一系列规则的集合,被用于对基于类别定义的概念模型的自动装配. 这些约定都被定义于 System.Data.Entity.ModelConfiguration.Conventions 命名空间下. 当然你可以进一步地对你的模型作出配置,…
Code First development with Entity Framework: Entity Framework supports three different development approaches to use entity framework in your application. Code First Model First Database first Code First: In the Code First approach, you avoid workin…
声明:本文针对 EF5+, Visual Studio 2012+ 空间数据类型(Spatial Data Types)是在 EF5 中引入的,空间数据类型表现有两种: Geography (地理学上的)- 存储的是椭圆形数据,如 GPS 的经纬度坐标; Geometry  (几何学上的)- 代表欧氏几何(平面的)坐标系统. 下面示例该数据类型的一个应用程序 新建 C# 控制台应用程序 相信 Console 应该都会建,命名为 SpatialCodeFirst 使用 Code First 建立…
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Fluent API.  一般来说我们访问 Fluent API 是通过重写继承自 DbContext 的类中方法 OnModelCreating. 为了便于例示,我们先创建一个继承自 DbContext 的类,以及其它的一些类以便使用 public class SchoolEntities : DbCo…
创建初始模型和数据库 在开始使用迁移(Migrations)之前,我们需要一个 Project 和一个 Code First Model, 对于本文将使用典型的 Blog 和 Post 模型 创建一个新的控制台应用程序 MigrationsDemo; 添加最新的 EntityFramework 到项目 Tools –> Library Package Manager –> Package Manager Console; 运行命令 Install-Package EntityFramework…
声明:本文只针对 EF6+ 默认情况下,Code First 对实体进行插入.更新.删除操作是直接在表上进行的,从 EF6 开始你可以选择使用存储过程(Stored Procedures) 简单实体映射 Basic Entity Mapping 注意:本文将使用 Fluent API 来配置使用存储过程 public class Blog { public int BlogId { get; set; } public string Name { get; set; } public strin…
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime EnrollmentDate { get; set; } // Navigation properties public virtual Address Address…
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime EnrollmentDate { get; set; } // Navigation properties public virtual Address Address…
EF开源项目地址:https://github.com/aspnet/EntityFramework6 MSDN :https://msdn.microsoft.com/en-us/library/aa937723(v=vs.113).aspx 中文:https://msdn.microsoft.com/zh-cn/library/bb399567(v=vs.110).aspx 开始EF 1.安装EF 新建类库项目,打开程序包控制台安装EF PM>Install-Package EntityFr…
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means delaying the loading of related data, until you specifically request for it. For example, Student class contains StudentAddress as a complex property…
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved using the Include() method. In the following example, it gets all the students from the dat…
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should target .NET framework 4.5 in order to use Enum. Enum can be created for the following data types: Int16 Int32 Int64 Byte SByte You can create and use an E…
Stored Procedure in Entity Framework: Entity Framework has the ability to automatically build native commands for the database based on your LINQ to Entities or Entity SQL queries, as well as, build the commands for inserting, updating, or deleting d…
Concurrency in Entity Framework: Entity Framework supports Optimistic Concurrency by default. In the optimistic concurrency, EF saves the entity to the database, assuming that the same data has not changed since the entity was loaded. If it determine…
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex task. It is easy to add a new entity graph in disconnected mode, but to update an entity graph needs careful design consideration. The problem in upda…
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let's see how to associate disconnected entity graph with the new context instance. There are two things we need to do when we get a disconnected entity…
Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on entities during its life time. Entity framework supports automatic change tracking of the loaded entities during the life time of the context. DbChangeTr…
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here, you will learn the different types of queries an entity framework supports, which is in turn converted into SQL query for the underlaying database.…
Model First development with Entity Framework: In the Model First approach, you create Entities, relationships, and inheritance hierarchies directly on the design surface of EDMX and then generate database from your model. So, in the Model First appr…
DBContext: As you have seen in the previous Create Entity Data Model section, EDM generates the SchoolDBEntities class, which was derived from the System.Data.Entity.DbContext class, as shown below. The class that derives DbContext is called context…
Create Entity Data Model: Here, we are going to create an Entity Data Model (EDM) for SchoolDB database and understand the basic building blocks. Entity Data Model is a model that describes entities and the relationships between them. Let's create fi…
What is Entity Framework? Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called "Entity Framework" to automate database related activities for your application. Microso…
以下系列文章为Entity Framework Turial Basics系列 http://www.entityframeworktutorial.net/EntityFramework5/entity-framework5-introduction.aspx ----------------------------------------------------------------------------------------------------------------------…
Download Sample Project: Download sample project for basic Entity Framework tutorials. Sample project includes SchoolDB.mdf for SQL Server 2012. It also includes SchoolDB.sql script if you are using a different version of SQL Server.…
Colored Entity in Entity Framework 5.0 You can change the color of an entity in the designer so that it would be easy to see related groups of entities in the designer from Visual Studio 2012 onwards. To change the color of an entity, select entity i…