query soft deleted records in an Azure mobile app easy table - sqlite

I have a Azure mobile Easy Table Xamarin App that is a bunch of lists, reminders for myself. It works fine to add, update and delete. I want to be able to get a list of soft deleted items so I can undelete some to add them back into a list without retyping them. I cannot figure out how to do this. I see in Google searches an IncludeDeleted attribute but it does not seem to apply to the IMobileServiceSyncTable table I am using. Here is the code, but it retrieves zero records. If I run it in LinqPad 5 I get all the soft deleted records.
public async Task<IEnumerable<ListData>> GetDeletedItemsAsync()
{
await InitializeClient();
await SyncItems();
try
{
IEnumerable<ListData> items = await listdata
.Where(listdata => listdata.Deleted == true )
.ToEnumerableAsync();
return new ObservableCollection<ListData>(items);
}
catch (MobileServiceInvalidOperationException msioe)
{
Debug.WriteLine(#"Invalid sync operation: {0}", msioe.Message);
}
catch (Exception e)
{
Debug.WriteLine(#"Sync error: {0}", e.Message);
}
return null;
}
Here is the Class:
public class ListData
{
public string id { get; set; }
[JsonProperty(PropertyName = "listname")]
public string ListName { get; set; }
[JsonProperty(PropertyName = "itemdata")]
public string ItemData { get; set; }
[JsonProperty(PropertyName = "itemdetail")]
public string ItemDetail { get; set; }
[JsonProperty(PropertyName = "deleted")]
public Boolean Deleted { get; set; }
// *** Enable Optimistic Concurrency *** //
[Version]
public string Version { get; set; }
}
What am I missing? Thanks.

Per my understanding, you could try to change your SyncItems method as follows:
private async Task SyncItems()
{
var queryName = $"incsync:s:{typeof(ListData).Name}";
var query = listDataTable.CreateQuery().IncludeDeleted();
await listDataTable.PullAsync(queryName, query);
}
And you could use fiddler to capture the network traces when invoking the above method, then you could check the response and find whether you could retrieve the deleted items.
Then you could leverage DB Browser for SQLite or any other tool to check your local SQLite database and verify your table records to narrow this issue.

The way to do this is to go directly to the server using an IMobileServiceTable object. It is pretty simple. You can then use the IncludeDeleted directive on the query. Case solved.

Related

EF Core with CosmosDB: OwnsOne and OwnsMany throw NullReferenceException

I'm working on a new project that uses CosmosDB and Entity Framework Core (via the Microsoft.EntityFrameworkCore.Cosmos NuGet package, version 5.0.7; the project itself is .NET Core 5). I'm new to both, and running into an issue I can't sort out.
In short, I need to save a complex object to the database. It's a big model that will have multiple collections of classes underneath it, each with their own properties and some with collections underneath them as well. I'm trying to configure EF with OwnsOne and OwnsMany to store these child objects underneath the top-level one. The code compiles, and will save to the database so long as all the owned objects are left empty. But whenever I put anything into an owned object, either with OwnsOne or OwnsMany, I get a pair of NullReferenceExceptions.
I've tried to strip my code down to the very basics. Here's how it currently looks.
Owner and owned classes:
public class Questionnaire
{
// Constructors
private Questionnaire() { }
public Questionnaire(Guid id)
{
Test = "Test property.";
TV = new TestQ();
Id = id;
}
public Guid Id { get; set; }
public string Test { get; set; }
public TestQ TV { get; set; }
// Public Methods
public void AddForm(Form f)
{
// not currently using this method
//Forms.Add(f);
}
}
public class TestQ
{
public TestQ()
{
TestValue = "test ownsone value";
}
public string TestValue { get; set; }
}
DbContext:
public class QuestionnaireDbContext : DbContext
{
public DbSet<Questionnaire> Questionnaires { get; set; }
public QuestionnaireDbContext(DbContextOptions<QuestionnaireDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultContainer(nameof(Questionnaires));
modelBuilder.Entity<Questionnaire>().HasKey(q => q.Id);
modelBuilder.Entity<Questionnaire>().OwnsOne(q => q.TV);
}
}
And the code from the service that calls the dbContext (note that this is based on a generic service that I didn't set up originally). The actual exceptions are thrown here.
public virtual TEntity Add(TEntity entity)
{
_context.Entry(entity).State = EntityState.Added;
_context.SaveChanges();
return entity;
}
Ultimately I need this to work with OwnsMany and a collection, but I figured it might be simpler to get it working with OwnsOne first. The key thing to note here is that if I comment out the line
TV = new TestQ();
in the Questionnaire class, the model persists correctly into CosmosDB. It's only when I actually instantiate an owned entity that I get the NullReferenceExceptions.
Any advice would be much appreciated! Thank you!
Well, I'm not sure why this is the case, but the issue turned out to be with how we were adding the document. Using this generic code:
public virtual async Task<TEntity> Add(TEntity entity)
{
_context.Entry(entity).State = EntityState.Added;
await _context.SaveChanges();
return entity;
}
was the issue. It works just fine if I use the actual QuestionnaireDbContext class like so:
context.Add(questionnaire);
await context.SaveChangesAsync();

Entity Framework with ASP.NET one-to-many relation causes NullReference error

In my database I have two entities: DbStatus and DbTask
public class DbStatus
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<DbTask> Tasks { get; set; }
}
public class DbTask
{
public int Id { get; set; }
public string Title { get; set; }
public bool Done { get; set; }
public int StatusId { get; set; }
public virtual DbStatus Status { get; set; }
}
In the OnModelCreating method, I establish the relation with the following code:
modelBuilder.Entity<DbStatus>()
.HasMany(s => s.Tasks)
.WithOne(t => t.Status)
.HasForeignKey(t => t.StatusId);
I also add some sample data in this method, setting the StatusId of newly created DbTasks.
Problem is, when I try to access the Status name of the DbTask using
task.Status.Name
I get a NullReferenceException.
Can anyone help me how to set up the relation properly?
IMPORTANT
For anyone reading this, the quickest solution (and the one fulfilling task-specific criterias) for this was provided Rob. However, you should read and implement the solution provided by Steve Py, for the reasons they also describe in their answer!
When getting your list of DbTasks from the database, you need to tell it to include the child Status objects.
Try something like this:
var tasks = dbContext.DbTasks
.Include(t => t.Status)
.ToList();
Setting a FK on an entity does not automatically cause that related entity to be loaded. When working with navigation properties I recommend avoiding declaring FK fields in entities and using shadow properties to avoid issues like this.
To update a status on a DbTask:
public ActionResult MarkTaskComplete(int taskId)
{
var completeStatus = _context.Statuses.Single(x => x.StatusId = Statuses.Complete);
// TODO: Validation that user can update task etc.
var task = _context.Tasks
.Include(x => x.Status)
.Single(x => x.TaskId == taskId);
task.Status = completeStatus;
_context.SaveChanges();
return Json(new { success = true; status = task.Status.Name } );
}
The issue with FK fields is that the behaviour can differ depending on whether you use the navigation property or the FK, and whether the navigation property is eager loaded or not. From the perspective of the Task, there are two sources of truth for the current Status, some code might check task.StatusId while others use task.Status.StatusId. These values could differ depending on one being updated without the other.
While this can mean a trip to the DB to fetch a status, fetching rows by ID is extremely fast, and also provides a validation that your methods are only using legal values.

SqlException: On Delete Cascade not working ASP.NET

When I try to delete a user from the ASP.NETUsers table I get SqlException:
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_Applications_AspNetUsers_UserID". The conflict occurred
in database "JobGuide", table "dbo.Applications", column 'UserID'.
This problem is occurring because the User's Id is the Foreign key in another table, but "On delete cascade" is not working. For more details this is my model:
My extended Identity User:
public class AppUser : IdentityUser
{
public string Name { get; set; }
public string City { get; set; }
public string RoleName { get; set; }
public virtual ICollection<Application> Applications { get; set; }
}
Application model (i.e. when a user applies for a job):
public class Application
{
public int ApplicationID { get; set; }
public int JobID { get; set; }
public virtual Job Job { get; set; }
public string UserID { get; set; }
public virtual AppUser User { get; set; }
}
Job model:
public class Job
{
public int JobID { get; set; }
public string Title { get; set; }
public virtual ICollection<Application> Applications { get; set; }
}
So up to here I created two One to Many relationships, AspNetUser one to many with Application and Job one to many with Application.
And this is my Fluent API mapping configuration:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Application>()
.HasKey(i => i.ApplicationID);
builder.Entity<Application>()
.HasOne<AppUser>(sc => sc.User)
.WithMany(s => s.Applications)
.HasForeignKey(sc => sc.UserID)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<Application>()
.HasOne<Job>(sc => sc.Job)
.WithMany(s => s.Applications)
.HasForeignKey(sc => sc.JobID)
.OnDelete(DeleteBehavior.Cascade);
}
Delete method from controller:
var userInfo = await userManager.FindByIdAsync(user.Id);
if (userInfo == null)
{
return NotFound();
}
_ = await userManager.RemoveFromRoleAsync(userInfo, userInfo.RoleName);
_ = await userManager.DeleteAsync(userInfo);
int rowsAffected = await db.SaveChangesAsync();
Any idea why this error is not disappearing, is Fluent API good? or i need to type raw Sql to delete the Application with that User once and then the User? I have looked at almost all similar questions but none of them are working for me.
It seems that the cascade delete is not configured in the application table, try to use SSMS to check it:
Open the SQL Server Object Explorer (or using Server Explorer), find the SQL Server Database, then right click the Applications table -> Script As -> CREATE To -> New Query Window, then check whether the table is configured Cascade delete, check this screenshot:
To solve this issue, after configuration Cascade Delete using Fluent API mapping, please remember to enable migration and update the database:
Add-Migration AddCascadeDelete
Update-Database
Besides, you could also configure the Cascade Delete by executing the following SQL command (via SSMS):
ALTER TABLE [dbo].[Applications]
ADD CONSTRAINT [FK_Applications_AspNetUsers_UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE;
Can you try setting it the other way around;
builder.Entity<User>()
.HasMany<Application>(u => u.Applications)
.WillCascadeOnDelete();
or use .WillCascadeOnDelete() on your code.

Using Backlink feature of realm-dotnet in Xamarin.Forms App

My current employer is developing a mobile app using Xamarin.Forms and Asp.net mvc on the backend. I suggested to use realm in the mobile app. My manager want to see a POC(Proof of concept) app using realm with backlink feature before allowing it to be used in the app. I am working on the POC on GitHub . The documentation is very limiting and the GitHub repo of realm-dotnet don’t have good sample.
I completed the project. But unable to implement backlink. The sample app I have developed allow user to create assignees(employees) in the first page. The user can delete or edit the employees using context menu. When the user clicks on the employee name the app navigates to the ToDoListPage of that particular employee. Here the user can create ToDoItems. On this ToDoList page I want to show the ToDoItems that where assigned to that employee only.
The models were as follows:
public class Assignee : RealmObject
{
public Assignee()
{
ToDoItems = Enumerable.Empty<ToDoItem>().AsQueryable();
}
[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
public string Role { get; set; }
[Backlink(nameof(ToDoItem.Employee))]
public IQueryable<ToDoItem> ToDoItems { get; }
}
public class ToDoItem : RealmObject
{
[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
public string Description { get; set; }
public bool Done { get; set; }
public Assignee Employee { get; set; }
}
I am adding employee to each ToDo Item:
Item.Employee = Employee;
_realm.Add(Item);
Now I want to access the ToDoItems for the Employee:
Items = _realm.All<Assignee>().Where(x => x.Id == EmployeeId).FirstOrDefault().ToDoItems;
But this does not work. I will be grateful if someone can help me out by preferably writing code in my sample app or write the correct code in the reply.
Thank you
Firstly, Realm .NET doesn't currently support traversing properties (x.Employee.Id). Due to this, when I start the app and try to go to the ToDoListPage, the app crashes with the exception:
The left-hand side of the Equal operator must be a direct access to a persisted property in Realm
Realm supports object comparison, so we can fix this like so:
var employee = _realm.Find<Assignee>(EmployeeId);
Items = _realm.All<ToDoItem>().Where(x => x.Employee == employee);
Secondly, everything seemed fine in your code, so I dug a bit deeper and saw why it isn't working. The issue is that when we try to get all items with the code above, the EmployeeId parameter is null. Since the EmployeeId is being populated after the load logic has been triggered, we don't need to load the data in the ctor. So you can remove this code.
Finally, since you won't be loading the data in the ctor, and instead in the SetValues method, the UI needs to know, when the data has been updated, what exactly to redraw. Thus, you need to mark the collection to be Reactive too:
[Reactive]
public IEnumerable<ToDoItem> Items { get; set; }
Then, you need to change the SetValues method to use object comparison, instead of traversing:
async Task SetValues()
{
Employee = _realm.Find<Assignee>(EmployeeId);
Title = Employee.Name;
Items = _realm.All<ToDoItem>().Where(x => x.Employee == Employee);
}
To sum up - you don't need to try and load the data in the ctor, since you don't know when the EmployeeId will be set. You are already tracking when the property will change and inside the SetValues command you simply need to change the expression predicate.

Windows Phone sqlite select all

I have a query which simply does, in a table named "CUSTOMER" a select all.
This is my Table:
public class CUSTOMER : Extension.Shared
{
[PrimaryKey, Indexed]
public Guid ID { get; set; }
[Indexed]
public string FULL_NAME { get; set; }
public string PHONE_NO { get; set; }
//public string PIVA_CF { get; set; }
public DateTime? MODIFIED_ON { get; set; }
And this is the query which gives me the problem
public List<CUSTOMER> SelectAll()
{
SQLiteConnection conn = new SQLiteConnection(DatabasePath());
return conn.Query<CUSTOMER>("SELECT * FROM CUSTOMER");
}
Every times i run this operation, the query returns an error from SQLite.cs file:
if (r != SQLite3.Result.OK)
{
throw SQLiteException.New (r, String.Format ("Could not open database file: {0} ({1})", DatabasePath, r));
}
If the size can be a useful, this table has nearly 50000 records.
I have the same problem in a couple of tables with 100000 or 80000 records.
No problems with other Tables, no problem with other queries.
I can say this because since I thought the table was badly saved, I installed again and again the table, but I noticed that from the same page i can call this query without any problem, for all tables:
public List<CUSTOMER> SelectByFilter(string Filter)
{
SQLiteConnection conn = new SQLiteConnection(DatabasePath());
return conn.Query<CUSTOMER>(string.Format("SELECT * FROM CUSTOMER WHERE FULL_NAME LIKE '{0}%'", Filter));
}
I don't really know where can this error came from. I don't know any size's restriction on Sqlite3. Any help will be appreciated.
This error tells that it cannot find the database at the path given check with isolated storage tool that the database exist in that place also check you are referencing the correct path to database file in your DatabasePAth() method or else try this
public List<CUSTOMER> SelectByFilter(string Filter)
{
string dbpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "yourdatabsefilename.db");
//database is in home directory not in any subfolders. then only this code will work
SQLiteConnection conn = new SQLiteConnection(dbpath);
return conn.Query<CUSTOMER>(string.Format("SELECT * FROM CUSTOMER WHERE FULL_NAME LIKE '{0}%'", Filter));
}

Resources