小新新2015

导航

 

1,用nuget引用entityframework

2,添加配置文件:

<connectionStrings>
<add name="modelcontext" connectionString="Data Source=panqingqiang-PC\SQL2008;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());


}

 

posted on 2015-06-19 10:32  小新新2015  阅读(166)  评论(0编辑  收藏  举报