I want to map MinerStatus from Entity.Miner.
I tried to include Miner entity in Issue entity, but it did not happen.
Service
Mapping profiles
I need to do it with AutoMapper, but it returns null.
This is my code:
Issue entity:
namespace Mining_Automation.Entities;
[Table("Issues")]
public class Issue : EntityBase, IAuditableEntity
{
public string? DescriptionForReportIssues { get; set; }
public string? ObjectsInMonitoringRoom { get; set; }
public bool? IsReadyToSendToFarm { get; set; }
public StatusInIssue? CurrentStatusInIssue { get; set; }
public long MinerId { get; set; }
public string Date { get; set; }
public string Time { get; set; } = TimeOnly.FromDateTime(DateTime.UtcNow).ToString();
public string? ProblemDescription { get; set; }
public string? Description { get; set; }
public string? TestRoomDescription { get; set; }
public string? RepairRoomDescription{ get; set; }
public string? RepairRoomIssueDescription { get; set; }
public Miner Miner { get; set; }
}
Miner entity:
namespace Mining_Automation.Entities;
[Table("Miners")]
public class Miner: EntityBase , IAuditableEntity
{
public string MinerName { get; set; }
public string AssetTag { get; set; }
public string MinerSerialNumber { get; set; }
public bool IsMinerActive { get; set; }
public long MinerStatus { get; set; }
public string WorkerName { get; set; }
public string? MinerDescription { get; set; }
public long FarmId { get; set; }
public Farm Farm { get; set; }
public ICollection<Issue> Issues{ get; set; }
public virtual MinerParts MinerParts { get; set; }
public ICollection<WatcherDataByMiner> WatcherDataByMiner { get; set; }
}
AllIssues view model:
namespace Mining_Automation.ViewModels.Issue;
public class ShowAllIssues
{
public long MinerId { get; set; }
public long IssueId { get; set; }
public string Date { get; set; }
public string WorkerName { get; set; }
public string MinerSerialNumber { get; set; }
public MinerStatus MinerStatus { get; set; }
public string DescriptionForReportIssues { get; set; }
public string ObjectsInMonitoringRoom { get; set; }
public bool IsReadyToSendToFarm { get; set; } = false;
public string ProblemDescription { get; set; }
public bool IsDeleted { get; set; }
public long Id { get; set; }
#endregion
}
Hi I am trying to deserialize this json. And my application does not do it for me.
i am using c#
any suggestion? thanks
this way i try to deserialize
var deserialize = resultado.Content.ReadAsStringAsync().Result;
var a = JsonConvert.DeserializeObject<product>(deserialize);
json received
{"product":{"id":6979552313549,"title":"Balance 100% Whey Protein 2.8kg w\/ FREE Magnesium complete powder","body_html":"Mountaineering backpack","vendor":"Balance","product_type":"physical","created_at":"2022-05-16T17:41:57-06:00","handle":"balance-100-whey-protein-2-8kg-w-free-magnesium-complete-powder-1","updated_at":"2022-05-26T12:34:07-06:00","published_at":"2022-05-16T17:41:57-06:00","template_suffix":null,"status":"active","published_scope":"web","tags":"Protein Powders, Specials, Stacks and Packs, Whey Protein Blend (WPI\/WPC)","admin_graphql_api_id":"gid:\/\/shopify\/Product\/6979552313549","variants":[{"id":40875072585933,"product_id":6979552313549,"title":"Default Title","price":"700.00","sku":"","position":1,"inventory_policy":"deny","compare_at_price":null,"fulfillment_service":"manual","inventory_management":"shopify","option1":"Default Title","option2":null,"option3":null,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","taxable":true,"barcode":null,"grams":0,"image_id":null,"weight":0.0,"weight_unit":"kg","inventory_item_id":42969806831821,"inventory_quantity":0,"old_inventory_quantity":0,"requires_shipping":true,"admin_graphql_api_id":"gid:\/\/shopify\/ProductVariant\/40875072585933"}],"options":[{"id":8937193341133,"product_id":6979552313549,"name":"Title","position":1,"values":["Default Title"]}],"images":[{"id":30230589407437,"product_id":6979552313549,"position":1,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","alt":null,"width":2862,"height":2143,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0618\/4189\/9725\/products\/Definici_C3_B3n-del-producto-y-servicio_0bf23268-fee3-4b3b-a577-aeaa2336d6fc.png?v=1653590047","variant_ids":[],"admin_graphql_api_id":"gid:\/\/shopify\/ProductImage\/30230589407437"}],"image":{"id":30230589407437,"product_id":6979552313549,"position":1,"created_at":"2022-05-26T12:34:07-06:00","updated_at":"2022-05-26T12:34:07-06:00","alt":null,"width":2862,"height":2143,"src":"https:\/\/cdn.shopify.com\/s\/files\/1\/0618\/4189\/9725\/products\/Definici_C3_B3n-del-producto-y-servicio_0bf23268-fee3-4b3b-a577-aeaa2336d6fc.png?v=1653590047","variant_ids":[],"admin_graphql_api_id":"gid:\/\/shopify\/ProductImage\/30230589407437"}}}
If I have a Model class called "Product"
public class product
{
public long id { get; set; }
public string title { get; set; }
public string body_html { get; set; }
public string vendor { get; set; }
public string product_type { get; set; }
public DateTime created_at { get; set; }
public string handle { get; set; }
public DateTime updated_at { get; set; }
public DateTime published_at { get; set; }
public object template_suffix { get; set; }
public string status { get; set; }
public string published_scope { get; set; }
public string tags { get; set; }
public string admin_graphql_api_id { get; set; }
public List<Models.Producto.Variant> variants { get; set; }
public List<Models.Producto.Option> options { get; set; }
public List<Models.Producto.Images> images { get; set; }
public Models.Producto.Image image { get; set; }
}
I can see that you are using a wrong class to deserialize json, should be
var data=JsonConvert.DeserializeObject<Data>(json);
main classes (I highly recommend you to use a Pascal style for property names)
public partial class Data
{
[JsonProperty("product")]
public Product Product { get; set; }
}
public partial class Product
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("body_html")]
public string BodyHtml { get; set; }
[JsonProperty("vendor")]
public string Vendor { get; set; }
[JsonProperty("product_type")]
public string ProductType { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("handle")]
public string Handle { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("published_at")]
public DateTimeOffset PublishedAt { get; set; }
[JsonProperty("template_suffix")]
public object TemplateSuffix { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("published_scope")]
public string PublishedScope { get; set; }
[JsonProperty("tags")]
public string Tags { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
[JsonProperty("variants")]
public List<Variant> Variants { get; set; }
[JsonProperty("options")]
public List<Option> Options { get; set; }
[JsonProperty("images")]
public List<Image> Images { get; set; }
[JsonProperty("image")]
public Image Image { get; set; }
}
other classes
public partial class Image
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("alt")]
public object Alt { get; set; }
[JsonProperty("width")]
public long Width { get; set; }
[JsonProperty("height")]
public long Height { get; set; }
[JsonProperty("src")]
public Uri Src { get; set; }
[JsonProperty("variant_ids")]
public List<object> VariantIds { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
}
public partial class Option
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("values")]
public List<string> Values { get; set; }
}
public partial class Variant
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("product_id")]
public long ProductId { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("price")]
public string Price { get; set; }
[JsonProperty("sku")]
public string Sku { get; set; }
[JsonProperty("position")]
public long Position { get; set; }
[JsonProperty("inventory_policy")]
public string InventoryPolicy { get; set; }
[JsonProperty("compare_at_price")]
public object CompareAtPrice { get; set; }
[JsonProperty("fulfillment_service")]
public string FulfillmentService { get; set; }
[JsonProperty("inventory_management")]
public string InventoryManagement { get; set; }
[JsonProperty("option1")]
public string Option1 { get; set; }
[JsonProperty("option2")]
public object Option2 { get; set; }
[JsonProperty("option3")]
public object Option3 { get; set; }
[JsonProperty("created_at")]
public DateTimeOffset CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTimeOffset UpdatedAt { get; set; }
[JsonProperty("taxable")]
public bool Taxable { get; set; }
[JsonProperty("barcode")]
public object Barcode { get; set; }
[JsonProperty("grams")]
public long Grams { get; set; }
[JsonProperty("image_id")]
public object ImageId { get; set; }
[JsonProperty("weight")]
public long Weight { get; set; }
[JsonProperty("weight_unit")]
public string WeightUnit { get; set; }
[JsonProperty("inventory_item_id")]
public long InventoryItemId { get; set; }
[JsonProperty("inventory_quantity")]
public long InventoryQuantity { get; set; }
[JsonProperty("old_inventory_quantity")]
public long OldInventoryQuantity { get; set; }
[JsonProperty("requires_shipping")]
public bool RequiresShipping { get; set; }
[JsonProperty("admin_graphql_api_id")]
public string AdminGraphqlApiId { get; set; }
}
try this:
var response = JsonConvert.DeserializeObject<Dictionary<<string, string>>(deserialize);
string value = response["key"];
I want to make the simple application to use with my PBX. I want to manage the providers and Lines tat connected to my PBX. The main objects are Provider and Line. Every provider has some lines connected to it. Lines can be added or deleted enabled or disabled and reserved. I want to keep the status of lines and history of actions in one table. Does this right solution or I need to create another table for history?
public class Line
{
public int LineId { get; set; }
public Provider Provider { get;set;}
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
}
What is the best solution for the code first model creating in the logic like that? Maybe I need to add some additional fields?
You need another table for history. Something like:
public class Line
{
public int LineId { get; set; }
public virtual Provider Provider { get; set; }
public string Login { get; set; }
public string Pass { get; set; }
public string Domain { get; set; }
public string ProjectOwner { get; set; }
public string Project { get; set; }
public string OutNumber { get; set; }
public string InNumber { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Human TechGuy { get; set; }
public Human Manager { get; set; }
public virtual ICollection<LineStatusHistory> ChangeHistory { get; } = new HashSet<LineStatusHistory>();
}
public class LineStatusHistory
{
[Key( )]
public int LineId { get; set; }
[Key()]
public int LineStatusHistoryId { get; set; }
public virtual Line Line { get; set; }
public DateTime ChangeDate { get; set; }
public string Change { get; set; }
}
public class Provider
{
public int ProviderId { get; set; }
public string Name { get; set; }
public string OurSideData { get; set; }
public string TheirSideData { get; set; }
public string Description { get; set; }
public DateTime Begin_datetime { get; set; }
public DateTime End_datetime { get; set; }
public Currency Currency { get; set; }
public virtual ICollection<Line> Lines { get; } = new HashSet<Line>();
}
I'm faced wiht an error, and I don't know how to correct it. I don't understand why it comes.
System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
The exception comes when I try to deserialize a JSON into objects that I created. (The second line code is throwing the exception)
List<Connection> connections = new List<Connection>();
var root = JsonConvert.DeserializeObject<RootObject2>(content);
connections = root.connections;
Here is the JSON that I want to use:
http://transport.opendata.ch/v1/connections?from=lausanne&to=fribourg
And here are my objects:
public class RootObject2
{
public List<Connection> connections { get; set; }
}
public class Connection
{
public Stop from { get; set; }
public Stop to { get; set; }
public string duration { get; set; }
public int? transfers { get; set; }
public Service service { get; set; }
public List<string> products { get; set; }
public int? capacity1st { get; set; }
public int? capacity2nd { get; set; }
public List<Section> sections { get; set; }
}
public class Stop
{
public Station station { get; set; }
public DateTime arrival { get; set; }
public int? arrivalTimestamp { get; set; }
public string departure { get; set; }
public int? departureTimestamp { get; set; }
public int? delay { get; set; }
public string platform { get; set; }
public Prognosis prognosis { get; set; }
public string realtimeAvailability {get; set;}
public Station location { get; set; }
}
public class Station
{
public string id { get; set; }
public string name { get; set; }
public int? score { get; set; }
public Coordinate coordinate { get; set; }
public double? distance { get; set; }
}
public class Service
{
public string regular { get; set; }
public string irregular { get; set; }
}
public class Section
{
public Journey journey { get; set; }
public Walk walk { get; set; }
public Stop departure { get; set; }
public Stop arrival { get; set; }
}
public class Journey
{
public string name { get; set; }
public string category { get; set; }
public string subcategory { get; set; }
public int? categoryCode { get; set; }
public string number { get; set; }
public string #operator { get; set; }
public string to { get; set; }
public List<Stop> passList { get; set; }
public int? capacity1st { get; set; }
public int? capacity2nd { get; set; }
}
public class Walk
{
public string duration { get; set; }
}
public class Prognosis
{
public string platform { get; set; }
public string arrival { get; set; }
public string departure { get; set; }
public int? capacity1st { get; set; }
public int? capacity2nd { get; set; }
}
public class Coordinate
{
public string type { get; set; }
public double x { get; set; }
public double y { get; set; }
}
The deserialization is on a try and catch block, and nothing is catched.
Also I am doing this on Android.
Based on json data.
Arrival property in Stop class should be DateTime?.
Table 1: Articles
Table 2: ArticleCategories
how do I represent the relationship between the two tables which is a 1->1 relationship:
I can do the following, but I'm not sure it's the correct way :
public class Article
{
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public ArticleCategory Category { get; set; }
}
public class ArticleCategory
{
public int CategoryIndex { get; set; }
public string Name { get; set; }
}
By convention, Code First expects an Id property for each class/table. Then you can do something like this:
public class Article
{
public int Id { get; set; }
public int ArticleIndex { get; set; }
public int Category { get; set; }
public Guid User { get; set; }
public int Parent { get; set; }
public int Level { get; set; }
public int Order { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateExpires { get; set; }
public bool Show { get; set; }
public string Title { get; set; }
public string TitleHtml { get; set; }
public string Content { get; set; }
public string ContentHtml { get; set; }
public string ShortTitle { get; set; }
public int ArticleCategoryId { get; set; }
public virtual ArticleCategory ArticleCategory { get; set; }
}
public class ArticleCategory
{
public int Id { get; set; }
public int CategoryIndex { get; set; }
public string Name { get; set; }
public virtual ICollection<Article> Articles { get; set; }
}
Note the virtual keyword. EF Code First needs this so it can perform its magic behind the scenes.
Now, if you are working with an Article, you can get all it's category info by doing article.ArticleCategory, and if you have an ArticleCategory you can find out what article it refers to with articleCategory.Articles.Single().
For more info, see this article by Scott Gu:
http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx