Codefirst
新建控控制台程序
nuget 输入Install-Package EntityFramework 回车;
Program.cs只 添加
using ConsoleApplication18.Migrations;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication18 //修改你的命名空间
{
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BloggingContext, Configuration>());
using (var db = new BloggingContext())
{
Console.WriteLine("新建一个名字");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog);
db.SaveChanges();
var query = from b in db.Blogs
orderby b.Name
select b;
Console.WriteLine("添加成功!");
foreach (var item in query)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("查询完毕");
Console.ReadKey();
}
}
public class BloggingContext : DbContext
{
public BloggingContext()
: base("name=con")
{ }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<GGXX> GGXX { get; set; }
public DbSet<GGXXBckup> GGXXBckup { get; set; }
public DbSet<GGXXLook> GGXXLook { get; set; }
//public DbSet<User> Users { get; set; }
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public int aa { get; set; }
public int ur { get; set; }
public int MyProperty { get; set; }
public virtual List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; } public int MyProperty2222 { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
}
//public class User
//{
// [Key]
// public string Username { get; set; }
// [Column("dis")]
// public string DisplayName { get; set; }
// public int MyProperty { get; set; }
// public int MyProperty1 { get; set; }
// public int MyPropertya { get; set; } //}
}
}
app.config中加入
<connectionStrings>
<add connectionString="Data Source=.;Initial Catalog=hh;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" name="con" providerName="System.Data.SqlClient"/>
</connectionStrings>
Codefirst的更多相关文章
- MVC5+EF6+MYSQl,使用codeFirst的数据迁移
之前本人在用MVC4+EF5+MYSQL搭建自己的博客.地址:www.seesharply.com;遇到一个问题,就是采用ef的codefirst模式来编写程序,我们一般会在程序开发初期直接在glob ...
- MVC CodeFirst简单的创建数据库(非常详细的步骤)
最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...
- ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存
ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存 part 1:给我点时间,允许我感慨一下2016年 正好有时间,总结一下最近使用的一些技术,也算是为2016年画上一个完 ...
- 使用EF CodeFirst 创建数据库
EntityFramework 在VS2015添加新建项时,选择数据->ADO.NET 实体数据模型,有一下选项 来自数据库的EF设计器,这个就是我们最常用的EntityFramework设计模 ...
- 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...
- 2.EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- 3.EF 6.0 Code-First实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...
- ASP.NET MVC——CodeFirst开发模式
Entity Framework框架提供了几种开发模式,比如Database First,Model First,Code First.Database First是最老也是应用得最广泛的一种设计方式 ...
- 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用
上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...
- EF使用CodeFirst方式生成数据库&技巧经验
前言 EF已经发布很久了,也有越来越多的人在使用EF.如果你已经能够非常熟练的使用EF的功能,那么就不需要看了.本文意在将自己使用EF的方式记录下来备忘,也是为了给刚刚入门的同学一些指导.看完此文,你 ...
随机推荐
- 大豆生物柴油驱动的大巴斯(Bus)
请看下图: 这是大豆生物柴油(Soybean biodiesel)驱动的大巴斯(Bus)的外观,感觉非常有劲. 回想历史,1893年,德国学者Rudolf Diesel(1858-1913)发明了一种 ...
- Hive HA使用说明
hive让大数据飞了起来,不再需要专人写MR.平常我们都可以用基于thrift的任意语言来调用hive. 不过爱恨各半,hive的thrift不稳定也是出了名的.很容易就出问题,让人无计可施.唯一的办 ...
- Problem D: Flip Five
大致题意:3 * 3的黑白格,在翻转的时候会本身和四周的都翻转,问最小翻转几次变成全部是白色解题思路:把3 * 3 = 9 个格子进行全排列,然后穷举然后找翻转的最小次数 #include <i ...
- javascript (十二)对象二
JavaScript 中的所有事物都是对象:字符串.数字.数组.日期,等等. 在 JavaScript 中,对象是拥有属性和方法的数据. 属性和方法 属性是与对象相关的值. 方法是能够在对象上执行的动 ...
- 14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes
14.2.5.6 Adaptive Hash Indexes 自适应Hash Indexes adaptive hash index(AHI) 让InnoDB 执行更加像在一个内存数据库里在, 在不牺 ...
- [读书笔记]黑客与画家[Hackers.and.Painters]
(书生注:这本书写的不错.针对程序员,可以带来不同角度的想法,有助于反思自己的程序员工作.我甚至从中发现了自己爱用铅笔的原因... 尤其是其中关于黑客的定义,包括黑客认为的乐趣和目的,让人更深层次思 ...
- Java设计模式之适配器模式(Adapter Pattern)
Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...
- APPCAN学习笔记003---原生开发与HTML5技术
APPCAN学习笔记003---原生开发与HTML5技术 技术qq交流群:JavaDream:251572072 1.HTML5的优势: HTML5强悍牢固的骨架 CSS3精致到每一个毛孔的皮 ...
- iOS高效开源类库
因为iOS SDK相对比较底层,所以开发者就得受累多做一些体力活.不过幸运的是,有很多第三方的类库可以用来简化很多不必要的工作.笔者整理了一下在本人学习过程中用到的一些比较有用Objective-C开 ...
- 2014年Windows平台软件推荐:神器小工具(骨灰级
原文 http://www.wtoutiao.com/a/120621.html 底层工具 “If you know how to use Process Monitor competently, ...