网站首页 > 技术教程 正文
The Entity Framework Core Provider simplifies operations on data in MongoDB clusters by mapping the data to .NET objects.
MongoDB Atlas is a fully-managed cloud database service that hosts your data on MongoDB clusters. In this guide, we show you how to get started with your own free (no credit card required) cluster.
Follow the steps below to connect your Entity Framework Core Provider application to a MongoDB Atlas cluster.
MongoDB.EntityFrameworkCore安装
dotnet add package MongoDB.EntityFrameworkCore --prereleaseBaseDbContext基类
public class BaseDbContext: DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = "mongodb://localhost:27017";
optionsBuilder.UseMongoDB(connectionString, "sample_mflix");
}
}Collection DbContext操作类
internal class MflixDbContext : BaseDbContext
{
public DbSet<Movie> Movies { get; init; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Movie>().ToCollection("movies");
}
}Collection实体类
internal class Movie
{
public ObjectId _id { get; set; }
public string title { get; set; }
public string rated { get; set; }
public string plot { get; set; }
}查询Collection数据
var db = new MflixDbContext();
List<Movie> listMovie = db.Movies.Where(m => m.title == "Pluto").ToList();
Console.WriteLine(listMovie[0].plot);删除Collection数据
var db = new MflixDbContext();
db.Movies.rtRemove(listMovie[0]);
db.SaveChanges();插入Collection数据
db.Movies.Add(new Movie()
{
title = "Pluto",
rated = "ia",
plot = "asdd",
});
db.SaveChanges();批量插入Collection数据
var movies = new[]
{
new Movie()
{
_id = ObjectId.GenerateNewId(),
title = "Pluto1",
rated = "ia1",
plot = "asdd1",
},
new Movie()
{
_id = ObjectId.GenerateNewId(),
title = "Pluto2",
rated = "ia2",
plot = "asdd2",
}
};
db.Movies.AddRange(movies);
db.SaveChanges();更新Collection数据
Movie movie = db.Movies.Where(m => m.title == "Pluto").FirstOrDefault();
movie.title = "更新数据";
db.Movies.Update(movie);
db.SaveChanges();静态运行效果图
参考文档
- https://www.mongodb.com/docs/entity-framework/current/quick-start/
猜你喜欢
- 2024-10-30 Spring Boot中RestTemplate开发实践(2)
- 2024-10-30 浅谈.NET EentityFramework的导航属性
- 2024-10-30 Entity Framework Core-删除数据(怎么删除framework4.0)
- 2024-10-30 EntityFramework Core 2.x/3.x (ef core) 在迁移中自动生成数据库表
- 2024-10-30 Entity Framework Core-使用Fluent API配置一对多关系
- 2024-10-30 .NET 7使用 Entity Framework Core 制作增删改查(CRUD) Web API 教程
- 2024-10-30 今日技术点:使用Entity Framework Core进行数据库迁移
- 2024-10-30 .Net Core下多种ORM框架特性及性能对比
- 2024-10-30 Entity Framework 7 支持批量操作和 JSON 列
- 2024-10-30 Entity Framework Core-Fluent API
欢迎 你 发表评论:
- 10-23Excel计算工龄和年份之差_excel算工龄的公式year
- 10-23Excel YEARFRAC函数:时间的"年份比例尺"详解
- 10-23最常用的10个Excel函数,中文解读,动图演示,易学易用
- 10-23EXCEL中如何计算截止到今日(两个时间中)的时间
- 10-2390%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 10-23计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- 10-23Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 10-23怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- 最近发表
-
- Excel计算工龄和年份之差_excel算工龄的公式year
- Excel YEARFRAC函数:时间的"年份比例尺"详解
- 最常用的10个Excel函数,中文解读,动图演示,易学易用
- EXCEL中如何计算截止到今日(两个时间中)的时间
- 90%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
- 计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
- Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
- 怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
- Excel日期函数之DATEDIF函数_excel函数datedif在哪里
- Excel函数-DATEDIF求司龄_exceldatedif函数计算年龄
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)

本文暂时没有评论,来添加一个吧(●'◡'●)