zoukankan      html  css  js  c++  java
  • .net framework , code first

    1. 创建一个控制台应用程序, 并添加引用 

    2 创建 一个类

    public class New
        {
            [Key]
            public string NewId { get; set; }
            public string Title { get; set; }
        }

    3 创建一个类继承 DbContext

     public class NewContext : DbContext
        {
            public NewContext()
            {
                System.Diagnostics.Debug.Write(Database.Connection.ConnectionString);
            }
            public DbSet<New> News { get; set; }
        }

    4 config 文件配置 ConnectionString

    <add name="NewContext" connectionString="Data Source=.;Initial Catalog=CodeFirstTest;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />

    5 在Main 方法中调用

     using (var db = new NewContext())
                    {
                        Console.Write("start");
                        Console.OutputEncoding = Encoding.GetEncoding(936);
    
                        var newModel = new New { NewId = Guid.NewGuid().ToString(), Title = "sdfdsfdsfdsf" };
                        db.News.Add(newModel);
                        db.SaveChanges();
                        foreach (var n in db.News)
                        {
                            Console.WriteLine(n.Title);
                        }
                        Console.Write("success");
                        Console.ReadKey();
                    }
  • 相关阅读:
    7. 流程控制
    6. 工作区域与包
    5. Go函数
    4. Go常量
    面试题1
    数据库三范式
    触发器和存储过程
    面试题
    js 程序执行与顺序实现详解 ,来自网上
    基础的优化数据库查询,个人笔记
  • 原文地址:https://www.cnblogs.com/zxhome/p/10342988.html
Copyright © 2011-2022 走看看