EF Code First学习笔记 初识Code First(转)
Code First是Entity Framework提供的一种新的编程模型。通过Code First我们可以在还没有建立数据库的情况下就开始编码,然后通过代码来生成数据库。
下面通过一个简单的示例来了解。
建立一个控制台项目。通过Nuget来获取Entity Framework。
增加两个模型类:
public class Destination
{
public int DestinationId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Description { get; set; }
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
} public class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public Destination Destination { get; set; }
}
再新增Context类:
public class BreakAwayContext : DbContext
{
public DbSet<Destination> Destinations { get; set; }
public DbSet<Lodging> Lodgings { get; set; }
}
在Main方法中加入下列代码:
static void Main(string[] args)
{
var d = DateTime.Now.Date.ToString("yyyyMM");
var destination = new Destination
{
Country = "Indonesia",
Description = "EcoTourism at its best in exquisite Bali",
Name = "Bali"
};
using (var context = new BreakAwayContext())
{
context.Destinations.Add(destination);
context.SaveChanges();
}
Console.WriteLine("OK");
}
执行成功后打开数据库,默认为.\SQLEXPRESS。
我们可以看到,新增了一个名为BreakAway.BreakAwayContext的数据库。
[Destinations]表里面也插入了我们刚增加的记录:
很COOL吧,Code First就是这么神奇。这里我们代码里面什么也没设置,都是Code First默认生成的。通过生成的数据库库我们来了解一下这些默认设置。
数据库名:当没有显示设置数据连接的时候,默认的数据库是:.\SQLEXPRESS。如果本地没有SQLEXPRESS,EF会尝试LocalDb ((localdb)\v11.0) .\SQLEXPRESS
这个数据库包含在VS2012中。数据库的名称一般是DbContext的“命名空间.类名”,本例中是BreakAway.BreakAwayContext
表名:表名默认为模型类名的复数形式,并且每个表都使用dbo构架创建。这里生成的就是dbo.Lodgings.
主键:Code First会默认将以类似Id结尾来命名的属性当作主键,如ID,Id,本例中的DestinationId都自动设置为主键。如果该属性是int类型,Code First会在数据库中默认将该列设置为自增长。
数据类型:在SQL Server中,字符串默认映射成nvarchar(max),byte[]映射成varbinary(max),bool映射成bit,decimal映射成decimal(18, 2),float映射成float。同时因为bool,decimal,float等是值类型,不能为给他们分配Null值。所生成的数据库会要求对应的列非空。如Lodgings表中的IsResort
外键:Code First检测到模型间有一对多的关系,会自动在相应表中生成外键。在这时,Code First检测到Destination类中有一个List<Lodging> Lodgings属性,而在Lodging类中有一个Destination Destination属性,说明Destination与Lodging是一对多的关系,因而在Lodgings表中生成了外键Destination_DestinationId保存对应的DestinationId。外键的命名默认是导航属性名(这里是Destination)_对应主表的主键(这里是DestinationId)。
转自:http://www.cnblogs.com/Gyoung/archive/2013/01/17/2863145.html
最新文章
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
- 【LintCode】计算两个数的交集(一)
- c++ SOA Axis2c 编译安装
- IOS中的多核并发编程GCD
- Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
- 第二百五十八天 how can I 坚持
- linux中shell如何输出换行符
- Weblogic8.1 的性能优化
- js获取浮动(float)元素的style.left值为空的解决办法
- Eclipse中SVN设置文件为ignore后重新添加至版本控制
- 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 */
- 尝试在条件“$(_DeviceSdkVersion) >= 21”中对计算结果为“”而不是数字的“$(_DeviceSdkVersion)
- Linux系统的基本使用
- consistent.go 源码阅读
- javascript最全最好的判断数组的方法
- ArcGIS for qml -关于空间参考如何选择设置
- Elasticsearch 快速入门教程
- ubuntu14.04 + cuda8.0 + cudnnv5 + caffe + py-faster-rcnn配置
- 【Java】 剑指offer(40) 最小的k个数
- 数据在内存中的存储方式( Big Endian和Little Endian的区别 )(x86系列则采用little endian方式存储数据)
热门文章
- 23种设计模式之状态模式(State)
- Django---应用如何创建
- [分布式系统学习] 6.824 LEC2 RPC和线程 笔记
- postgresql----文本搜索类型和检索函数
- SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)
- uoj#228. 基础数据结构练习题(线段树区间开方)
- 【紫书】 Unix ls UVA - 400 模拟
- CodeForces - 583D Once Again... LIS 循环
- Oracle安装部署之linux OS install oracle database安装脚本
- MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参