Serialization of Generic Classes - c#

I have a class named "City"
public class City
{
public int ID {get; set;}
public int StateID {get; set;}
public int CountryID{get; set;}
public string Name{get; set;}
.......
}
and i have an asp.net page named CityAdd.aspx, in this page i want to create a collection of city class that can be store in the viewstate.
Is it possible to make a Generic Collection Serializable?

do as below, add Serializable attribute
[Serializable]
public class City
{
}

Related

AutoMapper with nested objects and container class

I'm developing an API with .NET core and using AutoMapper.
all the API responses will be wrapped with a data element in the JSON response like below example
GET User
{
"data" {
"id" : 1,
"user_name": "abc"
"countryr" : {
"id" : 1348,
"code" : "USA"
}
}
}
So we have an entity for User and Country
public partial class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public virtual Country country { get; set; }
}
public partial class Country
{
public int Id { get; set; }
public string Code{ get; set; }
}
To map entities to DTO we have below reponseDTO
public class GetUserDTO {
public User data {get; set;} // To wrap reponse with data
}
public class UserDto {
public int id {get; set;}
public String user_name {get; set;}
public Country country {get; set;}
}
public class CountryDto {
public int id {get; set;}
public String code {get; set;}
}
As per my understanding , I should map the entity User to the UserDTO and Country entity to CountryDTO but what about GetUserDTO class itself? it basically contains other entities so the class itself cannot be mapped to anything it acts like a container.
So below what I did so far which is not correct
public class UserProfile : AutoMapper.Profile
{
public MappingProfile()
{
CreateMap<User, GetUserDTO>();
CreateMap<User, UserDto>()
.ForMember(userDto => userDto.user_name, map => map.MapFrom(user => user.FirstName))
CreateMap<Country, CountryDto>();
}
}
Json response
{
"data" : null
}
What to do for such situations?
You should not refer your entity classes in your DTO classes. You might want to change the DTO as below.
public class GetUserDTO {
public UserDto data {get; set;} // To wrap reponse with data
}
public class UserDto {
public int id {get; set;}
public string user_name {get; set;}
public CountryDto country {get; set;}
}
public class CountryDto {
public int id {get; set;}
public string code {get; set;}
}
And then in the Mapper profile, you need to explicitly map each property as cases are different(AutoMapper will map without explicity map if the names are exactly the same. In your example, there is a case difference)
And then remove the below line from the MapperProfile as there is no mapping from the User class to GetUserDTO class. This is the reason you are not getting any output.
CreateMap<User, GetUserDTO>();
While getting the data, you should create a new instance of GetUserDTO class and set the property "data" from the result of mappaing of the User object.
This will get you the output.

EF Core - Hardcoded Foreign keys

I am trying to work out how to add a reference an object from within EF.
I little hard to explain so I thought I would share a dotnetfiddle example
https://dotnetfiddle.net/4n5Flk
public class ReportConfig
{
[Key]
public int ReportConfigKey {get; set;}
public int ConfigTypeKey {get; set;}
public int ConfigValue {get; set;}
[ForeignKey("ConfigTypeKey")]
public virtual <ConfigType> ConfigType {get; set;}
}
public class ConfigType
{
[Key]
public int ConfigTypeKey {get; set;}
//This can be
//Address
//Car etc etc.
public string ConfigName {get; set;}
}
public class Car
{
[Key]
public int CarId {get; set;}
//other items
}
public class Address
{
[Key]
public AddressId {get; set;}
}
In a nutshell I am trying to create a navigation property to either a car or an address based on the ConfigValue and the ConfigTypeKey
I was thinking to create 2 navigation properties "Car" and "Address" and for both add 2 foreign key's 1 to the object and 1 as a hardcoded reference to ConfigName as Car or Address
is this possible atall
Sorry for the poor explanation.

How to populate nested object in model?

I'm creating a database first web api using entity framework. I created my models by creating ADO.NET Entity Models that were populated automatically with the fields on the tables that I selected. I want to be able to take some of those fields and group them in a custom object so when the xml is displayed, they are displayed as a child node.
An example is if I had the class with the following attributes
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public string street{get; set;}
public string city{get; set;}
public string zip{get; set;}
}
I want it to be something like this instead
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public string address{get; set;}//where address contains street, city, and zip
}
How would I go about in implementing this new Person class and populating the data of the custom object with my dbcontext
It is impossible to change three string properties to the one string property. How you can divide Address property to the street city and zip for example in case when street has two words?
You can create Address class (with three properties) and use it your Person class. But you will have problems when you try to regenerate/update your model - in such case is better to use code first approach.
Belongs-to Relationship
I think what you're looking for is a relationship between Person and a new class called Address.
To reference your new Address class inside Person, you would do the following:
public class Person
{
...
public Guid AddressId { get; set; }
public Address { get; set; }
}
Entity Framework will then associate an Address with the given AddressId with a Person.
Inside Address you can do something like this:
public class Address
{
public string City { get; set; }
public string Street { get; set; }
...
}
Eager Loading
To make sure an Address is loaded when a Person is loaded, you can use Entity Framework's Eager Loading capabilities, such as:
context.Person.Include(x => x.Address)
Use enumerable List like
public class Person
{
public string firstname{get; set;}
public string lastname{get; set;}
public List addresses{ get; set; }
}
public class Address
{
public string street{get; set;}
public string city{get; set;}
public string zip{get; set;}
}

Editing variable-length collections with complex object

I would like to implement an Editor in ASP.NET MVC application for a Model with ICollection property in it.
public class Peson
{
public int Id {get; set;}
public int Name {get; set;}
public ICollection<PersonCode> {get; set;}
}
public class PersonCode
{
public int PersonId {get; set;}
public int Code {get; set;}
public int SubCode {get; set;}
}
In PersonCode class each SubCode depends on Code for example for Code 1 we can choose only SubCodes 2,3 and for Code 2 SubCodes 5,7
Each Code and SubCode has a description stored somewhere in database
So I need to edit this collection (add,delete,edit items in it) inside my Person object on a single Person Edit page and also provide dropdown lists for choosing Code and SubCode.

CustomAttribute with property that is a AttributeTarget property name as string

I want to change my CustomAttribute usage in this way, I'm using it to refer to the search parameters of an autocomplete dropdown
now:
[EntitySearchDropBox(Id="Id",SearchColumns="Name")]
public virtual class MyTarget
But i hate strings for defining entity properties and the best solution for me is passing AttributeTarget property without writing free strings.
[EntitySearchDropBox(Id=MyTarget.Id,SearchColumns=MyTarget.Name)]
public virtual class MyTarget
MyTarget example
public class MyTarget{
public int Id {get; set;}
public string Name {get; set;}
public string otherProperty { get; set; }
//etc..
}

Categories

Resources