Entity Framework: One to many with the same class - c#

I have the following requirement: There are basically two classes Object and Attribute. I want to model the database in such a way that one Object can have many Attributes which is easy to do. But the othe requirement is that one Object can also have many other Objects. How would that be possible to implement and what do I need to do in my Model Classes to achieve this?
So far I have this:
public class Object
{
public int ID { get; set; }
public Attribute Attribute { get; set; }
}
public class Attribute
{
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public Object Object { get; set; }
}

I believe you can just add new property to your object class, that targets the object you want, so something like this:
public class Object
{
public int ID { get; set; }
public Attribute Attribute { get; set; }
public Object Object { get; set; }
}
public class Attribute
{
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public Object Object { get; set; }
}
but from the code provided, your Object class references just one instance of the Attribute class, so you dont have 1:N relation as you said you want, but rather 1:1! If you want the Object class to point to several Attributes and several Object Instances, the code should be something like this:
public class Object
{
public int ID { get; set; }
public Object ParentObject { get; set; }
}
public class Attribute
{
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public Object ParentObject { get; set; }
}
EDIT: https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

I guess what your looking for is Many-to-many relationships for the Database.
You could structure it like this, which will give you one extra table.
public class Object
{
public int ID { get; set; }
}
public class Attribute
{
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set; }
}
public class ObjectAndAttribute
{
public int id { get; set; }
public Attribute Attribute { get; set; }
public Object Object { get; set; }
}

Related

Dynamic mapping of properties

I am interested if there is any way to dynamically map or add a child's object properties to the parent? For example, I have the following classes:
public class Parent {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child> Children { get; set; }
}
public class Child {
public int Id { get; set; }
public string Name { get; set; }
}
And I will need to send from the back-end to the front-end in the following format:
public class Dto {
public int ParentId { get; set; }
public string ParentName { get; set; }
public string Child1Name { get; set; }
public string Child2Name { get; set; }
public string Child3Name { get; set; }
}
keeping in mind that the parent can have a different number of children objects.I need this information in this format because it will be displayed in a grid (see image below). Can something like this be achieved and how?

Automapper configuration falls short on partial class?

I have following AccountDetailRecord class to be mapped to the AccountDetailDto with the following configuration but somehow I am missing something in mapping the partial classes.
services
.AddSingleton(new MapperConfiguration(cfg =>
{
cfg.CreateMap<AccountDetailRecord, AccountDetailDto>().ReverseMap();
cfg.CreateMap<AddressRecord, AccountDetailDto.AddressDto>().ReverseMap();
}).CreateMapper())
source:
internal class AccountDetailRecord
{
public Guid Id { get; set; }
public string EstablishmentNumber { get; set; }
public string Name { get; set; }
}
internal class AddressRecord
{
public Guid Id { get; set; }
public string City { get; set; }
public string County { get; set; }
public int AddressTypeId { get; set; }
public string AddressTypeDescription { get; set; }
}
destination:
public partial class AccountDetailDto
{
public Guid Id { get; set; }
public string EstablishmentNumber { get; set; }
public string Name { get; set; }
public List<AddressDto> Addresses { get; set; }
}
public partial class AccountDetailDto
{
public class AddressDto
{
public Guid Id { get; set; }
public string City { get; set; }
public string County { get; set; }
public AddressTypeDto AddressType { get; set; }
}
}
public partial class AccountDetailDto
{
public class AddressTypeDto
{
public int Id { get; set; }
public string Description { get; set; }
}
}
but in the result the values of the AddressType.
what am I missing in configuration?
According to your classes properties, what you need is to reverse the mapping, so AutoMapper will be able to unflatten your AddressTypeId and AddressTypeDescription source properties into an AddressTypeDto object.
As the documentation states:
Unflattening is only configured for ReverseMap. If you want
unflattening, you must configure Entity -> Dto then call ReverseMap to
create an unflattening type map configuration from the Dto -> Entity
So you simply need to swap your mapping declaration to:
cfg.CreateMap<AccountDetailDto.AddressDto, AddressRecord>().ReverseMap();

How to map ICollection<Type> to extending class

I have created a data model that looks like this:
// Base data item class
public class DataItem
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[Required]
public int DataItemGroupID { get; set; }
public virtual DataItemGroup DataItemGroup { get; set; }
}
// Extending data item class
public class SpecificDataItem : DataItem
{
public string ExtraField { get; set; }
}
// Class for grouping of data items that may be extended or not.
public class DataItemGroup
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<DataItem> DataItems { get; set; }
public virtual ICollection<ExtendedDataItem> ExtendedDataItems { get; set; }
}
My issues is that when I use Entity Frameworks lazy loading, DataItemGroup.DataItems is populated with all items, including my ExtendedDataItems, but DataItemGroup.ExtendedDataItems is always empty.
I suspect that I am missing a mapping of some sort, but I don't know what I should be looking for. Any ideas on how to get DataItemGroup to load both the DataItems and ExtendedDataItems collections through lazy loading?
You could create just one relationship using the base class (DataItem):
public class DataItemGroup
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<DataItem> DataItems { get; set; }
}
When you need to filter the elements for an specific type that inherit from DataItem, you could use OfType<T> method , for example:
var dItemGroup= dc.DataItemGroups.FirstOrDefault();
var extendedDataItems=dItemGroup.DataItems.OfType<ExtendedDataItem>();

Inheriting from Multiple Classes in C#

I have a class Annotations, and 2 classes that inherit from it, HousingAnnotations and AutoAnnotations
public class Annotations
{
public string original_posting_date { get; set; }
public string price { get; set; }
}
public class HousingAnnotations : Annotations
{
public string laundry_on_site { get; set; }
public string condo { get; set; }
public string w_d_in_unit { get; set; }
public string street_parking { get; set; }
public string w_d_hookups { get; set; }
public string bedrooms { get; set; }
}
public class AutoAnnotations : Annotations
{
public string vin { get; set; }
public string year { get; set; }
}
What I would like to do is allow for population of either Auto or HousingAnnotation without specifying which type, for instance
AnnotationHelper annotation = new AnnotationHelper{bedrooms = "2", vin = "123"};
or something like that. Since I cannot inherit from both classes, how can I get around this? I know there are workarounds using interfaces, but I do not want to have to call out each property in my AnnotationHelper class. What are some ways to get around this?

List<> failing to serialize to JSON

I am using the following C# class in my ASP.NET MVC project:
public class ZoneModel {
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public bool LineFault { get; set; }
public bool Sprinkler { get; set; }
public int Resistance { get; set; }
public string ZoneVersion { get; set; }
List<DetectorModel> Detectors { get; set; }
}
In one of my Controllers, I have an Action with a return type of JsonResult, from which I return a list of ZoneModel objects (populated from the database). The Detectors property contains data, but when I return the list from the controller using return Json(viewModel);, the list of detectors is missing from the serialized response.
Why is the Detectors property not serializing to JSON?
Just to clarify my comment. Properties need to be declared as Public members in order to be Serialized by either JSON.NET or the built-in JavaScriptSerializer.
public class ZoneModel {
public int Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public bool LineFault { get; set; }
public bool Sprinkler { get; set; }
public int Resistance { get; set; }
public string ZoneVersion { get; set; }
// this property will not be serialized since it is private (by default)
List<DetectorModel> Detectors { get; set; }
}

Categories

Resources