Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityFramework与SQLite #17

Open
renyh opened this issue Feb 17, 2020 · 7 comments
Open

EntityFramework与SQLite #17

renyh opened this issue Feb 17, 2020 · 7 comments

Comments

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

image

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

image

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

image

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

Step 1: Create a simple appliation

Step 2: 引用Nuget包

Microsoft.EntityFrameworkCore.SQlite,最新版本3.1.1。安装会自动安装其引用的下级包。

Step 3: Defining your Models

Create your models and add a new property for each of your new models (given that you want a table for each!)

    public class Cat
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Step 4: Create a context class for your database.

What’s very interesting here is that the database doesn’t exist, the class SimpleDataStorage tells EF how and where to create it.

public class SimpleDataStorage: DbContext
{
    public SimpleDataStorage(){}
 
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        //Set the filename of the database to be created
        optionsBuilder.UseSqlite("Data Source=db.sqlite");
    }
 
    public DbSet<Cat> Cats { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
 
        //Define the Table(s) and References to be created automatically
        modelBuilder.Entity<Cat>(b =>
        {
            b.HasKey(e => e.Id);
            b.Property(e => e.Id).ValueGeneratedOnAdd();
            b.Property(e => e.Name).IsRequired().HasMaxLength(255);
            b.ToTable("Cats");
        });
    }
}

Step 5: Create the database and tables.

在程序初始化的阶段调用

   public void CreateDb()
    {
        using (var client = new SimpleDataStorage())
        {
            //Create the database file at a path defined in SimpleDataStorage
            client.Database.EnsureCreated();
            //Create the database tables defined in SimpleDataStorage
            client.Database.Migrate();
        }
    }

Step 6: Add Cats to the database!

We can now access the database anywhere in code

public void AddCate(SimpleDataStorage db)
{
    for (int i = 0; i < 1000; i++)
    {
        db.Cats.Add(new Cat() {Name = "Caty McCat: " + i}); 
    }
    db.SaveChanges(true);
}

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

查询

使用语言集成查询 (LINQ) 从数据库检索实体类的实例。 有关详细信息,请参阅查询数据。

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

例2

using (var context = new BloggingContext())
{
    var blogs = context.Blogs
        .Where(b => b.Url.Contains("dotnet"))
        .ToList();
}

加载所有数据

using (var context = new BloggingContext())
{
    var blogs = context.Blogs.ToList();
}

加载单个实体

using (var context = new BloggingContext())
{
    var blog = context.Blogs
        .Single(b => b.BlogId == 1);
}

@renyh
Copy link
Collaborator Author

renyh commented Feb 17, 2020

保存数据

使用实体类的实例在数据库中创建、删除和修改数据。 有关详细信息,请参阅保存数据。

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

@renyh
Copy link
Collaborator Author

renyh commented Feb 18, 2020

删除数据
使用 DbSet.Remove 方法删除实体类的实例。
如果实体已存在于数据库中,则将在“SaveChanges” 期间删除该实体。 如果实体尚未保存到数据库(即跟踪为“已添加”),则在调用SaveChanges时,该实体会从上下文中移除且不再插入。

using (var context = new BloggingContext())
{
    var blog = context.Blogs.First();
    context.Blogs.Remove(blog);
    context.SaveChanges();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant