asp.net json .net objects - asp.net

I have this class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestConnect
{
public class UserInfo
{
public string id { get; set; }
public string name { get; set; }
public string picture { get; set; }
public string bio { get; set; }
public string quotes { get; set; }
//public HometownInfo from { get; set;
public From hometown { get; set; }
public string relationship { get; set; }
public string gender { get; set; }
public List<Education> data { get; set; }
}
public class Education
{
public From school { get; set; }
public From year { get; set; }
//public From [] concentration { get; set; }
public string type { get; set; }
}
}
public class HometownInfo
{
public string id { get; set; }
public string homename { get; set; }
}
Now trying to do
url = "https://graph.facebook.com/me?fields=education&access_token=" + oAuth.Token;
json = oAuth.WebRequest(oAuthFacebook.Method.GET, url, String.Empty);
Education edu = js.Deserialize<Education>(json);
foreach(Education_history edu_history in Education.data) gives an error
Object reference not set to an
instance of an object. Description: An
unhandled exception occurred during
the execution of the current web
request
Thanks
User

Have you tried setting breakpoints at the various steps in your code and seeing where it might be?
somewhere else in your code you must be declaring url and json?
does js.Deserialize() create a new object or are you missing this
Education edu = new Education();
Also if its in a web application make sure that your code is placed in the correct page events so that they happen in the correct order

Related

Refit me showing an error when initializing

I am trying to consume a service using Refit and I have the following problem. I was looking at the documentation but I can't find a solution and I don't know if I should do anything else.
This is my model
public partial class Global
{
[JsonProperty("updated")]
public long Updated { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("countryInfo")]
public List<CountryInfo> CountryInfo { get; set; }
}
My second class:
public partial class CountryInfo
{
[JsonProperty("_id")]
public long? Id { get; set; }
[JsonProperty("iso2")]
public string Iso2 { get; set; }
[JsonProperty("iso3")]
public string Iso3 { get; set; }
[JsonProperty("lat")]
public double Lat { get; set; }
[JsonProperty("long")]
public double Long { get; set; }
[JsonProperty("flag")]
public Uri Flag { get; set; }
}
Application with Refit
Link : https://corona.lmao.ninja/v2/countries
[Get("/v2/countries")]
Task<CountryInfo> GetCountry();
In my viewModel
private async Task GetTaskInterf()
{
var webApp = RestService.For<ICovidInterface>("https://corona.lmao.ninja");
var result = await webApp.GetGlobalCountry();
}
At the time of initialization

How can i create one list from tow related table

