Forum Xamarin.Forms

Big problem about use EF Core in Xamarin.forms

ClawRageClawRage Member ✭✭
edited June 2019 in Xamarin.Forms

Hi,
I'm trying use Entity Framework Core to connect Sqlite database,
because it is seem to be able make it easier to update database's model future.
but bow,I even can't run first Migrations...

I added five NuGets about EF core now:

Microsoft.EntityFrameworkCore(2.2.4)
Microsoft.EntityFrameworkCore.Sqlite(2.2.4)
Microsoft.EntityFrameworkCore.Tools(2.2.4)
Microsoft.EntityFrameworkCore.Design(2.2.4)
sqlite-net-pcl(1.5.231)

with the database like:

using Microsoft.EntityFrameworkCore;
using System;
using System.IO;
public class DatabaseContext : DbContext
{
    protected string _databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Test_DataContext.db3");
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite($"Filename={_databasePath}");
    }

    public DbSet<DataM> A_Data { get; set; }

}

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class DataF
{
    public readonly DatabaseContext _db = new DatabaseContext();
    public  Task<List<DataM>> GetData_All_List()
    {
     return _db.A_Data.ToListAsync();
    }
}

it seems right,but when I try to Add Migration,

PM> Add-Migration Initial 

it's respond "無法辨識 'Add-Migration' 詞彙是否為 Cmdlet、函數、指令檔或可執行程式的名稱。"
=> can't Identification 'Add-Migration' is Cmdlet or function or.......'s name.

On the other hand,I build a new project just has a <.NET core>'s ClassLibrary to test,without Xamarin.
just write some database code like my sample,it is work to add migration ,
but next step is fail.......

PM> Update-Database

is respond

"Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE "DataTable" (
    "ID" INTEGER NOT NULL CONSTRAINT "PK_DataTable" PRIMARY KEY AUTOINCREMENT,
    "CreatTime" TEXT NOT NULL,
    "UpdateTime" TEXT NOT NULL
);"

+
"SQLite Error 1: 'table "DataTable" already exists'".

By the way,
why EF Core always very slow when run the App's first times open SQL

Sign In or Register to comment.