EF对于已有数据库的Code First支持
EF对于已有数据库的Code First支持
本文将逐步介绍怎样用Code First的方式基于已有数据库进行开发。Code First支持你使用C#或者VB.Net定义类.并使用数据模型标识和Fluent API定义与配置模型。
前提
已经安装 Visual Studio 2012 或者 Visual Studio 2013
同时,你还需要安装Entity Framework Tools for Visual Studio的6.1或更高版本。请参考Get Entity Framework
1. 创建一个数据库
因为本文是研究基于已有数据库进行Code First,所以,我们先创建一个数据库作为我们的操作目标。(至于创建数据库的过程,可以去 原文链接 看图操作,这部分内容我就不翻译了)
Let’s go ahead and generate the database.
Open Visual Studio
View -> Server Explorer
Right click on Data Connections -> Add Connection…
If you haven’t connected to a database from Server Explorer before you’ll need to select Microsoft SQL Server as the data source
- Connect to your LocalDb instance ((localdb)\v11.0), and enter Blogging as the database name
- Select OK and you will be asked if you want to create a new database, select Yes
The new database will now appear in Server Explorer, right-click on it and select New Query
复制以下代码,到数据库管理器中执行,以生成新的数据表
CREATE TABLE [dbo].[Blogs] (
[BlogId] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (200) NULL,
[Url] NVARCHAR (200) NULL,
CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([BlogId] ASC)
);
CREATE TABLE [dbo].[Posts] (
[PostId] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (200) NULL,
[Content] NTEXT NULL,
[BlogId] INT NOT NULL,
CONSTRAINT [PK_dbo.Posts] PRIMARY KEY CLUSTERED ([PostId] ASC),
CONSTRAINT [FK_dbo.Posts_dbo.Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [dbo].[Blogs] ([BlogId]) ON DELETE CASCADE
);
INSERT INTO [dbo].[Blogs] ([Name],[Url])
VALUES ('The Visual Studio Blog', 'http://blogs.msdn.com/visualstudio/')
INSERT INTO [dbo].[Blogs] ([Name],[Url])
VALUES ('.NET Framework Blog', 'http://blogs.msdn.com/dotnet/')
2.创建应用程序
依然基于简单原则,我们还是创建一个控制台应用程序,并用Code First进行数据库操作:
- 打开 Visual Studio
- 文件 -> 新建 -> 项目…
- 选择控制台应用程序
- 输入 CodeFirstExistingDatabaseSample 作为项目名
- 点击确定
3.逆向工程
我们将使用Entity framework Tools for Visual Studio来生成一些基于数据库的初始化代码。当然,你也可以手动创建这些代码。
项目 -> 添加新项…
从左侧菜单的数据(Data)分类中选择Ado.net 实体模型(Ado.net Entity Data Model)(目前我的电脑上没装中文版,在菜单中具体显示的选项是什么 可能会不太精确,这里我只能是把大概意思译一下,请见谅,谢谢。)
将新文件命名为 BloggingContext 并点击确定
这时会启动实体模型创建向导
选择从数据库创建模型(Code First from DataBase) 并点击下一步
- 创建连接并点击下一步
- 选择要导入的数据表并点击完成
当逆向工程完成后,项目中会自动生成一些文件,现在让我们看看到底添加了一些什么内容吧。
配置文件
在项目根目录下添加了一个App.config
的文件,这个文件中包含了一个有关目标数据库的连接字符串.
<connectionStrings>
<add
name="BloggingContext"
connectionString="data source=(localdb)\v11.0;initial catalog=Blogging;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>
你还会注意到配置文件中的其他一些设置,这些是默认的EF设置,它告诉Code First在哪里创建数据库。 由于我们正在映射到现有数据库,因此我们的应用程序将忽略这些设置。
衍生(派生)DbContext
在项目目录下添加了一个名为BloggingContext的文件。这是与数据交互的上下文,通过它我们可以从数据库中查询数据或保存数据到数据库。同时,BloggingContext为每一个表公开了一个DbSet<TEntity>属性。 您还会注意到,默认构造函数使用 name = syntax语法调用基础构造函数。 这是告诉Code First,应该从配置文件基于连接字符串名称加载用于此上下文的连接字符串。
public partial class BloggingContext : DbContext
{
public BloggingContext()
: base("name=BloggingContext")
{
}
public virtual DbSet<Blog> Blogs { get; set; }
public virtual DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
当您在配置文件中使用连接字符串时,应始终使用name = syntax。 这样可以确保如果连接字符串不存在,那么Entity Framework将抛出异常而不是按照惯例创建一个新的数据库。
实体模型类
最后,一个Blog和Post类也被添加到项目中。 这些是构成我们模型的实体类。 你将看到应用于类的数据模型标识配置,其中Code First约定与现有数据库的结构不一致。 例如,你在Blog.Name和Blog.Url上看到StringLength特性,因为它们在数据库中的最大长度为200(Code First 默认是使用数据库提供程序支持的最大长度 - nvarchar(max))(你可以尝试一下用Code First创建一个新的数据表,其中的string类型字段不用StringLength或MaxLength进行标识,你会发现,在数据库中自动生成的这个字段的类型就是nvarchar(max))。
public partial class Blog
{
public Blog()
{
Posts = new HashSet<Post>();
}
public int BlogId { get; set; }
[StringLength(200)]
public string Name { get; set; }
[StringLength(200)]
public string Url { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
4.读写数据
现在,数据模型的逆向工程完成了,是时候与数据库交互进行一些数据访问了。在Main函数中实现如下代码:
这段代码的意思是创建一个新的 DbContext的对象实例,再用它去插入一个新的Blog对象到数据库.再用Linq to Sql 从数据库中查询所有的Blogs,并按 Name 属性排序。 在此过程中你完全感觉不到你在操作数据库,是的,你就是在操作面向对象语言,EF帮你实现了与数据库的所有交互
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
// Create and save a new Blog
Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
// Display all Blogs from the database
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("All blogs in the database:");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
现在请运行这个程序,并查看运行结果,结果如下:
Enter a name for a new Blog: ADO.NET Blog
All blogs in the database:
.NET Framework Blog
ADO.NET Blog
The Visual Studio Blog
Press any key to exit...
Customizing the Scaffolded Code
For information on customizing the code that is generated by the wizard, see Customizing Code First to an Existing Database.
What if My Database Changes?
The Code First to Database wizard is designed to generate a starting point set of classes that you can then tweak and modify. If your database schema changes you can either manually edit the classes or perform another reverse engineer to overwrite the classes.
Using Code First Migrations with an Existing Database
If you want to use Code First Migrations with your existing database, see Code First Migrations with an existing database.
Summary
In this walkthrough we looked at Code First development using an existing database. We used the Entity Framework Tools for Visual Studio to reverse engineer a set of classes that mapped to the database and could be used to store and retrieve data.
EF对于已有数据库的Code First支持的更多相关文章
- EF POWER TOOLS由数据库逆向CODE FIRST
EF POWER TOOLS由数据库逆向CODE FIRST 前言 利用db first的开发方式有很多可供选择的方案,一种可以用ado.net实体框架模型,由向导直接生成edmx,并生成数据库上下文 ...
- EF框架step by step(4)—DBcontext应用于已存在数据库
EF4.1有三种方式来进行数据操作及持久化.分别是Database-First,Model-First,Code-first,前面都已经简单介绍过了.下面简单小结一下:1.Database First ...
- System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
System.InvalidOperationException: 支持“XXX”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.micro ...
- EntityFramework Core Code First 已有数据库
问题场景:我已经有一个数据库,想用 EF core Code First,怎么办? 首先,可以参考微软的API文档:通过现有数据库在 ASP.NET Core 上开始使用 EF Core, 这一步可以 ...
- 支持“***Context”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
在用VS进行MVC开发的过程中遇到如下问题: 支持“***Context”上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.microsoft ...
- "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库。
一,在我使用自动生成数据库的时候,当你改变了数据库就会出现下面问题 "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改. ...
- 支持“xxxContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
将项目的数据库连接用户及密码修改后(切换用户,用户名与原来不一样,用户下对象结构一致),报以下错误: 支持“XXXDBContext”上下文的模型已在数据库创建后发生更改.请考虑使用 Code Fir ...
- 错误:支持“EFDbContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId
支持"EFDbContext"上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/ ...
- 支持“EFDBContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
在修改数据库表后会出现 支持"EFDBContext"上下文的模型已在数据库创建后发生更改.请考虑使用 Code First 迁移更新数据库 这个问题解决方法: 在Global.a ...
随机推荐
- 4.vbs的循环,switch,判断等语句
1.条件判断语句 If Then Else Sub judge(x) Then MsgBox "the num is 0" Then MsgBox "the num is ...
- 再谈AbstractQueuedSynchronizer:共享模式与基于Condition的等待/通知机制实现
共享模式acquire实现流程 上文我们讲解了AbstractQueuedSynchronizer独占模式的acquire实现流程,本文趁热打铁继续看一下AbstractQueuedSynchroni ...
- 探索Windows命令行系列(7):通过命令编译C#类和Java类
1.编译 C# 类 1.1.C# 编译工具 1.2.编译一个 C# 类 1.3.编译多个 C# 类 2.编译 Java 类 2.1.Java 编译工具 2.2.编译 Java 类 3.组合命令符 4. ...
- Wireshark初步入门
第一次捕获数据包 为了能让Wireshark得到一些数据包,你可以开始你的第一次数据包捕获实验了.你可能会想:"当网络什么问题都没有的时候,怎么能捕获数据包呢?" 首先,网络总是有 ...
- (转载)基于Bash命令行的百度云上传下载工具
原文链接:http://hi.baidu.com/meoow/item/aef5814bbd5be3e1bcf451e9 这是我根据百度云PCS的API写的一个基于bash的命令行工具, 使用了cur ...
- Java Web - HTML 常用标签
1.HTML head, title, body 标签 其中 meta 中的keywords是为了快速的让搜索引擎找到 <html> <head> <title>这 ...
- js字符串与数组的相互转换
一.数组转字符串,通过join()拼接数组元素 var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); c = a.join('');consol ...
- [CF337D]邪恶古籍-树状dp
Problem 邪恶古籍 题目大意 给出一些关键点,求这棵树上到最远关键点距离小于等于d的有多少个. Solution 一个非常简单的树形dp.然而我被这道题给玩坏了. 在经过分析以后,我们发现只需要 ...
- C#语法糖演进-C#语言和.NET Framework平台介绍
p { font-size: 14px; text-indent: 2em } .title { text-indent: 0; font-size: 22px; font-weight: bold; ...
- mybatis中resultType和resultMap的联系
在使用mybatis进行数据库连接操作时对于SQL语句返回结果的处理通常有两种方式,一种就是resultType另一种就是resultMap,下面说下我对这两者的认识和理解 比如,我们平时使用的单表查 ...