使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据)
1 using (var context = new BloggingContext()) 2 { 3 // Load all blogs from the database into the context 4 context.Blogs.Load(); 5 6 // Add a new blog to the context 7 context.Blogs.Add(new Blog { Name = "My New Blog" }); 8 9 // Mark one of the existing blogs as Deleted 10 context.Blogs.Remove(context.Blogs.Find(1)); 11 12 // Loop over the blogs in the context. 13 Console.WriteLine("In Local: "); 14 foreach (var blog in context.Blogs.Local) 15 { 16 Console.WriteLine( 17 "Found {0}: {1} with state {2}", 18 blog.BlogId, 19 blog.Name, 20 context.Entry(blog).State); 21 } 22 23 // Perform a query against the database. 24 Console.WriteLine("\nIn DbSet query: "); 25 foreach (var blog in context.Blogs) 26 { 27 Console.WriteLine( 28 "Found {0}: {1} with state {2}", 29 blog.BlogId, 30 blog.Name, 31 context.Entry(blog).State); 32 } 33 }
获取追踪对象的详细信息
1 using (var context = new BloggingContext()) 2 { 3 // Load some entities into the context 4 context.Blogs.Load(); 5 context.Authors.Load(); 6 context.Readers.Load(); 7 8 // Make some changes 9 context.Blogs.Find(1).Title = "The New ADO.NET Blog"; 10 context.Blogs.Remove(context.Blogs.Find(2)); 11 context.Authors.Add(new Author { Name = "Jane Doe" }); 12 context.Readers.Find(1).Username = "johndoe1987"; 13 14 // Look at the state of all entities in the context 15 Console.WriteLine("All tracked entities: "); 16 foreach (var entry in context.ChangeTracker.Entries()) 17 { 18 Console.WriteLine( 19 "Found entity of type {0} with state {1}", 20 ObjectContext.GetObjectType(entry.Entity.GetType()).Name, 21 entry.State); 22 } 23 24 // Find modified entities of any type 25 Console.WriteLine("\nAll modified entities: "); 26 foreach (var entry in context.ChangeTracker.Entries() 27 .Where(e => e.State == EntityState.Modified)) 28 { 29 Console.WriteLine( 30 "Found entity of type {0} with state {1}", 31 ObjectContext.GetObjectType(entry.Entity.GetType()).Name, 32 entry.State); 33 } 34 35 // Get some information about just the tracked blogs 36 Console.WriteLine("\nTracked blogs: "); 37 foreach (var entry in context.ChangeTracker.Entries<Blog>()) 38 { 39 Console.WriteLine( 40 "Found Blog {0}: {1} with original Name {2}", 41 entry.Entity.BlogId, 42 entry.Entity.Name, 43 entry.Property(p => p.Name).OriginalValue); 44 } 45 46 // Find all people (author or reader) 47 Console.WriteLine("\nPeople: "); 48 //返回所有实现IPerson接口的类的Name属性值 49 foreach (var entry in context.ChangeTracker.Entries<IPerson>()) 50 { 51 Console.WriteLine("Found Person {0}", entry.Entity.Name); 52 } 53 } 54 55 56 public class Author : IPerson 57 { 58 public int AuthorId { get; set; } 59 public string Name { get; set; } 60 public string Biography { get; set; } 61 } 62 63 public class Reader : IPerson 64 { 65 public int ReaderId { get; set; } 66 public string Name { get; set; } 67 public string Username { get; set; } 68 } 69 70 public interface IPerson 71 { 72 string Name { get; } 73 }
EF中执行原生的sql语句
1.在指定实体上进行查询
using (var context = new BloggingContext()) { var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); }
2.执行存储过程
using (var context = new BloggingContext()) { var blogs = context.Blogs.SqlQuery("dbo.GetBlogs").ToList(); } //带参数的存储过程 using (var context = new BloggingContext()) { var blogId = 1; var blogs = context.Blogs.SqlQuery("dbo.GetBlogById @p0", blogId).Single(); }
3.执行sql命令
using (var context = new BloggingContext()) { var blogNames = context.Database.SqlQuery<string>( "SELECT Name FROM dbo.Blogs").ToList(); } using (var context = new BloggingContext()) { context.Database.SqlCommand( "UPDATE dbo.Blogs SET Name = ‘Another Name‘ WHERE BlogId = 1"); }
时间: 2024-10-10 18:30:07