Windows 10 UWP 21 of N: EntityFramwrok Core and SQLite with UWP

  • 625
  • 0
  • UAP
  • 2021-04-30

Demo how to setup envrionment to integrate EFCore and SQLite with UWP

之前有介紹過如何使用EF7( Aka EntityFramework 7 )和SQLite在UWP上,這次要介紹的是使用EFCore ( EntityFramework Core )。

建立好空白的UWP專案後再project.json打上如下的package以及版本

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.Design": "1.0.1",
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.1",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview3-final",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

目前有測試成功能夠正常使用的nuget package版本如上,會需要將UWP的.Net Core版本調整到5.2.2(但可能會有些小issue出現) 接者再把EntityFrameworkCore的相關package抓下來,需要注意到的是在EFCore的Tools還是使用preview的版本!因為目前EFCore在compile的時候無法正確mapping到UWP的某些componet!所以需要加入文字檔案(text)並且變更檔名成app.config,並且輸入以下config

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.IO.FileSystem.Primitives"
                          publicKeyToken="b03f5f7f11d50a3a"
                          culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0"
                         newVersion="4.0.1.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Overlapped"
                          publicKeyToken="b03f5f7f11d50a3a"
                          culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0"
                         newVersion="4.0.1.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations"
                          publicKeyToken="b03f5f7f11d50a3a"
                          culture="neutral" />
        <bindingRedirect oldVersion="4.1.0.0"
                         newVersion="4.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

這樣就可以在compile的時候正確remapping到正確的componet了!

接者還需要注意一件重要的是.... 使用EF的時候是能夠使用async await的非同步對DB進行CRUD,但是在EFCore + UWP是不建議使用非同步的操作!原因無他就是在UWP配上.net的話會使用.net native的轉譯讓部分management core變成C++的code。

接下來就來看看C#的code的拉~ 先是建立DbInstance的class並輸入以下code

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


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Filename=Blogging.db");
        }

    }

    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 int BlogId { get; set; }
        public Blog Blog { get; set; }

    }

這邊和之前的EF 7的介紹非常相似一樣需要override OnConfiguring的method來進行db的設定

接下來到app.xaml.cs加些code

public App()
{
            this.InitializeComponent();
            this.Suspending += OnSuspending;

            using (var db = new DBInstanceContext())
            {
                db.Database.Migrate();
            }
}

最後在Visual Studio的Tools選單 -> Nuget Package Manager -> Package Manager Console的視窗叫出後並打上一下指令

Add-Migration " DemoMigration "

基本上EFCore和EF7在使用上是非常接近的,效能好差異也可以參考Channel9或是其他網路資源。從EF7 ( EF Core ) 開始所有的EF都將會是使用Code first的開發方式建立db schema所以這點是必經的過程。

Sample code link 可以參考 = > https://github.com/EvaRichie/SQLiteUWP_Sample

 

***以上Code以及說明都有可能隨著Windows 10 的版本以及Visual Studio 2015版本有所調整!***

參考資料 MSDN, Local SQLite on UWP

下次再分享Windows 10 的新技術拉~