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>

 

posted @ 2015-02-06 13:44  crazyair  阅读(176)  评论(0编辑  收藏  举报