zoukankan      html  css  js  c++  java
  • ef入门

    1,用nuget引用entityframework

    2,添加配置文件:

    <connectionStrings>
    <add name="modelcontext" connectionString="Data Source=panqingqiang-PCSQL2008;Initial Catalog=QiangGe2014;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>

    3,设计实体:

    public class Book
    {
    public int BookID { get; set; }
    public string BookName { get; set; }
    public string Author { get; set; }
    public string Publisher { get; set; }
    public decimal Price { get; set; }
    public string Remark { get; set; }
    }

    4,设计访问数据库:

    public class AppDbContext:DbContext
    {
    public AppDbContext():base("name=modelcontext"){}

    public DbSet<Book> Books { get; set; }

    }

    5,下面来做一个测试:

    main函数:

    static void Main(string[] args)
    {
    Book book = new Book()
    {
    BookName = "C#高级编程333",
    Price = 151.8M,
    Publisher = "清华大学出版社",
    Author = "Wrox",
    };

    AppDbContext dbContext = new AppDbContext();
    dbContext.Books.Add(book);
    int result=dbContext.SaveChanges();
    Console.WriteLine(result);

    var booksQuery = from b in dbContext.Books select b;
    List<Book> booksList = booksQuery.ToList();
    Console.WriteLine(booksList.Count());


    }

  • 相关阅读:
    招聘.NET开发人员
    SQL 2005 SSIS 导入数据效率问题
    用户控件使用事件与调用页面交互
    使用sql语句删除标识列属性
    poj1520
    poj1476
    poj1363
    poj1477
    poj1312
    大端法小端法与union
  • 原文地址:https://www.cnblogs.com/panqingqiang/p/4587875.html
Copyright © 2011-2022 走看看