带有没有App.config的实体框架的SQLite

时间:2020-07-02 07:49:54

标签: sqlite entity-framework-6 android-sqlite

我正在尝试创建一个基本应用程序,该应用程序无需app.config文件即可连接到SQLite数据库。 我发现的只是点点滴滴的代码,没有完整的示例。

我有一个包含InventoryItems(ID,位置,数量)的数据库

我收到以下错误: System.ArgumentException:'无法找到请求的.Net Framework数据提供程序。可能未安装。'“

我创建了数据库上下文,我的代码如下:

enter code here


    public class SQLiteDbContext : DbContext
    {
        public DbSet<InventoryItem> InventoryItems { get; set; }

        public SQLiteDbContext() : base(GetSQLiteConnection(), false)
        { }

        protected static DbConnection GetSQLiteConnection()
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SQLite.EF6");
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = "C:\SQLite\Inventory.db";
            return connection;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Configurations.Add(new InventoryItemMap());
            base.OnModelCreating(modelBuilder);
        }
    }
public class InventoryItemMap : EntityTypeConfiguration<InventoryItem>
    {
        public InventoryItemMap()
        {
            ToTable("InventoryItem");
            Property(p => p.Id).IsRequired();
            Property(p => p.Name).IsRequired();
            Property(p => p.Quantity).IsRequired();
            Property(p => p.Storage).IsOptional();
            Property(p => p.Location).IsOptional();
        }
    }

public class InventoryItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Quantity { get; set; }
        public string Storage { get; set; }
        public string Location { get; set; }
        public string Container { get; set; }
    }

0 个答案:

没有答案