码迷,mamicode.com
首页 > 其他好文 > 详细

EFCore

时间:2020-04-01 16:37:02      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:资料   override   core   protected   serve   prot   contex   sam   config   

 

 

1.NuGet安装 entity-framework-core

 

2.创建DB上下文和Model类。

 

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace Intro
{
    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Server=(localdb)\mssqllocaldb;Database=Blogging;Integrated Security=True");
        }
    }

    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; }
    }
}

 

查询

using (var db = new BloggingContext())
{
    var blogs = db.Blogs
        .Where(b => b.Rating > 3)
        .OrderBy(b => b.Url)
        .ToList();
}

 

保存数据

 

using (var db = new BloggingContext())
{
    var blog = new Blog { Url = "http://sample.com" };
    db.Blogs.Add(blog);
    db.SaveChanges();
}

 

 

 

 

参考资料:https://docs.microsoft.com/en-us/ef/core/

EFCore

标签:资料   override   core   protected   serve   prot   contex   sam   config   

原文地址:https://www.cnblogs.com/JinweiChang/p/12613206.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!