在模型中包含类型
比如我们现在要增加一个新实体或者表
按照约定,在上下文中的 DbSet 属性中公开的类型作为实体包含在模型中。 还包括在 OnModelCreating 方法中指定的实体类型,就像通过递归方式浏览其他发现的实体类型的导航属性找到的任何类型一样。
在下面的代码示例中,包含了所有类型:
- Blog 包含在内,因为它在上下文的 DbSet 属性中公开。
- 包含 Post 是因为它是通过 Blog.Posts 导航属性发现的。
- AuditEntry,因为它是在 OnModelCreating中指定的。
class MyContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AuditEntry>();
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Blog Blog { get; set; }
}
public class AuditEntry
{
public int AuditEntryId { get; set; }
public string Username { get; set; }
public string Action { get; set; }
}Code language: HTML, XML (xml)
NUGET安装 MySql.Data.EntityFrameworkCore
创建表、
DROP TABLE IF EXISTS `blogs`;
CREATE TABLE `blogs` (
`BlogId` int(11) NOT NULL AUTO_INCREMENT,
`Url` varchar(255) DEFAULT NULL,
`Rating` varchar(255) DEFAULT NULL,
PRIMARY KEY (`BlogId`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
`PostId` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(255) DEFAULT NULL,
`Content` varchar(255) DEFAULT NULL,
`BlogId` int(11) DEFAULT NULL,
PRIMARY KEY (`PostId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;Code language: PHP (php)
执行数据库
class Program
{
static void Main(string[] args)
{
using (var db = new BloggingContext())
{
var blog = new Blog { Url = "http://bamn.cn", Rating = 9 };
db.Blogs.Add(blog);
db.SaveChanges();
var blog = db.Blogs.Include(blog => blog.Posts)
.Where(b => b.Rating > 3)
.OrderBy(b => b.Url)
.first(m=>m.blogid = 1);
foreach(var item in blog.posts){
}
}
}
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL(
@"server=localhost;Database=web;Uid=root;Pwd=123456");
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public 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 Blog Blog { get; set; }
public Author Author { get; set; }
}
public class Author
{
public Photo Photo{ get; set; }
}
public class Photo
{
}Code language: HTML, XML (xml)
就整理 不要改动一个字
从模型中排除类型
如果您不希望在模型中包含某一类型,则可以排除它:
两种方法
标注的方式
[NotMapped]
public class BlogMetadata
{
public DateTime LoadedFromDatabase { get; set; }
}Code language: JavaScript (javascript)
API的方式
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Ignore<BlogMetadata>();
}Code language: HTML, XML (xml)
表名
按照约定,每个实体类型将设置为映射到与公开实体的 DbSet 属性同名的数据库表。 如果给定实体不存在 DbSet,则使用类名称。
可以手动配置表名:
标注的方式
[Table("blogs")]
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}Code language: JavaScript (javascript)
API的方式
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable("blogs");
}Code language: JavaScript (javascript)