新建控控制台程序

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的更多相关文章

  1. MVC5+EF6+MYSQl,使用codeFirst的数据迁移

    之前本人在用MVC4+EF5+MYSQL搭建自己的博客.地址:www.seesharply.com;遇到一个问题,就是采用ef的codefirst模式来编写程序,我们一般会在程序开发初期直接在glob ...

  2. MVC CodeFirst简单的创建数据库(非常详细的步骤)

       最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...

  3. ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存

    ASP.Net MVC4+Memcached+CodeFirst实现分布式缓存 part 1:给我点时间,允许我感慨一下2016年 正好有时间,总结一下最近使用的一些技术,也算是为2016年画上一个完 ...

  4. 使用EF CodeFirst 创建数据库

    EntityFramework 在VS2015添加新建项时,选择数据->ADO.NET 实体数据模型,有一下选项 来自数据库的EF设计器,这个就是我们最常用的EntityFramework设计模 ...

  5. 1.【使用EF Code-First方式和Fluent API来探讨EF中的关系】

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/relationship-in-entity-framework-using-code-firs ...

  6. 2.EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  7. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  8. ASP.NET MVC——CodeFirst开发模式

    Entity Framework框架提供了几种开发模式,比如Database First,Model First,Code First.Database First是最老也是应用得最广泛的一种设计方式 ...

  9. 第三篇:Entity Framework CodeFirst & Model 映射 续篇 EntityFramework Power Tools 工具使用

    上一篇 第二篇:Entity Framework CodeFirst & Model 映射 主要介绍以Fluent API来实作EntityFramework CodeFirst,得到了大家一 ...

  10. EF使用CodeFirst方式生成数据库&技巧经验

    前言 EF已经发布很久了,也有越来越多的人在使用EF.如果你已经能够非常熟练的使用EF的功能,那么就不需要看了.本文意在将自己使用EF的方式记录下来备忘,也是为了给刚刚入门的同学一些指导.看完此文,你 ...

随机推荐

  1. Mac 终端提示You have not agreed to the Xcode license agreements

    在mac 终端运行命令的时候会被提示没有同意xcode 证书 ,这个时候需要在Terminal中同意license 打开终端输入xcodebuild -license,会看到一大堆的license说明 ...

  2. Swift - 将String类型的数字转换成数字类型

    Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = (tf1.text as N ...

  3. Qt之启动外部程序(使用参数很全面,还使用了setProcessChannelMode)

    简述 QProcess可以用来启动外部程序,并与它们交互. 要启动一个进程,通过调用start()来进行,参数包含程序的名称和命令行参数,参数作为一个QStringList的单个字符串. 另外,也可以 ...

  4. CentOS6.4 编译安装Python 3.3.2 - CRPER木木

    基础环境: CentOS6.4(预装GCC,或者联网YUM---GCC编译写进这里太臃肿,找机会另外写一篇) Python 3.3.2     下载链接: http://www.python.org/ ...

  5. rsyslog+LogAnalyzer 日志收集

    Linux 之rsyslog+LogAnalyzer 日志收集系统 一.LogAnalyzer介绍 LogAnalyzer工具提供了一个易于使用,功能强大的前端,用于搜索,查看和分析网络活动数据,包括 ...

  6. 更改windows系统语言,改为英文

  7. Automatic logon configuration on Linux OS

    Automatic logon configuration on Linux OS 1. Regarding to DSA: a) ssh-keygen -t dsa  b) cat ~/.ssh/i ...

  8. C# split字符串 依据1个或多个空格

    实例场景.对于字符串:"AAAA AAA        BBBB BBB BBB        CCCCCCCC". 1. 分隔为 "AAAA AAA" . & ...

  9. 百度GPSutil

    ================================================= package com.qcar.benz.biz.common; import com.aliba ...

  10. RealThinClient学习(一)

    服务端代码: unit RtcHttpServer; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ...