zoukankan      html  css  js  c++  java
  • Entity Framework 6

    参考官方文档中入门找到对应的实例建立第一个EFCodeFirst实例

     1:建立CodeFirstNewDatabaseSample控制台应用程序

     2:获取实体框架,添加EF 参看

    https://docs.microsoft.com/zh-cn/ef/ef6/fundamentals/install

    3:添加以下代码



    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Text;

    namespace CodeFirstNewDatabaseSample
    {
        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();
                }
            }


        }

        public class Blog
        {
            public int BlogId { get; set; }
            public string Name { get; set; }

            public virtual List<Post> Posts { get; set; }
        }

        public class Post
        {
            public int PostId { 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 BloggingContext:DbContext
        {
            public BloggingContext() : base("BlogContext") { }

            public DbSet<Blog> Blogs { get; set; }

            public DbSet<Post> Posts { get; set; }

        }
    }

    这个是配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>

      <connectionStrings>
        <add name="BlogContext"
              providerName="System.Data.SqlClient"
              connectionString="Server=.;Database=Blogging;Integrated Security=True;"/>
      </connectionStrings>


     
    </configuration>
     

     代码分享如下

    链接:https://pan.baidu.com/s/1lEZeprJB4y3_APKrIYD1oA
    提取码:bvbo
    复制这段内容后打开百度网盘手机App,操作更方便哦
     
  • 相关阅读:
    python 获取在线视频时长,不下载视频
    python treeview 多线程下表格插入速度慢解决方法
    c#操作magick,magick.net
    油猴脚本-Tampermonkey-淘宝dsr过滤器(过滤非3红商品)
    python 基础小坑 0==False is True
    pyd 编译,简单命令cythonize
    python 调用Tesseract,dll模式,无需安装,绿色版
    list与set的查询效率,大量数据查询匹配,必须选set
    selenium 页面加载慢,超时的解决方案
    selenium 不打印chromedriver的日志信息
  • 原文地址:https://www.cnblogs.com/seawh411/p/9882188.html
Copyright © 2011-2022 走看看