简体   繁体   English

带有Android c#Cant的Entity Framework Core无法找到MigrateAsync

[英]Entity Framework Core with Android c# Cant Find MigrateAsync

I am trying to create an entity framework example using a class library in .net c# xamrain for android but I am having an issue with an example I am following. 我正在尝试使用.net c#xamrain for android中的类库创建实体框架示例,但是我所关注的示例存在问题。

 var dbFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
 var fileName = "dbappoitnemtns.db";
 var dbFullPath = Path.Combine(dbFolder, fileName);
 try
   {
      using (var db = new BookingContext(dbFullPath))
      {
         await db.Database.MigrateAsync(); //We need to ensure the latest Migration was added. This is different than EnsureDatabaseCreated.

       Appointments _appTestDate1 = new Appointments() { Subject="Test Appointment           
     1",StarDate=DateTime.Now,EndDate=DateTime.Now.AddHours(2),isActive=true };                 

      }
      catch (Exception ex)
     {
           System.Diagnostics.Debug.WriteLine(ex.ToString());
      }

The issue I am having is that it cant find MigrateAsync() My Context is as follows 我遇到的问题是它找不到MigrateAsync()我的上下文如下

using BookingDataAccess.Models;
using Microsoft.EntityFrameworkCore;

namespace BookDataContext
{
   public class BookingContext : DbContext
{
    public DbSet<Appointments> Appointment { get; set; }

    private string DatabasePath { get; set; }

    public BookingContext()
    {

    }

    public BookingContext(string databasePath)
    {
        DatabasePath = databasePath;
    }

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

And my class is as follows I do not no what I missing as to why it cannot find MirgrateAsync I am using the following packages. 我的课如下,我不遗漏我为什么找不到MirgrateAsync我正在使用以下软件包。

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore">
      <Version>2.0.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core">
      <Version>2.0.2</Version>
    </PackageReference>
    <PackageReference Include="Xamarin.Android.Support.v7.AppCompat" Version="25.*" />
  </ItemGroup>

This is my source code to see if someone can see anything that I could be missing in terms of a reference I am using the latest versions of ef core and sql lite. 这是我的源代码,以查看是否有人在使用最新版本的ef core和sql lite进行参考时发现我可能缺少的任何内容。

Also where is best practise to place the database creation code can this be done in the context I am following this example which placed it in the main activity some how I do not believe that this is the best place for it. 同样,可以在上下文中完成放置数据库创建代码的最佳实践的位置,我正在跟随这个示例,该示例将其放置在主要活动中,但我不相信这是放置它的最佳位置。

I am trying to follow this example which is a year old also what is the best way to get my migrations in. https://blog.xamarin.com/building-android-apps-with-entity-framework/ 我正在尝试遵循这个已经一岁的示例,这也是进行迁移的最佳方法。https://blog.xamarin.com/building-android-apps-with-entity-framework/

This is my full source if anyone could be able to tell me whats wrong also does sql express 2016 have an offline sync mode yet ?. 如果有人可以告诉我出什么问题了,sql express 2016是否还具有脱机同步模式,这是我的完整信息。 I persume a web service is the best way to go. 我认为Web服务是最好的选择。

https://github.com/davidbuckleyni/andriodapp https://github.com/davidbuckleyni/andriodapp

The issue I am having is that it cant find MigrateAsync() My Context is as follows 我遇到的问题是它找不到MigrateAsync()我的上下文如下

The method Migrate and MigrateAsync exists in an extension class called RelationalDatabaseFacadeExtensions , which included in Microsoft.EntityFrameworkCore . MigrateMigrateAsync方法存在于名为RelationalDatabaseFacadeExtensions的扩展类中,该扩展类包含在Microsoft.EntityFrameworkCore

So, you just need to import this namespace: 因此,您只需要导入以下名称空间:

using Microsoft.EntityFrameworkCore;

namespace BookingApp{
    ...
}

Then you can use it. 然后就可以使用它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM