zoukankan      html  css  js  c++  java
  • 使用Code First 创建数据库

    这是一个控制台程序,作用是通过Code First创建数据库.

    1.添加EntityFrameWork的引用.

    2.添加类 CodeFirstTest1.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data.Entity;
    
    namespace ConsoleApplication1
    {
      public   class CodeFirstTest1
        {
            public class Blog
            {
                public int blogID { get; set; }
                public string title { get; set; }
                public string content { get; set; }
                public int columnID { get; set; }
                public virtual Column column { get; set; }
            }
            public class Column
            {
                public int columnID { get; set; }
                public int columnname { get; set; }
                public virtual ICollection<Blog> blogs { get; set; }
            }
    
            public class BlogContext : DbContext
            {
                public BlogContext()
                    : base("CodeFirstTest")
                {
                }
                public DbSet<Blog> blogs { get; set; }
                public DbSet<Column> columns { get; set; }
            }
            public class TestCodeFirst
            {
                public void Test()
                {
                    using (var db = new BlogContext())
                    {
                        //向数据库添加记录
                        var column = new Column() { columnID = 1, columnname = 1 };
                        db.columns.Add(column);
                        var blog = new Blog()
                        {
                            blogID = 1,
                            title = "abc",
                            content = "test content",
                            columnID = 1
                        };
                        db.blogs.Add(blog);
    
                        //保存记录,返回受影响的行数
                        int recordsAffected = db.SaveChanges();
                        Console.WriteLine("追加{0}记录成功", recordsAffected);
                    }
                }
            }
        }
    }

    3.在主函数里面调用TestCodeFirst

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConsoleApplication1.CodeFirstTest1.TestCodeFirst test = new ConsoleApplication1.CodeFirstTest1.TestCodeFirst();
    
                test.Test();
            }
        }
    }

    4.改一下配置文件里面的连接字符串,指定名称.

      <connectionStrings>
        <add name="CodeFirstTest" connectionString="data source=.;initial catalog=CodeFirstTest;integrated security=True;"
     providerName="System.Data.SqlClient"/>
      </connectionStrings>

    5.调试

    OK,这就生成了名称为CodeFirstTest的数据库了.

  • 相关阅读:
    L317 电子烟
    L316 波音737Max 危机
    2.19.3月 专业综合错题
    L314 单音节词读音规则(二)-元音字母发音规则
    L313 珊瑚裸鼠灭绝
    L312 难看懂的
    Pycharm写代码中文输入法不跟随
    Windows下Python多版本共存
    Python之批处理字符串(打开文件)
    Python Url请求代码
  • 原文地址:https://www.cnblogs.com/miaoying/p/4999316.html
Copyright © 2011-2022 走看看