当我们使用时C#,我们可以使用Code-First方法以强类型方式访问我们的数据库:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Posts { get; set; }
}
...
public class Database : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
var db = new Database()
var blog = new Blog { Name = "My new blog", BlogId = 1 };
db.Blogs.Add(blog);
db.SaveChanges(); // save object to database
Run Code Online (Sandbox Code Playgroud)
编译器将确保我们只访问现有的属性/方法,并且我们在代码中的每个地方都使用正确的类型. …