how to Pull data with paramter using Azure Offline Sync? - azure-mobile-services

I have schema as below and I would like to know if I can get all the TaskCategoryMappings by CategoryId when I have to pulll all data. I went through the documentation here but I cant figure out how to do it ? All the examples are like this one based on UserId. but userid is used for also authentication and on the server side I already handle it fine to return only mappings belong to relevant user and i want in adition to filter by CategoryId?
another SO sample is here also using userId Parameter Passing with Azure Get Service
public class TaskCategoryMapping : TableData
{
public string TaskId { get; set; }
public string CategoryId { get; set; }
public string UserId { get; set; }
}

According to your description, I checked this issue on my side and found that it could work as expected, you could follow the details below to check your code:
Backend models:
public class Tag : EntityData
{
public string TagName { get; set; }
public bool Status { get; set; }
}
public class Message : EntityData
{
public string UserId { get; set; }
public string Text { get; set; }
public virtual Tag Tag { get; set; }
[ForeignKey("Tag")]
public string Tag_Id { get; set; }
}
GetAllMessage action:
// GET tables/Message
public IQueryable<Message> GetAllMessage()
{
return Query();
}
For the client, I just invoke the online table for retrieving the message entities as follows:
Model on client-side:
public class Message
{
public string Id { get; set; }
public string UserId { get; set; }
public string Text { get; set; }
public string Tag_Id { get; set; }
}
var result=await mobileServiceClient.GetTable<Message>().Where(msg => msg.Tag_Id == "c3cd4cf8-7af0-4267-817e-f84c6f0e1733").ToListAsync();
For offline table, the pull operation query would
await messageSyncTable.PullAsync($"messages_{userid}", messageSyncTable.Where(m => m.Tag_Id == "<Tag_Id>"));
Use fiddler, you could find that the request would look like this:
https://{your-app-name}.azurewebsites.net/tables/Message?$filter=Tag_Id eq 'c3cd4cf8-7af0-4267-817e-f84c6f0e1733'

Related

include field from another class

I have a class called Program that has a field called ProgramStatusCode. I'm planning to create another class called Status that has the StatusCode (ID) and StatusDescription. The Status class has a column called StatusType that needs to be filtered on Program and an Active column that needs to be filtered on Yes.
How do I add StatusDescription to the Program class that's already filtered on Program and Yes?
Here are the classes:
Program.cs
public class Program
{
public decimal ID { get; set; }
public string ProgramName { get; set; }
public string ProgramDescription { get; set; }
public string ProgramStatusCode { get; set; }
}
Status.cs
public class Status
{
public int ID { get; set; }
public string StatusType { get; set; }
public string StatusDescription { get; set; }
public string Active { get; set; }
}
Update
The work around that I'm doing is creating another class called ProgramFinal with a field called Status. Instead of creating another class just to include Status, I'd like to have Status in the Program class.
Here is the code:
ProgramFinal.cs
public class ProgramFinal
{
public decimal ID { get; set; }
public string ProgramName { get; set; }
public string ProgramDescription { get; set; }
public string Status { get; set; }
}
Linq Query:
programFinal = (from program in dbContext.Program
join status in dbContext.Status on program.ProgramStatusCode equals status.ID
where status.Active.Contains("Y") && status.StatusType.Contains("Program") && program.ProgramName.Contains(programName)
select new ProgramFinal
{
ID = program.ID,
ProgramName = program.ProgramName,
ProgramDescription = program.ProgramDescription,
Status = status.StatusDescription
}).FirstOrDefault();
So instead of creating a new class just to get the Status Description, I'd like to have Status Description in the Program class itself.
Thanks!

Where should I do the mapping stuff? Repository or Service Layer?

Well, I have this DB Model "Book"
public class Book {
public int Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public bool IsSubmitted { get; set; }
public bool IsCompleted { get; set; }
public bool IsDeleted { get; set; }
}
And I have implemented repository pattern where my GetBook(int id) method returns a Book which looks like this:
public Book GetBook(int id) {
return db.Books.Find(id);
}
However, my BookViewModel needs to query some other things as well. It looks like this:
public class BookViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public string AuthorName { get; set; }
public int CommentsCount { get; set; }
public int FeedbacksCount { get; set; }
public int ViewsCount { get; set; }
}
Currently, my service layer is mapping binding models to DB models and passing them to repository.
Now my question is where should I query this additional (view-specific) data? Should I write separate repository methods for CommentsCount, FeedbacksCount, ViewsCount etc. and call them from my service layer to prepare my view model or should I write a new repository method with return type BookViewModel where I query all the required data in a single query?
Any help is highly appreciated.
The repository methods should map and return or recive DTO's, DAL layer should not know about MVC project, it should only know about DTO's.

If Exists Dont Add Data Entity Framework Many-To-Many

I have these two Models the logic is here One Post can have multiple Categories.
public class Post
{
public Post()
{
this.Categories = new HashSet<Category>();
}
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string ShortDescription { get; set; }
public string PostImage { get; set; }
public string Thumbnail { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? PublishedDate { get; set; }
public string CreatedBy { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
I have three static categories.
When I am trying to add new post its multiplexing CategoryTable creating new categories with same name ,And Mapping Them in to CategoryPostsTable.
The problem is here i want to map that data with existing categories. I dont want to add new category with same name.
I am using Repository Pattern how should i control that ? Is EF has some solution for that ?
I assume you have code like:
var post = new Post();
post.Categories.Add(cat);
context.Posts.Add(post);
...where cat is a Category object representing an existing category.
The latter Add method (DbSet.Add) doesn't only mark the received entity as Added but all entities in its object graph that are not attached to the context. So cat will also be marked as Added if it wasn't attached yet.
What you can do is
context.Entry(cat).State = System.Data.Entity.EntityState.Unchanged;
Now EF will only create the association to the category, but not insert a new category.

Prevent webapi from returning all data from associated tables

I have a table called MemberCompany which has a record for each company a member has. the model is below. When i query it via a webapi method passing in the memberid, i can see in debug mode that it returns the one company for that member, however when i run it in the browser i can see it returns the entire list of members also. Is it possible to just return a collection of membercompany records without the two referenced tables? I commented out the initial code to include these two tables but they appear to still be being included in the response.
public partial class MemberCompany
{
public int id { get; set; }
public int membership_id { get; set; }
public string company_name { get; set; }
public string company_address1 { get; set; }
public string company_address2 { get; set; }
public string company_town_city { get; set; }
public Nullable<int> company_county { get; set; }
public string company_postcode { get; set; }
public string company_tel { get; set; }
public string company_fax { get; set; }
public string company_email { get; set; }
public string company_contact { get; set; }
public string company_web { get; set; }
public string company_country { get; set; }
public Nullable<System.DateTime> last_updated { get; set; }
public Nullable<decimal> latitude { get; set; }
public Nullable<decimal> longitude { get; set; }
public virtual counties counties { get; set; }
public virtual members members { get; set; }
}
WebAPI
[HttpGet("admin/api/membercompany/member/{member_id}")]
public IEnumerable<MemberCompany> GetByMember(int member_id)
{
var Companies = db.MemberCompanies
// .Include(t => t.counties)
//.Include(t => t.members)
.Where(m => m.membership_id == member_id);
return Companies.AsEnumerable();
}
Turn off lazy loading for the context. My best guess is it's on and the entities are loaded when the graph is serialized...
Note: that's actually a good idea in a web app and I'd recommend you do it globally, so that you don't get bitten by performance issues due to lazy loading later, and always know precisely what you'll return.

Asp.net MVC 4 Code First Return Specific Fields from Navigation Property

Been stuck on this for a while so i thought i would ask. I am sure there is something simple i am missing here. Trying to learn Asp.net mvc 4 on my own by building a simple app.
Here is the model:
public class Category
{
public int Id { get; set; }
[Required]
[StringLength(32)]
public string Name { get; set; }
//public virtual ICollection<Note> Notes { get; set; }
private ICollection<Note> notes;
public ICollection<Note> Notes
{
get
{
return this.notes ?? (this.notes = new List<Note>());
}
}
}
public class Note
{
public int Id { get; set; }
[Required]
public string Content { get; set; }
[Required]
[StringLength(128)]
public string Topic { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual IEnumerable<Comment> Comments { get; set; }
public virtual ICollection<Tag> Tags {get; set;}
public Note()
{
Tags = new HashSet<Tag>();
}
}
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Note> Notes { get; set; }
public Tag()
{
Notes = new HashSet<Note>();
}
}
I call this method in a repository from the controller:
public IQueryable<Note> GetAll()
{
var query = _db.Notes.Include(x => x.Category).Include(y => y.Tags);
return query;
}
On the home controller i am trying to return a list of all the notes and wanted to include the category name that it belongs to as well as the tags that go with the note. At first the did not show up so i read some tutorials about eager loading and figured out how to get them to show.
However, my method is not that efficient. The mini-profiler is barking at me for duplicate queries because the navigation properties for category and tags are sending queries for the notes again. IS there any way to just return the properties i need for the category and tag objects?
I have tried several methods with no luck. I was hoping i could do something like this:
var query = _db.Notes.Include(x => x.Category.Name).Include(y => y.Tags.Name);
But i get an error: Cannot convert lambda expression to type 'string' because it is not a delegate type
I have seen that error before that was caused by some missing using statements so i already double checked that.
Any suggestions? Thanks for the help