I use in SQLite database in UWP. It has a few tables and is located in Assets folder and has build action marked as "Content".
Under development machine it works fine.
But after deployment to MS Windows 10 tablet it has the error
SQLite error 1 no such table Companies
It corresponds to the code
using (MobileContext db = new MobileContext())
{
companiesList.ItemsSource = db.Companies.ToList();
...
}
var createdResult = Database.EnsureCreated(); // It gives false so
I assume based on https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.infrastructure.databasefacade.ensurecreated?view=efcore-2.1 that database exists.
I have tried
optionsBuilder.UseSqlite("Data Source=" + ApplicationData.Current.LocalFolder.Path + #"\Mobile.db");
optionsBuilder.UseSqlite("Data Source=Mobile.db");
Any clue folk? Thanks!
P.S. I use Microsoft.EntityFrameworkCore to accees SQLite.
P.S. #2 I have tried this solution https://social.msdn.microsoft.com/Forums/en-US/1a933b13-09ee-46ec-9045-e2f567b6048c/uwp-sqlite-error-1-no-such-table-name-table?forum=wpdevelop but it does not work.
Derive from official document,
Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. They typically do not contain sensitive information, and do not need to be changed as an application is deployed. As such, these connection strings are usually fine to be left in code, as shown below. If you wish to move them out of code then UWP supports the concept of settings
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=blogging.db");
}
}
And the default path of db file is application LocalFolder. It looks like this C:\Users\vxx\AppData\Local\Packages\e045f456-27d9-4966-b639-01e2281b249f_7jxxxxxxxxxx\LocalState. If your configuration is same as above, when deploy in new machine, the content of db file is empty.
OP Update
I just commented some decorations of the class like [Table("Companies")] and [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] and it works now!
Related
what is the proper way to create an EF Core database on an Android/iOS device with Xamarin.Forms and EntityFrameworkCore?
I've come across the EnsureCreated vs Migrate thing and since I'm planning on changing the database structure in the future, I'd like to be able to apply those migrations to an existing database, thus I've chosen the Migrate approach.
However, when I call the Migrate method, it doesn't seem to create a database. I've managed to make it work in case I copy the pre-generated database file on the devices beforehand, but that's not what I want. Is there a way to tell EF Core to check if there's an existing database, create a new database if not, and then apply pending migrations to it?
Here's what I've done so far:
I've created a .net standard class library "Data" that will hold all my database classes (context, models, and migrations).
I've changed the TargetFramework of the "Data" project from <TargetFramework>netstandard2.0</TargetFramework> to <TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks> so I could run PMC commands on it.
I've installed these nuget packages to the "Data" project:
EntityFrameworkCore
EntityFrameworkCore.Sqlite
EntityFrameworkCore.Tools
EntityFrameworkCore.Design
I've created a database context class. I've made two constructors for it. The default one will only be used by the PMC commands for generating migrations. The other one will be used in production.
public class MyTestContext : DbContext
{
private string _databasePath;
public DbSet<Issue> Issues { get; set; }
[Obsolete("Don't use this for production. This is only for creating migrations.")]
public MyTestContext() : this("nothing.db")
{
}
public MyTestContext(string databasePath)
{
_databasePath = databasePath;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//I've also tried Filename={_databasePath} here, didn't work either
optionsBuilder.UseSqlite($"Data Source={_databasePath}");
}
}
I've created a test model class, just to have something to play with
public class Issue
{
public string Id { get; set; }
}
I created an inital migration using this command, which completed successfully:
Add-Migration Migration001 -Context MyTestContext -Output Migrations
I added a call to the Migrate method in the App.xaml.cs of my Xamarin.Forms project to test if it works.
public partial class App : Application{
//... other code
protected override async void OnStart()
{
base.OnStart();
using (var db = new MyTestContext(_dbPath))
{
try
{
db.Database.Migrate();
db.Issues.Add(new Issue
{
Id = "TestIssueId"
});
db.SaveChanges();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
//... other code
}
The _dbPath variable contains this (on Android where I'm testing it):
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "test.db");
After all this, I'm getting this exception:
Microsoft.Data.Sqlite.SqliteException: SQLite Error 1: 'no such table: Issues'.
I'm working on multitenant application (ASP.NET 5 + EF7). Each tenant will have separate database. I will have one separate database for tenant account data. I have registered service for EF in startup class for this separate database. I have problem with migrations. I cant create EF migration, until tenantDbContext is registered as service with specific connection string. But this conection string must be dynamic for each tenant... Any idea please? What is the best option to manage DbContexts for tenants?
Future edit - protected override void OnConfiguring was the key how to do: Is this good solution please?
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<TenantDbContext>();
public class TenantDbContext : IdentityDbContext<ApplicationUser>
{
public TenantDbContext() //development database with no connectionString in constructor
{
this._connectionString = "Connection String";
}
public TenantDbContext(string ConnectionString)
{
this._connectionString = ConnectionString;
}
private string _connectionString { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
...etc
As I mentioned in comments I have not tried multi-tenant/multi-db myself but try the following:
You can use DbContext CreateIfNotExists() method. https://msdn.microsoft.com/en-us/library/system.data.entity.database.createifnotexists(v=vs.113).aspx
If you have a Migrations/Configuration.cs you can set AutomaticMigrationsEnabled property to false
Setting the initializer off is probably needed as well: Database.SetInitializer<DatabaseContext>(null);
Sorry without knowing more details like workflow of creating a new tenant (automatic from DB or is a screen filled out with the connection string and name etc.) I can't make more detailed suggestions. I would suggest that your data layer be quite abstracted from the context. It seems like a bad idea for developers to have to select the correct context. Hence the use of a factory.
An option is always requiring a tenant id to be passed into all service or repository methods. I'm guessing this would be in some kind of user claim available in the controller.
Like the title says, I am trying to connect a new MVC 5 app created by Visual Studio 2013 to an existing database table created by and Azure Mobile Service.
I realized that the issue was with the schema of the tables that were created by the mobile service. Instead of using the default "dbo" schema, the tables used <mobile_service_name> as the schema. My MVC project was looking for dbo.<Table_Name> instead of <mobile_service_name>.<Table_Name> and was throwing an error as a result. To fix this, you need to add some mappings in you DB Context class to tell it where exactly to find the tables it is looking for. These mappings are done in an overridden method called OnModelCreating. It ends up looking like this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
public System.Data.Entity.DbSet<MyObject> MyObjects { get; set; }
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyObject>().ToTable("<mobile_service_name>.<Table_Name>");
}
}
I am building a Windows 8 application using sql-net and mvvmcross for data access to a sqlite database. This would be applicable to any Win-8 or Win-Phone app.
I need to install an existing sqlite file on app start.
When using the connection you use syntax such as this
public FlashCardManager(ISQLiteConnectionFactory factory, IMvxMessenger messenger)
{
_messenger = messenger;
_connection = factory.Create("Dictionary.sqlite");
_connection.CreateTable<FlashCardSet>();
_connection.CreateTable<FlashCard>();
}
public void CreateCard(FlashCard flashCard)
{
_connection.Insert(flashCard);
}
That connection creates a file in: C:\Users\USER\AppData\Local\Packages\793fd702-171e-474f-ab3b-d9067c58709b_ka9b83fa3fse2\LocalState
My application uses an existing sqlite database file that I have created. I need to place it in this folder when the application is installed. How would I go about doing this?
Thanks,
JH
Make sure you have the database file you want your app to start off with in one of your apps folders (as in the folders visible in visual studios solution explorer). For this example I'll call this folder "Assets"
All you need to do then is copy this file to the LocalState folder the first time your app runs. This can be done in App.xaml.cs
private async void InitializeAppEnvironment()
{
try
{
if (!(await AppHelper.ExistsInStorageFolder(AppHelper.localFolder, dbName)))
{
StorageFile defaultDb = await AppHelper.installedLocation.GetFileAsync("Assets\\" + dbName);
await defaultDb.CopyAsync(AppHelper.localFolder);
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e);
}
}
I made an AppHelper class to simplify accessing the app data folders, here's the parts I used above:
static class AppHelper
{
public static StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
public static StorageFolder localFolder = ApplicationData.Current.LocalFolder;
public static async Task<bool> ExistsInStorageFolder(this StorageFolder folder, string fileName)
{
try
{
await folder.GetFileAsync(fileName);
return true;
}
catch (FileNotFoundException)
{
return false;
}
}
}
For a more detailed response on MVVM cross I found the current discussion about cross platform file placement in this discussion: Link
The current thought is that you have to inject platform specific code for this sort of functionality.
We have an app that uses NHibernate/FluentNHibernate with a MsSqlConfiguration.MsSql2008.ConnectionString pointed at our SQL environment. The SQL servers have several database and we can connect to different databases by using a convention like so:
public class FactseDatabaseConvention : IClassConvention
{
public void Apply(IClassInstance instance)
{
if (instance.EntityType.Namespace.EndsWith("Model.OtherEntities"))
{
instance.Schema("OtherDatabase.dbo");
}
}
}
This works and the correct queries are generated to access the OtherDatabase. The problem comes in when we want to test using a SQLiteConfiguration.Standard.InMemory() with our SessionFactory. The Persistence tests fail when SQLite is preparing:
System.Data.SQLite.SQLiteException : SQL logic error or missing database
unknown database OtherDatabase
This is the command it generates:
create table OtherDatabse.dbo_my_other_entity_table (
Id UNIQUEIDENTIFIER not null,
... other properties
)
Is there a way I can change my SQLiteConfiguration to have it create 2 in-memory databases and, if so, how? Or should I just create a separate Session for testing these other entities?
I had this very same problem - (no longer since we moved from Sqlite to Sql Server LocalDB for tests).
I do still have the code, what we did was provide a component which configures whether to use schemas or not, thus:
public class SqlLiteMappingsHelper : IMappingsHelper
{
public string TextColumnTableSpecification
{
get
{
// see sqlite faqs re: all varchars are very large whatever you specify
// http://www.sqlite.org/faq.html#q9
return "nvarchar(10)";
}
}
public bool SchemasEnabled
{
get { return false; }
}
}
(and one similar for sql server with SchemasEnabled = true) - in the web app you tell your IoC container to use the sql server one and in your test app you use the Sqlite one. Then in your convention:
public void Apply(IClassInstance instance)
{
var helper = IoC.get<IMappingsHelper>();
if (instance.EntityType.Namespace.EndsWith("Model.OtherEntities") && helper.SchemasEnabled)
{
instance.Schema("OtherDatabase.dbo");
}
}