Mobile Offline Sync database on Xamarin form - c#

Currently looking at the Enable Offline Sync for Xamarin Form : https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-offline-data.
I am having the code running on mobile and the backend run and created ToDoList table with 2 entries. The table created in ToDoList has UpdateAt, Version and Deleted columns create from the controller which extends TableController.
In existing web application currently already using the database with tables, do I have to create all the tables with UpdateAt, Version and Deleted columns. I am unsure how this data generated for those columns in web application so that both mobile and web data can be consistent.

This free online book will go through details of getting the backend setup.
https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/dataconcepts/
One of the ways described is to you EF already built in Code-First with migrations, and have a base class that looks like this
public abstract class TableData
{
public string Id { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public byte[] Version { get; set; }
}
Those are the table properties missing you asked about.

Related

Why is the wrong DB being updated by EF6?

I'm trying to use EF6 in my project and I've got two databases I'm trying to interact with. My app.config has connection strings for both, and I have two DbContext classes that pass in the app.config's key for the corresponding connection string. One context:
public LogProcessorContext() : base("LogProcessorDb")
{
}
public DbSet<LogFile> LogFiles { get; set; }
the other context:
public MessageTrackingContext() : base("MessageTrackingDb")
{
}
public DbSet<JournalLog> JournalLogs { get; set; }
but when I add the migration using add-migration NewBranch and update the db, a log file table gets added to the message tracking db (the wrong one), and the journallogs table doesn't get added at all. If anyone has any experience getting ef to play nice with multiple db/contexts, i'm all ears. I'm sure its just some simple mistake I'm making.
btw,
"LogProcessorDb"
and
"MessageTrackingDb"
are the keys in my app.config for my connection strings.
Thanks!

Xamarin sqlite mapping models to database table

I have deployed a database file for a xamarin project using the following guide. http://arteksoftware.com/deploying-a-database-file-with-a-xamarin-forms-app/
I created a model class as such:
[Table ("Person")]
public class Person
{
[PrimaryKey, AutoIncrement, Column("Id")]
public int Id { get; set; }
[NotNull, Column("Actor_Id")]
public int ActorId { get; set; }
}
When I try to do an insert in the repository
dbConn.Insert(newPerson);
I'm getting SQLite.Net.SQLiteException: table Person has no column named ActorId.
If the column name in the database is Actor_Id shouldn't the [Column] attribute in the model map it to the table.
There is a chance that your phone/emulator is previously deployed with app that with older database schema.
In this case, you will need to:
Remove the existing app in your phone/emulator. Then the next run should be OK due to it is created using new database schema. OR
Perform database upgrade script to change the schema of the current old database. Only do this if your app is already publish to store.
I found the problem to be that I was using the SQLite library in the model. Changing it to use SQLite.Net.Attributes allowed the correct mapping.
using SQLite.Net.Attributes;

ASP.NET publishing creating not all database

I have a little problem with publishing my application.
Here's the DbContext:
public class UsersDb : DbContext
{
public DbSet<Users> Users { get; set; }
public DbSet<Locations> Locations { get; set; }
}
When i'm publishing my application, all tables (Users and __MigrationHistory) are creating on SQL server database, but Locations table isn't.
I'm trying to use Package/Publish SQL tab in project properties, but got and error
Error MSB4018: The "SqlScriptPreprocessSqlVariables" task failed unexpectedly
Also tried to use several DbContexts but with no luck. Locations table was still not created.

Azure Mobile Services Identity Column Not Assigned While Offline

I'm using the Azure Mobile Services .NET backend with Xamarin.Forms and I'm having an issue with using int identity ID columns. When in an offline scenario, I can successfully add the record to the DB, but the CustomerId column which is an identity remains at 0. Once online, when I sync, the value is set by SQL server and populated on the mobile device.
As the ID is required as I also need to create related data in another table, the question is, how can I get/set an ID for an entity before it's sync'd with the server? I can't set it manually as it may clash with another client. I am suing an existing SQL database, so would prefer not to have to entirely change the scheme to change the ID to use strings or guid's.
public class Customer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomerId { get; set; }
public string Name { get; set; }
....
}
public async Task SaveCustomer(Customer item) {
await CustomerTable.InsertAsync (item);
}
The scenario you are mentioning is exactly why Azure Mobile Apps uses strings for IDs. If you use a string, you can generate a GUID for the ID and use that prior to sync.
The only way to get the Id that is generated by the database is to sync the table.

Asp.Net MVC Identity and Our Database single context for whole application

I have used MVC Identity for user login and registration purposes....but i needed to add additional database table also like this are completly unrelated with Identity database table...in my application i use code first.....i have made class for this.....but i want a single context in whole application
public DbSet<Dept> Dept{ get; set; }
public DbSet<Course> Course{ get; set; }
public DbSet<Etc> Etc{ get; set; }
As asp.net identity have ApplicationDBContext and i was using other database context which is defined by this
public class MainContext : DbContext
So what i want is i should be able to create my database table and Identity table when database changes detected and i am creating user and role when application starts at ApplicationDbInitializer which is working ok.
Now i want just a single context for whole application....
Things i have tried or think is inherit Maincontext with ApplicationDbContext
But still i can see 2 context and Other thing is i can inherit from IdentityDbContext but still there will be 2 context.
Show me simple example if possible.
Thanks.

Categories