I want to have one class that has common information; CommonData class will be used in all tables:
public class CommonData
{
public string CreatedBy { get; set; } = "";
public string ModifiedBy { get; set; } = "";
public Nullable<DateTime> CreateDate { get; set; } = DateTime.Now;
public Nullable<DateTime> ModifiedDate { get; set; } = DateTime.Now;
}
Person class:
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
}
I know I could do this if I put all of the data in a common class
public class CommonClass
{
public string Id { get; set; }
public string CreatedBy { get; set; } = "";
public string ModifiedBy { get; set; } = "";
public Nullable<DateTime> CreateDate { get; set; } = DateTime.Now;
public Nullable<DateTime> ModifiedDate { get; set; } = DateTime.Now;
}
If I inherit from the common class
public class Person : CommonClass
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
}
The problem is that I have looking at the table it creates and I feel that their must be a better way to do this. When the table is created it puts all of the CommonData in the first fields of the table then it put all of the specific table data. It makes it a mess.
I have tried this to make this but this didn't work I like to have this data in a CommonData object connected to the Person class. This would be the ideal scenario but I don't know how to achieve this.
public class Person
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string ZipCode { get; set; }
public string Phone { get; set; }
public CommonData CommonData {get;set;}
}
Related
I want to parse a JSON data dynamically and read its key and their values without serializing it in a concrete class because the JSON data can be different time to time. But for the initial testing one sample data is like this . In this data I want to get property values and if the JSON data aslo contains complex structure I also want to read child property values also How to do that in C#.
{"id":4877891717,"email":"rvp#insync.com","accepts_marketing":true,"created_at":"2017-03-24T08:39:56+01:00","updated_at":"2017-04-10T09:40:42+02:00","first_name":"Robin","last_name":"Van Persie","orders_count":8,"state":"disabled","total_spent":"2320.00","last_order_id":4434634693,"note":"","verified_email":true,"multipass_identifier":null,"tax_exempt":true,"phone":"+3225551212","tags":"","last_order_name":"#1116","addresses":[{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}],"default_address":{"id":5143111941,"first_name":"Robin","last_name":"Van Persie","company":"InSync","address1":"CB 28, El Solo Road","address2":"CB 28, El Solo Road","city":"Brussels","province":"EU","country":"Belgium","zip":"123456","phone":"12345678","name":"Robin Van Persie","province_code":null,"country_code":"BE","country_name":"Belgium","default":true}}
I am trying this way. But after geiing tiken what I will do.
foreach (JObject token in jObject.Children())
{}
Thanks
You can use NewtonsoftJson library to parse without creating concrete class
dynamic parseJson = JsonConvert.DeserializeObject("your json");
and get the value using below code like...
string Id=parseJson.Id.Value
I have tested it and it's working for me
you have two options(as far as i know). you can generate a class acording to your json string then parse the result in it. like this:
public class Address
{
public long id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string zip { get; set; }
public string phone { get; set; }
public string name { get; set; }
public object province_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public bool #default { get; set; }
}
public class DefaultAddress
{
public long id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string company { get; set; }
public string address1 { get; set; }
public string address2 { get; set; }
public string city { get; set; }
public string province { get; set; }
public string country { get; set; }
public string zip { get; set; }
public string phone { get; set; }
public string name { get; set; }
public object province_code { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public bool #default { get; set; }
}
public class RootObject
{
public long id { get; set; }
public string email { get; set; }
public bool accepts_marketing { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public int orders_count { get; set; }
public string state { get; set; }
public string total_spent { get; set; }
public long last_order_id { get; set; }
public string note { get; set; }
public bool verified_email { get; set; }
public object multipass_identifier { get; set; }
public bool tax_exempt { get; set; }
public string phone { get; set; }
public string tags { get; set; }
public string last_order_name { get; set; }
public List<Address> addresses { get; set; }
public DefaultAddress default_address { get; set; }
}
then parse it(using json.net) like this:
var jObject=JsonConvert.DeserializeObject<RootObject>(responseString);
or you can use dynamic object and deal with json result at runtime. like this:
var jObject=JsonConvert.DeserializeObject(responseString);
foreach (JObject token in jObject.Children)
You can use NewtonsoftJson library to parse data easily
dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata);
foreach (var product in x) {
Messagebox.show(product.data.ToString());
}
JSON response string send by client:
{ "{\"user_id\":\"\",\"school_id\":\"1\",\"user_name\":\"pfirstname1#example.com\",\"firstname\":\"pfirstname1\",\"surname\":\"psurname1\",\"type\":\"P\",\"phoneno\":\"1446863\",\"mobileno\":\"3614632\",\"address\":\"mehdipatnam\",\"relation\":\"Father\",\"date_created\":\"\",\"student_dto\":": { "{\"student_id\":0,\"user_id\":0,\"firstname\":\"sfirstname1\",\"surname\":\"ssurname1\",\"rollno\":1,\"class_id\":\"2\",\"school_id\":\"1\",\"address\":\"mehdipatnam\",\"remarks\":\"\",\"date_created\":\"\"}]}": "" }}
Then to deserialize this I'm doing this step:
dynamic data = new JavaScriptSerializer().Deserialize(response.ToString(), typeof(object));
Then when I look in to the debugger I can see:
[0] {[{"user_dto":{"user_id":"","school_id":"1","user_name":"pfirstname1#example.com","firstname":"pfirstname1","surname":"psurname1","type":"P","phoneno":"1446863","mobileno":"3614632","address":"mehdipatnam","relation":"Father","date_created":"","student_dto":, System.Collections.Generic.Dictionary`2[System.String,System.Object]]} System.Collections.Generic.KeyValuePair<string,object>
But how do I access the values?
I'm going into this assuming that the mess you posted as JSON should actually be the following after all the \" and erroneous quotes are removed.
{
"user_id":"",
"school_id":"1",
"user_name":"pfirstname1#example.com",
"firstname":"pfirstname1",
"surname":"psurname1",
"type":"P",
"phoneno":"1446863",
"mobileno":"3614632",
"address":"mehdipatnam",
"relation":"Father",
"date_created":"",
"student_dto":
{
"student_id":0,
"user_id":0,
"firstname":"sfirstname1",
"surname":"ssurname1",
"rollno":1,
"class_id":"2",
"school_id":"1",
"address":"mehdipatnam",
"remarks":"",
"date_created":""
}
}
If that's the case, then json2csharp.com gives the following as the classes to deserialize into:
public class StudentDto
{
public int student_id { get; set; }
public int user_id { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public int rollno { get; set; }
public string class_id { get; set; }
public string school_id { get; set; }
public string address { get; set; }
public string remarks { get; set; }
public string date_created { get; set; }
}
public class RootObject
{
public string user_id { get; set; }
public string school_id { get; set; }
public string user_name { get; set; }
public string firstname { get; set; }
public string surname { get; set; }
public string type { get; set; }
public string phoneno { get; set; }
public string mobileno { get; set; }
public string address { get; set; }
public string relation { get; set; }
public string date_created { get; set; }
public StudentDto student_dto { get; set; }
}
Now you should be able to deserialize directly into that type.
new JavaScriptSerializer().Deserialize<RootObject>(response.ToString());
I have a class customer that contains address properties, phone properties and fax properties, but I want to take off the address, phone properties to complex types. Does properties are already in the database as columns.
[Table("tblCustomer")]
public partial class Customer : Entity
{
[Key]
public int CustomerID { get; set; }
[StringLength(10)]
public string CustomerCode { get; set; }
[StringLength(60)]
public string AddressLine1 { get; set; }
[StringLength(70)]
public string AddressLine2 { get; set; }
[StringLength(35)]
public string City { get; set; }
[StringLength(2)]
public string State { get; set; }
[StringLength(10)]
public string ZipCode { get; set; }
[StringLength(15)]
public string PhoneNo { get; set; }
[StringLength(3)]
public string PCountryCode { get; set; }
[StringLength(3)]
public string PAreaCode { get; set; }
[StringLength(7)]
public string PPhoneNo { get; set; }
[StringLength(3)]
public string FCountryCode { get; set; }
[StringLength(3)]
public string FAreaCode { get; set; }
[StringLength(7)]
public string FaxNumber { get; set; }
[StringLength(3)]
public string CountryCode { get; set; }
}
how to refactor this into:
[Table("tblCustomer")]
public partial class Customer : Entity
{
[Key]
public int CustomerID { get; set; }
[StringLength(10)]
public string CustomerCode { get; set; }
public Address Address { get; set; }
public Phone Phone { get; set; }
public Phone Fax { get; set; }
}
without conflicting with what already exist in the database?
Your address class should be annotated with the ComplexType attribute and you'd need to explicitly map the column names:
[ComplexType]
public class Address
{
[Column("street")]
public string Street {get; set;}
// Other properties
}
I'm trying to map two entities connected via linker table.
OrganizationVM = Organization -|---< AddressLinker >---|- Address
one to many many to one
My goal is to get Organization ViewModel with State property from Address entity.
string state = OrganizationVM.State
Does anyone know how do I do it?
Thanks!
public class OrganizationVM : BaseViewModel
{
[Display(Name = "Parent ID")]
[Required]
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public string State { get; set; }
public string StateAbr { get; set; }
public string City { get; set; }
}
public class AddressLinker
{
public int ID {get; set;}
public int OrganizationID { get; set; }
public int AddressID { get; set; }
}
public class Address
{
public int ID {get; set;}
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}
public class Organization
{
public int ID {get; set;}
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public bool StructuralNode { get; set; }
public string InternalName { get; set; }
public string ExternalShortName { get; set; }
public string ExternalFullName { get; set; }
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}
I want to create my first application using the EF 4.1 Code First Model. I want to model a magazine subscription but just want to check that my POCO classes are fit for purpose.
The following are my classes. Am I missing anything?
Should Customer be a member of Subscription or should it be just that List be a member of Customer?
Thanks.
public class Subscription
{
public int SubscriptionID { get; set; }
public string CardNumber { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal NetPrice { get; set; }
public decimal Tax { get; set; }
public decimal Discount { get; set; }
public string PromotionalCode { get; set; }
public Customer Customer{ get; set; }
}
public class Customer {
public int CustomerID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public PostalAddress PostalAddress { get; set; }
public string EmailAddress { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public DateTime DateOfBirth { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public bool Valid { get; set; }
public IList<Subscription> Subscriptions { get; set; }
public bool MailMarketing { get; set; }
public bool PartnerMailMarketing { get; set; }
}
public class PostalAddress
{
public int ID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string Region { get; set; }
public string Country { get; set; }
}
If a Customer have X suscriptions, every suscription should have the customerID so you need that in your Suscription class (public int CustomerID {get; set;}).
OTOH, I think that you have to put that Customer reference as virtual and the suscription one (I don't know why).
Maybe Im wrong but that works for me.
Anyway, what's your problem?
Your models look correct. You don't necessarily need the Customer property in the Subscription class, but it does help if you want to retrieve a specific Subscription and then want to find the customer that is tied to that subscription. In that instance you can then do var customer = mySub.Customer instead of querying for a customer with the specific subscription id.