Hi I have model with to table and a one to many relationship between
Order and status
i want to show a list with all of properties in order and the last Title in status according to Order id in order
how could i get it?
thank you
this is my code :
namespace Pur.Models
{
using System;
using System.Collections.Generic;
public partial class Order
{
public int OrdeId { get; set; }
public Nullable<int> UserID { get; set; }
public Nullable<System.DateTime> OrderDate { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public string Unit { get; set; }
public string Priority { get; set; }
public string Department { get; set; }
public virtual ICollection<Status> Status { get; set; }
}
}
and the other:
namespace Pur.Models
{
using System;
using System.Collections.Generic;
public partial class Status
{
public int StatusId { get; set; }
public string Title { get; set; }
public int OrderID { get; set; }
public Nullable<System.DateTime> StatusDate { get; set; }
public virtual Order Order { get; set; }
}
You can create join query and then pass it to a new model
var query = your query
var Lists = db.Database.SqlQuery<OrderModel>(query);

AspNet EF6 - Entity type has no key defined

So I changed up my databases and remade them. I followed the EF6 tutorial but encountered this error when trying to create a controller with views. I did some research on the error and people said to add data annotations and I tried [Key] but I keep getting the same error. Not sure if i've forgotten anything? Thanks!
"There was an error running the selected code generator: 'Unable to retrieve metadata for 'LinkDB.Models.Identifier.' Unable to determine the principal end of an association between the type 'LinkDB.Models.Identifier' and 'LinkDB.Models.Identity'. The principal end of this association must be explicity configured using either the relationship fluent API or data annotation.'
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace LinksDB.Models
{
public class Identity
{
[Key]
public int ID { get; set; }
public int IdentifierID { get; set; }
public string contact { get; set; }
public string contactname { get; set; }
public string price { get; set; }
public virtual ICollection<Link> Links { get; set; }
public virtual Identifier Identifiers { get; set; }
public virtual Metric Metrics { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace LinksDB.Models
{
public class Identifier
{
[Key]
public int ID { get; set; }
public string domain { get; set; }
public virtual ICollection<Link> Links { get; set; }
public virtual Identity Identitys { get; set; }
public virtual Metric Metrics { get; set; }
}
}
using LinksDB.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace LinksDB.DAL
{
public class LinkData : DbContext
{
public LinkData() : base("LinkData")
{
}
public DbSet<Identifier> Identifiers { get; set; }
public DbSet<Identity> Identitys { get; set; }
public DbSet<Link> Links { get; set; }
public DbSet<Metric> Metrics { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
OK, if you want a 1:1 relationship between Identity and Identifier those models should look like below where the child (Indentifier) uses the IdentityId as both it's PK and FK. You can also do this with fluent code. Start with this and then add in your Metric and Links relationships.
public class Identity
{
[Key]
public int ID { get; set; }
public string contact { get; set; }
public string contactname { get; set; }
public string price { get; set; }
public virtual Identifier Identifier { get; set; }
}
public class Identifier
{
[Key, ForeignKey("Identity")]
public int IdentityID { get; set; }
public string domain { get; set; }
public virtual Identity Identity { get; set; }
}
Example here

How to Update model and database with code first approach in Asp.net MVC

I'm new to mvc. I've creted an MVC app, in which i have used code first approach. Right now i have two tables Deal and Comment. Now i want to add a new table Category in the database and new column categoryId in Deal table.
How i can update database and model?
I'm using Sql Server 2008 R2 for Database.
I've following structure of class:
namespace FBWebApp.Models
{
public class Deal
{
public int ID { get; set; } // ID
public string Title { get; set; } // Titolo del deal
public string Description { get; set; } // Descrizione dell'annuncio
public string FacebookUID { get; set; } // UID facebook dell'utente
public string Visibility { get; set; } // Visibility
public string Category { get; set; }
public int Option1 { get; set; }
public int Option2 { get; set; }
public int Option3 { get; set; }
public int Option4 { get; set; }
public string PhotoURL { get; set; } // URL of the facebook photo profile
public string Name { get; set; } // Name of the user
public string ProfileUrl { get; set; } // URL of the facebook profile
public string Photo1 { get; set; } // URL of the Photo1 (local )
public string Photo2 { get; set; }
public string Photo3 { get; set; }
public string Photo4 { get; set; }
public string Photo5 { get; set; }
}
public class Comment
{
[Key]
public int CommentId { get; set; }
public string CommentText { get; set; }
public int ID { get; set; }
[ForeignKey("ID")]
public Deal DelNav { get; set; }
}
public class DealDBContext : DbContext
{
public DealDBContext() : base("DealDBContext") { }
public DbSet<Deal> Deals { get; set; }
public DbSet<Comment> Comments { get; set; }
}
}
Try using 'update-database -force -verbose' in the Package Manager Console.
If it doesn't work, modify the migration but typing 'add-migration somename' and it will apear in the Migrations folder.
If you are new to MVC and EF, definitely check out this tutorial. It explains all about that and everything else you need to know:
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m1-intro&mode=live&clip=0&course=mvc4-building
first add your model :
public class Category
{
public int ID { get; set; }
public int cateName { get; set; }
}
in Deal class :
public class Deal
{
//..
[ForeignKey("CatId")]
public virtual Category Category { get; set; }
}
after Enable Migration you should use this command in console manager to update your database :
update-database

Entity framework 5: net40

I'm developing a web application with ASP.NET 4.0.
I have a database SQL Server 2005 and I'm using Entity Framework 5 connecting to it.
public class ArchivioClienti : DbContext
{
public ArchivioClienti()
: base("ICAMConnection")
{
}
public DbSet<ClientiProspect> clienti { get; set; }
}
[Table("pvw_clienti_prospect")]
public class ClientiProspect
{
[Key]
public string tipologia { get; set; }
public string cod_ppro { get; set; }
public string rag_soc { get; set; }
public string indirizzo { get; set; }
public string cap { get; set; }
public string citta { get; set; }
public string prov { get; set; }
public string nazione { get; set; }
public string telefono { get; set; }
public string fax { get; set; }
public string part_iva { get; set; }
public string cod_fisc { get; set; }
public string note { get; set; }
public string e_mail { get; set; }
public string cod_ppro_anag { get; set; }
public string cod_vage { get; set; }
public string cod_visp { get; set; }
public string fonte { get; set; }
public string cod_vzon { get; set; }
public decimal perc_provv { get; set; }
public string flag_stato { get; set; }
public string cod_anag { get; set; }
public string gg_chiusura { get; set; }
public DateTime? data_ins { get; set; }
public string cod_canale { get; set; }
}
I'm trying to get data from a table. This isn't a really table but only a view.
When I load a query with LINQ,
var result = from u in dbClienti.clienti
select u;
return result.ToList()
I receive the correct number of results (the same that i receive with the same query in SQL Server Management studio), but with wrong fields: the first one is replicated several times, the second one the same, etc.
Why is it working in this way? Are there any problems on Entity Frameworks with database's views?
I assume your view is correctly configured and returning the correct results when executed outside of Entity Framework?
There's no problem with using a view instead of a table in Entity Framework. I would run SQL Profiler and see what E.F is doing when executing the query.

Resources