MobileServiceClient IMobileServiceSyncTable PullAsync Resource Not Found - xamarin.forms

This error "
WindowsAzure.MobileServices The resource you are looking for has been
removed, had its name changed, or is temporarily unavailable"
Occurs when trying to retrieve records from an Azure SQL Db connected to a App Service. It occurs for below line of code
await SurveyResponseTable.PullAsync("SurveyResponse", SurveyResponseTable.CreateQuery()).ConfigureAwait(false);
where;
SurveyResponseTable = mClient.GetSyncTable<SurveyResponse>();
and
mClient = new MobileServiceClient(Constants.BackendUrl);
I have read every SO reference to this error but can find no solution. I am not sure whether the table on the Azure SQL Db has some difference to the class on the mobile client, whether a permissions problem or what.
So at this stage I am asking for suggestions as to how to debug, what to look for (can find nothing that helps me in the exception details). Or maybe how to create a table correctly on server side. I have seen when playing with the ToDoItem example that small things can stop successful connection such as misspellings, EF using plurals on server table name, lower case id etc. Tried all those things but still same error.
Azure SQL table structure;
CREATE TABLE [dbo].[SurveyResponses] (
[id] NVARCHAR (128) DEFAULT (newid()) NOT NULL,
[ThinkingStyle] NVARCHAR (128) NULL,
[ThinkingStyleA] NVARCHAR (128) NOT NULL,
[ThinkingStyleB] NVARCHAR (128) NULL,
[ResponseOrder] INT NOT NULL,
[ResponseGroup] NVARCHAR (128) NOT NULL,
[Question] NVARCHAR (128) NULL,
[ResponseA] NVARCHAR (512) NOT NULL,
[ResponseB] NVARCHAR (512) NULL,
[AzureVersion] ROWVERSION NOT NULL,
[CreatedAt] DATETIMEOFFSET (7) DEFAULT (sysutcdatetime()) NOT NULL,
[UpdatedAt] DATETIMEOFFSET (7) NULL,
[Deleted] BIT DEFAULT ((0)) NOT NULL,
CONSTRAINT [PK_dbo.SurveyResponses] PRIMARY KEY NONCLUSTERED ([id] ASC));
GO CREATE CLUSTERED INDEX [IX_CreatedAt]
ON [dbo].[SurveyResponses]([CreatedAt] ASC);
GO CREATE TRIGGER [TR_dbo_SurveyResponses_InsertUpdateDelete] ON [dbo].[SurveyResponses] AFTER INSERT, UPDATE, DELETE AS BEGIN UPDATE [dbo].[SurveyResponses] SET [dbo].[SurveyResponses].[UpdatedAt] = CONVERT(DATETIMEOFFSET, SYSUTCDATETIME()) FROM INSERTED WHERE inserted.[id] = [dbo].[SurveyResponses].[id] END
And here is the mobile client class;
public class SurveyResponse
{
public string id { get; set; }
public string ThinkingStyle { get; set; }
public string ThinkingStyleA { get; set; }
public string ThinkingStyleB { get; set; }
public int ResponseOrder { get; set; }
public string ResponseGroup { get; set; }
public string Question { get; set; }
public string ResponseA { get; set; }
public string ResponseB { get; set; }
[Version]
public string AzureVersion { get; set; }
[CreatedAt]
public DateTime CreatedAt { get; set; }
[UpdatedAt]
public DateTime UpdatedAt { get; set; }
}

Related

How to do bulk update using dapper in .NET core

I have two tables Systeminfo and Driveinfo. In Systeminfo table I have
SystemID, SystemDate, HostName, UserName, Total_Of_Drives_Size, IsActive, CreateDate, ModifyDate column
and In DriveInfo table I have DriveID, DriveName, Used_Space, Drive_Space, SystemID as foreign key of first table's systemId.
I'd like to create a view model and use a user-defined table to insert and update bulk records into the table, with the view model consisting of a username, hostname, and a list of drives and create a controller class that will expose a web API endpoint for collecting information. This class should use the service class to collect the data and return a response to the client.
Here I tried this:
stored procedure:InsertUpdateSystemAndDriveInfo
alter PROCEDURE InsertUpdateSystemAndDriveInfo
#SystemID INT = NULL OUTPUT,
#SystemDate DATETIME,
#HostName NVARCHAR(50),
#UserName NVARCHAR(50),
#Total_Of_Drives_Size BIGINT,
#IsActive BIT,
#CreateDate DATETIME,
#ModifyDate DATETIME,
#DriveInfoList DriveInfoTableType READONLY
AS
BEGIN
SET NOCOUNT ON;
IF #SystemID IS NULL
BEGIN
INSERT INTO SystemInfo (SystemDate, HostName, UserName, Total_Of_Drives_Size, IsActive, CreateDate, ModifyDate)
VALUES (#SystemDate, #HostName, #UserName, #Total_Of_Drives_Size, #IsActive, #CreateDate, #ModifyDate);
SET #SystemID = SCOPE_IDENTITY();
END
ELSE
BEGIN
UPDATE SystemInfo
SET SystemDate = #SystemDate,
HostName = #HostName,
UserName = #UserName,
Total_Of_Drives_Size = #Total_Of_Drives_Size,
IsActive = #IsActive,
ModifyDate = #ModifyDate
WHERE SystemID = #SystemID;
DELETE FROM DriveInfo WHERE SystemID = #SystemID;
END
INSERT INTO DriveInfo ( DriveName, Used_Space, Drive_Space, SystemID)
SELECT DriveName, Used_Space, Drive_Space, #SystemID
FROM #DriveInfoList;
END
Viewmodel code:
namespace SystemDrive_Api.Model
{
public class SystemInformation
{
public int SystemID { get; set; }
public DateTime SystemDate { get; set; }
public string UserName { get; set; }
public string HostName { get; set; }
public long Total_Of_Drives_Size { get; set; }
public bool IsActive { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ModifyDate { get; set; }
public List<DrivesInformation> driveInfoList { get; set; }
}
public class DrivesInformation
{
public string DriveName { get; set; }
public long Used_Space { get; set; }
public long Drive_Space { get; set; }
}
}

linq for two tables on dotvvm framework

I recently started to learn C# and DotVVM and i run to a problem. I dont know how to make linq query from two tables. I know how to make a query or linq for one table, but i got stuck with two tables.
My database of Authors
CREATE TABLE [dbo].[Autors] (
[ID] INT IDENTITY (1, 1) NOT NULL,
[Jmeno] NVARCHAR (MAX) NULL,
[Prijmeni] NVARCHAR (MAX) NULL,
[ProstredniJmeno] NVARCHAR (MAX) NULL,
[Narozeni] DATE DEFAULT (NULL) NULL,
[Umrti] DATE DEFAULT (NULL) NULL,
[Bio] NVARCHAR (MAX) NULL,
[Narodnost] NVARCHAR (MAX) NULL,
[Obrazek] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Autors] PRIMARY KEY CLUSTERED ([ID] ASC));
And my database of Books
CREATE TABLE [dbo].[Books] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Nazev] NVARCHAR (MAX) NOT NULL,
[IdAutor] INT NOT NULL,
[Popis] NVARCHAR (MAX) NULL,
[Isbn] NCHAR (15) NULL,
[IdZanr] INT NULL,
[RokVydani] INT NULL,
[PocetStran] INT NULL,
[Obrazek] TINYINT NULL,
CONSTRAINT [PK_dbo.Books] PRIMARY KEY CLUSTERED ([Id] ASC));
And i need to create linq query to get this
namespace AbsolvetnskaPrace.Models
{
public class AutorListModel
{
public int Id { get; set; }
public string Jmeno { get; set; } //First name
public string Prijmeni { get; set; } //Last name
}
}
namespace AbsolvetnskaPrace.Models
{
public class BookListModel
{
public int Id { get; set; }
public string Nazev { get; set; } //Title
public int IdAutor { get; set; }
public string JmenoAutor { get; set; } //Author's first name
public string PrijmeniAutor { get; set; } //Author's last name
}
}
i have class of Services where i have all linq queries i use. What I need is to make list of books but get a author's name and last name.
This is a view of the list:
<div class="page-center">
<div class="page-grid-top">
<div class="student-image"></div>
<h1>{{resource: Texts.Title_BookList}}</h1>
<dot:AuthenticatedView>
<dot:RouteLink Text="{resource: Texts.Label_NewBook}" RouteName="CRUD_BookCreate" class="page-button btn-add btn-long" />
</dot:AuthenticatedView>
</div>
<dot:GridView DataSource="{value: Books}" class="page-grid">
<Columns>
<dot:GridViewTextColumn ValueBinding="{value: Nazev}" HeaderText="{resource: Texts.Label_Title}" />
<dot:GridViewTextColumn ValueBinding="{value: IdAutor}" HeaderText="{resource: Texts.Label_BookAutor}" />
<dot:GridViewTemplateColumn>
<dot:RouteLink Text="{resource: Texts.Label_Detail}" RouteName="CRUD_BookDetail" Param-Id="{{value: Id}}" />
</dot:GridViewTemplateColumn>
<dot:GridViewTemplateColumn>
<dot:RouteLink Text="{resource: Texts.Label_Edit}" RouteName="CRUD_BookEdit" Param-Id="{{value: Id}}" />
</dot:GridViewTemplateColumn>
</Columns>
<EmptyDataTemplate>
{{resource: Texts.EmptyAutorTable}}
</EmptyDataTemplate>
</dot:GridView>
</div>
And finaly the result looks like this but the numbers in "Jméno a příjmení autora" shuld by Autor's first and last name. Right now the is Author's Id.
I know that this question is really long but I would be thankfull for any advice you can get me.
your classes (entity framework model) are missing navigation properties.
You have to add properties like this:
namespace AbsolvetnskaPrace.Models
{
public class AutorListModel
{
public int Id { get; set; }
public string Jmeno { get; set; } //First name
public string Prijmeni { get; set; } //Last name
--> pubilc ICollection<BookListModel> BookListModels { get; set; }
}
public class BookListModel
{
public int Id { get; set; }
public string Nazev { get; set; } //Titleenter code here
public int IdAutor { get; set; }
--> public AutorListModel Author { get; set; }
public string JmenoAutor { get; set; } //Author's first name
public string PrijmeniAutor { get; set; } //Author's last name
}
}
Then you linq query can look like:
context.Books.Include(a=> a.Authors).Where( -- lambda condition --).Select( -- lambda transformation --).ToList()
DotVVM serializes the objects in ViewModel, so it is highly recommended to transform you EF classes into DTO (data transfer objects) which has no logic and their main purpose is to transfer data and nothing else.

How does Entity Framework Core decide which properties are going to be NOT NULL in the database?

I am using Entity Framework to create a simple banking application for learning purposes. I don't understand how it decides which properties will become nullable and which become non nullable columns in the database.
For example this
model
public class Transaction
{
public int Id { get; set; }
public Account From { get; set; }
public Account To { get; set; }
[DataType(DataType.Currency)]
public decimal Amount { get; set; }
public String Currency { get; set; }
public DateTime Timestamp { get; set; }
}
generates the following
SQL
CREATE TABLE [dbo].[Transactions] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Amount] DECIMAL (18, 2) NOT NULL,
[Currency] NVARCHAR (MAX) NULL,
[FromId] INT NULL,
[Timestamp] DATETIME2 (7) NOT NULL,
[ToId] INT NULL,
CONSTRAINT [PK_Transactions] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Transactions_Accounts_FromId] FOREIGN KEY ([FromId]) REFERENCES [dbo].[Accounts] ([Id]),
CONSTRAINT [FK_Transactions_Accounts_ToId] FOREIGN KEY ([ToId]) REFERENCES [dbo].[Accounts] ([Id])
);
GO
CREATE NONCLUSTERED INDEX [IX_Transactions_FromId]
ON [dbo].[Transactions]([FromId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_Transactions_ToId]
ON [dbo].[Transactions]([ToId] ASC);
Question
Why is Currency nullable but not Amount? And what about the foreign keys FromId and ToId?
Appendix
In case it is relevant, here is the Account class referenced by the Transaction class.
public class Account
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
[InverseProperty("From")]
public ICollection<Transaction> TransactionsSend { get; set; }
[InverseProperty("To")]
public ICollection<Transaction> TransactionsReceived { get; set; }
public decimal Balance { get; set; }
}
There is a set of conventions at work here. You can fine-tune everything but the basic rules are clear and simple:
Why is Currency nullable but not Amount? And what about the foreign keys FromId and ToId?
Amount is a value-type. The C# property can't be null When you change it to decimal? the column will be nullable too.
Currency is a reference-type (string ). The From and To properties are references too. References will map to ALLOW-NULL unless you mark them as [Required]
And DateTime is also a value-type.

Wrong navigation properties when using SQLite and Entity Framework 6

Lately I've been playing around with SQLite using Entity Framework but something is not very clear to me regarding the navigation properties of the generated entities after DB first approach. And more specifically, many-to-many relationships.
Note: Using ASP.NET Web Api OWIN project.
This is what I did:
I installed latest version of Entity Framework
I installed latest version of System.Data.SQLite
I used Firefox add-on to create my database. It generated my *.sqlite
Example for one of my many-to-many db definitions while creating the DB:
CREATE TABLE "Users"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"IdSrvId" INTEGER NOT NULL UNIQUE ,
"FirstName" VARCHAR NOT NULL ,
"LastName" VARCHAR NOT NULL ,
"Email" VARCHAR NOT NULL ,
"About" VARCHAR NOT NULL ,
"GenderId" INTEGER NOT NULL UNIQUE ,
"BirthDate" DATETIME,
"PhoneNumber" VARCHAR
)
CREATE TABLE "UserLanguаges"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"UserId" INTEGER NULL REFERENCES Users(Id),
"LanguageId" INTEGER NULL REFERENCES Languаges(Id)
)
CREATE TABLE "Langugaes"
(
"Id" INTEGER PRIMARY KEY NOT NULL UNIQUE ,
"Name" VARCHAR NOT NULL UNIQUE
)
After that, I used Visual Studio 2015 to create a Data Model using that *.sqlite file. Following this tutorial: SQLite EntityFramework 6 Tutorial
After the generation I got all of my tables as entities looking like this:
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.GroupUsers = new HashSet<GroupUser>();
this.UserLanguаges = new HashSet<UserLanguаges>();
}
public long Id { get; set; }
public long IdSrvId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string About { get; set; }
public long GenderId { get; set; }
public Nullable<System.DateTime> BirthDate { get; set; }
public string PhoneNumber { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
}
public partial class Languаges
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Languаges()
{
this.UserLanguаges = new HashSet<UserLanguаges>();
}
public long Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
}
public partial class UserLanguаges
{
public long Id { get; set; }
public Nullable<long> UserId { get; set; }
public Nullable<long> LanguageId { get; set; }
public virtual Languаges Languаges { get; set; }
public virtual User User { get; set; }
}
What worries me here, are the navigation properties inside User and Language entities. As you can see, they make a reference to the 'bridge' table helping for the many-to-many relationship but not directly to the other entity as I expected.
I expected this:
public virtual ICollection<UserLanguаges> UserLanguаges { get; set; }
to look like this:
public virtual ICollection<Languаge> Languаges { get; set; }
inside of the User entity.
How can I fix that?
The only time Entity Framework can omit the join table is if that table consists purely of the keys of the tables being joined in a many-to-many relationship. It's the presence of the Id column on this table that is causing it to generate a new entity.
The only way around this is to remove that Id column and make that table have a composite key consisting of the UserId and LanguageId keys. If you cannot change the database schema, there's no other option but take a deep breath and accept how it works.
Some additional reading on how EF handles many-to-many relationships: https://msdn.microsoft.com/en-us/library/dd742359.aspx

Update a table when the view has been modified by using a trigger

I have a database in a server as [test]. This [test] database has about fifty tables and I have created a view in that [test] database by using most essential table columns. Then again there's another database in the same server as [list] and I have created a view in the [list] database by referring to the [test] database view. Then I inserted the data in the view of the [list] database to a table in the same [list] database. I need to update my both view and table in the [list] database once the [test] database has been updated.
To do that first I have written a trigger to update the view in the [list] database and it worked. This is the trigger.
Use database list
Create trigger UpdateView on listView Instead of Update
AS
Begin
Update [test].[dbo].[testView]
set testPersonId = testPersonId + 1
From testView
End
Go
Now I need to update my [list] database table once the listView has updated. To do that I have written the following trigger. When I executed the trigger, I got the result of commands completed successfully, but didn't update my table.
Use database list
Create trigger UpdateTable on listTable Instead of Update
AS
Begin
Update [list].[dbo].[listView]
set listPersonId = listPersonId + 1
From listView
End
Go
I just need to know the wrong of this trigger and how to make it work.
Here are the object in my testView.
> testPersonId int
> testPersonName nvarchar
> testDepId int
> testDepName nvarchar
> testLogRecordNowId int
> testLogRecordNowTime datetime
> testNowLogEvent smallint
> testLastLogRecordTime datetime
> testLastLogEvent smallint
> testInTimeHour nvarchar
> testInTimeMinute nvarchar
Here are the objects in my listView.
> listPersonId int
> listPersonName nvarchar
> listDepId int
> listDepName nvarchar
> listLogRecordNowId int
> listLogRecordNowTime datetime
> listNowLogEvent smallint
> listLastLogRecordTime datetime
> listLastLogEvent smallint
> listInTimeHour nvarchar
> listInTimeMinute nvarchar
Then my table in the [list] database has created by the codefirst manner. Here's the code.
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PersonLogId { get; set; }
[Required]
public int PersonId{ get; set; }
[Required, StringLength(32)]
public string PersonName { get; set; }
[Required]
public int DepId { get; set; }
[Required, StringLength(64)]
public string DepName { get; set; }
public int LogRecordNowId { get; set; }
public DateTime LogRecordNowTime { get; set; }
[Required]
public Int16 LogEventNow { get; set; }
public DateTime LogRecordLastTime { get; set; }
[Required]
public Int16 LogEventLast { get; set; }
[Required, StringLength(33)]
public string InTimeHour { get; set; }
[Required]
public int InTimeMinuteMinute { get; set; }
I was able to insert the listView data to this table(listTable). Now I just need to update this table once the listView is updating. Is it possible to do that with triggers? If it is not, is there another way to do that without using triggers?

Resources