I am wondering if it is possible to make one table related to many.
This is what I got now (working one-to-zero-or-one relation between Report and ReportHeader):
public class Report
{
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
[ForeignKey("ReportHeader")]
public int ReportHeaderId {get; set;}
}
public class ReportHeader
{
[Key, ForeignKey("Report")]
public int Id {get; set;}
public Report Report {get; set;}
}
At this point I want to add table named Style to Report BUT also to table ReportHeader. Thus, the relations would look like this:
Report
|--ReportHeader
| |-- Style
|
|-- Style
After that the classes should look like:
public class Report
{
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
public Style Style {get; set;}
[ForeignKey("ReportHeader")]
public int ReportHeaderId {get; set;}
[ForeignKey("Style")]
public int StyleId {get; set;}
}
public class ReportHeader
{
[Key, ForeignKey("Report")]
public int Id {get; set;}
[ForeignKey("Style")]
public int StyleId {get; set;}
public Report Report {get; set;}
public Style Style {get; set;}
}
This is so much fun... until it comes to think about the Style class. At this point I have no idea how to design it. Is that even possible to make that class be in two relations with different tables?
public class Style
{
// ???
//[Key, ForeignKey("Report"), ForeignKey("ReportHeader")]
public int Id {get; set;}
public ReportHeader ReportHeader {get; set;}
public Report Report {get; set;}
}
At this case you should to mask your desired relation: one-to-one as many-to-one:
public BaseClass
{
//indeed, collection always will have zero or one items
public virtual ICollection<Style> styles {get; set;}
[NotMapped]
public Style style {
get { return styles.FirstOrDefault(); }
set { styles.Add(value); };
}
}
public class Report : BaseClass
{
//other stuff...
}
public class ReportHeader : BaseClass
{
//other stuff...
}
public class Style
{
public int Id {get; set;}
public virtual Report report {get; set;}
[Index(IsUnique = true)]//to ensure that relation is exactly one-to-one
public int? reportId {get; set;}
public virtual ReportHeader reportHeader {get; set;}
[Index(IsUnique = true)]//to ensure that relation is exactly one-to-one
public int? reportHeaderId {get; set;}
}
Related
I have two class that you can see below:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public decimal SuggestionPrice {get; set;}
public List<SaleOrder> SaleOrders {get; set;} = new List<SaleOrder>();
}
public class SaleOrder
{
public int Id {get; set;}
public List<Product> Products {get; set;} = new List<Product>();
}
as you can see,this two class have Many-To-Many RealationShip to eachother.in Product class i have SuggestionPrice property that i want save multiple value in it and it's difference for each SaleOrder Class.
for example,Someone offers $ 1,000 in one SaleOrder and someone else offers $ 2,000 in another SaleOrder for same product and infinitely another suggestions that I want to save them all in database.
problem is i don't know how can i do this?how can i know witch SuggestionPrice is for witch SaleOrder Class?
To save in database You should create a SuggestionPrice table like this:
public class SuggestionPrice
{
public int Id {get;set}
public int ProductId {get;set;}
public decimal Price {get;set;}
public Product Product {get;set;}
}
and change product class to this:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public ICollection<SuggestionPrice> SuggestionPrices {get; set;}
}
to create a one to many relation between Product and SuggestionPrice.
I have a many-to-many relationship with EF Core 2.x.
I have created 3 classes:
public class Place
{
public string Name {get; set;}
public int Id {get; set;}
public ICollection<PlaceUser> Users {get; set;}
}
public class User
{
public string Name {get; set;}
public int Id {get; set;}
public ICollection<PlaceUser> Places {get; set;}
}
public class PlaceUser
{
public int UserId{get; set;}
public User User{get; set;}
public int PlaceId{get; set;}
public Place Place{get; set;}
}
public class PlaceDto
{
public string Name {get; set;}
public int Id {get; set;}
public ICollection<PlaceUserDto> Users {get; set;}
}
In my dbcontext, I set up the relationship. Everything works well.
But when I want to Map my Dto Place to my Place Object in my object place I have a recursivity and overflow exception:
I have:
Place
|-> Users
|-> User
|-> Place
|-> Users
|-> ...
I tried in my config of mapper to use depth but it's not working.
The only workaround I have found is:
if(place!=null && place.Users!=null)
{ // I set all the place.Users[i].Place = null; and place.Users[i].User=null;}
But it's an ugly solution and not convenient at all.
So which solution can I use?
Thanks,
I added the automapper config:
configuration.CreateMap<Place, PlaceDto>().MaxDepth(1);
https://stackoverflow.com/a/48824922/8101300
https://stackoverflow.com/a/57134333/8101300
CreateMap<Foo, Bar>().PreserveReferences();
I have an object called Product which have the attribute called ProductItem and tags.
public class Shop{
public string Id {get; set;}
public Product Products {get; set;}
}
public class Product{
public string Name {get; set;}
public string Id {get; set;}
public Tag Tags {get; set;}
public ProductItem ProductItems {get; set;}
public Shop ShopInformation {get; set;}
}
public class Tag{
public string Id {get; set;}
public string name {get; set;}
}
public class ProductItem{
public string Id {get; set;}
public Tag Tags {get; set;}
}
I would like to get all the tags in both Products and Tags from the datacontext
and I use this query
var result = from shop in dataContext.Shop
select new Response
{
Id = shop.Id,
Tags = (from product in dataContext.Product
where product.ShopInformation.Id == shop.Id
from productTag in
product.Tags.Union(product.ProductItems.SelectMany(s => s.Tags))
select productTag.Name).Distinct(),
};
However, the performance is pretty slow, and thus I would like to know if there is a way to collect all tags faster.
I have the following models (and corresponding DTOs):
public class Link
{
public int Id {get; set;}
public int FirstLinkId {get; set;}
public int SecondLinkId {get; set;}
public virtual Link FirstLink {get; set;}
public virtual Link SecondLInk {get; set;}
}
public class OtherObject
{
public int Id {get; set;}
public int LinkId {get; set;}
public string Name {get; set;}
public virtual Link Link {get; set;}
}
In my scenario, I can have a Link object where FirstLink and/or SecondLink can be null, references to other objects, or references to the same object.
Now I want to load an OtherObject entity from the db using EF. I load the entity itself and also the Link object associated with it. This is done perfectly by EF.
In this particular case, both FirstLink and SecondLink are the same as Link, therefore, when automapping from model to dto it just keeps on mapping into oblivion.
My mapping is:
Mapper.CreateMap<OtherObject, OtherObjectDto>().Bidirectional()
.ForMember(model => model.LinkId, option => option.Ignore());
where Bidirectional() is this extension:
public static IMappingExpression<TDestination, TSource> Bidirectional<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
return Mapper.CreateMap<TDestination, TSource>();
}
Is there way to tell Automapper not to map further down the tree in this case?
The way I would handle this is to create separate DTO objects for the children:
public class Employee
{
public int Id {get; set;}
public string Name { get; set; }
public Employee Supervisor {get; set; }
}
public class EmployeeDto {
public int Id {get; set;}
public string Name { get; set; }
public SupervisorDto Supervisor { get; set; }
public class SupervisorDto {
public int Id {get; set;}
public string Name { get; set; }
}
}
Mapper.CreateMap<Employee, EmployeeDto>();
Mapper.CreateMap<Employee, EmployeeDto.SupervisorDto>();
Don't let your DTOs be recursive/self-referential. Be explicit in your structure on how deep you want it to go.
EF can't do recursive joins, you're only doing one level, so don't make your DTOs go nuts with infinitely deep relationships. Be explicit.
If I have the following model:
public class Customer
{
public int Id {get; set;}
public int CustomerTypeId {get; set;}
public virtual CustomerType {get; set;}
}
Should the Dto exclude foreign Id's to look like this:
public class CustomerDto
{
public int Id {get; set;}
public virtual CustomerType {get; set;}
}
And when using Graphdiff to update the object graph, will EF know that CustomerType maps to CustomerTypeId?
Yes, you need to use it but you can avoid virtual member declaration. If you use AutoMapper, then the mapping will be done automatically. So, your Dto will look like this:
public class CustomerDto
{
public int Id {get; set;}
public int CustomerTypeId {get; set;}
}
And the mapping:
Mapper.CreateMap<Customer, CustomerDto>();
Mapper.CreateMap<CustomerDto, Customer>();