标签:sele ges ons 一个 border not foreach class start
1.使用Linq查询
- using (var db = new BloggingContext())
- {
- #region 使用linq查询
- 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();
- 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();
- #endregion
2. 使用sql语句查询
2.1 执行存储过程
- List<Post> Posts = db.Database.SqlQuery<Post>("TestProcedure @personId", new SqlParameter("@personId", "1000")).ToList();
- foreach (var item in Posts)
- {
- Console.WriteLine("{0} {1} {2} {3}", item.BlogId, item.Content, item.PostId, item.Title);
- }
2.2 执行普通sql语句 查询
- var blogs = db.Database.SqlQuery<Blog>("select * from Blogs where Name = @name", new SqlParameter("name", "gjp"));
- foreach (var item in blogs)
- {
- Console.WriteLine("ID:{0},Name:{1}", item.BlogId, item.Name);
- }
- Console.ReadKey();
2.3 使用sql语言更新,删除 ,增加操作可以使用ExecuteSqlCommand方法
- var items = db.Blogs.ToList();
- int id;
- foreach (var item in items)
- {
- id = item.BlogId;
- break;
- }
- Console.WriteLine("执行sql,更改");
- int count = db.Database.ExecuteSqlCommand("update Blogs set Name = @content where BlogId = @postid", new SqlParameter[] { new SqlParameter("@content", "shjgshgujsjgjkagj"), new SqlParameter("@postid", 1) });
- if (count == 1)
- {
- db.SaveChanges();
- Console.WriteLine("Sql执行成功");
- foreach (var item in db.Blogs)
- {
- Console.WriteLine("ID:{0},Name:{1}", item.BlogId, item.Name);
- }
- }
- Console.WriteLine("是否继续?yes/no。将值改回来");
- if (Console.ReadLine() == "yes")
- {
- int count1 = db.Database.ExecuteSqlCommand("update Blogs set Name = @content where BlogId = @postid", new SqlParameter[] { new SqlParameter("@content", "gjp"), new SqlParameter("@postid", 1) });
- if (count1 == 1)
- {
- db.SaveChanges();
- Console.WriteLine("sql执行成功");
- foreach (var item in db.Blogs)
- {
- Console.WriteLine("ID:{0},Name:{1}", item.BlogId, item.Name);
- }
- }
- Console.WriteLine("按任意键继续");
- Console.ReadKey();
- }
3 code first 控制数据库
1)会在没有数据库时创建一个,这是默认行为
- Database.SetInitializer( new CreateDatabaseIfNotExists<BreakAwayContext>());
2)模型改变时,自动重新创建一个新的数据库
- Database.SetInitializer( new DropCreateDatabaseIfModelChanges<BreakAwayContext>());
3)每次运行时都重新生成数据库
- Database.SetInitializer(new DropCreateDatabaseAlways<BreakAwayContext>());
3.code first创建
1)首先打开“程序保管理器控制台”
2)输入"Enable-Migrations",用于启用项目“code frist”迁移
3)然后创建Model,Context等
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 DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
4)此时执行“控制台程序”就行了,ef已帮你自动创建好数据库了
4 把Code First应用于已经存在的数据库
1)创建“Ado.net实体数据模型”
2.选择来自“数据库的code frist”
3)设置“连接属性”
4)然后就生成好了,大功告成
EF code frist 使用Linq或者Sql执行
标签:sele ges ons 一个 border not foreach class start
原文地址:https://www.cnblogs.com/tianwen9579/p/10062487.html