使用DbSet的Local属性可以访问当前context中被追踪且没有被标记为删除的实体(内存中的数据)
using (var context = new BloggingContext())
{
// Load all blogs from the database into the context
context.Blogs.Load(); // Add a new blog to the context
context.Blogs.Add(new Blog { Name = "My New Blog" }); // Mark one of the existing blogs as Deleted
context.Blogs.Remove(context.Blogs.Find()); // Loop over the blogs in the context.
Console.WriteLine("In Local: ");
foreach (var blog in context.Blogs.Local)
{
Console.WriteLine(
"Found {0}: {1} with state {2}",
blog.BlogId,
blog.Name,
context.Entry(blog).State);
} // Perform a query against the database.
Console.WriteLine("\nIn DbSet query: ");
foreach (var blog in context.Blogs)
{
Console.WriteLine(
"Found {0}: {1} with state {2}",
blog.BlogId,
blog.Name,
context.Entry(blog).State);
}
}
获取追踪对象的详细信息
using (var context = new BloggingContext())
{
// Load some entities into the context
context.Blogs.Load();
context.Authors.Load();
context.Readers.Load(); // Make some changes
context.Blogs.Find().Title = "The New ADO.NET Blog";
context.Blogs.Remove(context.Blogs.Find());
context.Authors.Add(new Author { Name = "Jane Doe" });
context.Readers.Find().Username = "johndoe1987"; // Look at the state of all entities in the context
Console.WriteLine("All tracked entities: ");
foreach (var entry in context.ChangeTracker.Entries())
{
Console.WriteLine(
"Found entity of type {0} with state {1}",
ObjectContext.GetObjectType(entry.Entity.GetType()).Name,
entry.State);
} // Find modified entities of any type
Console.WriteLine("\nAll modified entities: ");
foreach (var entry in context.ChangeTracker.Entries()
.Where(e => e.State == EntityState.Modified))
{
Console.WriteLine(
"Found entity of type {0} with state {1}",
ObjectContext.GetObjectType(entry.Entity.GetType()).Name,
entry.State);
} // Get some information about just the tracked blogs
Console.WriteLine("\nTracked blogs: ");
foreach (var entry in context.ChangeTracker.Entries<Blog>())
{
Console.WriteLine(
"Found Blog {0}: {1} with original Name {2}",
entry.Entity.BlogId,
entry.Entity.Name,
entry.Property(p => p.Name).OriginalValue);
} // Find all people (author or reader)
Console.WriteLine("\nPeople: ");
//返回所有实现IPerson接口的类的Name属性值
foreach (var entry in context.ChangeTracker.Entries<IPerson>())
{
Console.WriteLine("Found Person {0}", entry.Entity.Name);
}
} public class Author : IPerson
{
public int AuthorId { get; set; }
public string Name { get; set; }
public string Biography { get; set; }
} public class Reader : IPerson
{
public int ReaderId { get; set; }
public string Name { get; set; }
public string Username { get; set; }
} public interface IPerson
{
string Name { get; }
}
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 = ; 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");
}