pyrodistic
pyrodistic
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
So I should just stop trying to dig a deeper hole and simply do this, and add properties as they are needed?
var teamDtos = teams.Select(team => new TeamDto
{
Id = team.Id,
Name = team.Name,
// dozens of other properties here...
CompetitionId = team.CompetitionId,
Competition = team.Competition == null ? null : new CompetitionDto
{
Id = team.Competition.Id,
Name = team.Competition.Name,
//dozens of other properties here...
},
Persons = team.Persons == null ? null : team.Persons.Select(person => new PersonDto
{
Id = person.Id,
GivenName = person.GivenName,
//dozens of other properties here...
//excludes TeamDto
}).ToList()
});
var teamDtos = teams.Select(team => new TeamDto
{
Id = team.Id,
Name = team.Name,
// dozens of other properties here...
CompetitionId = team.CompetitionId,
Competition = team.Competition == null ? null : new CompetitionDto
{
Id = team.Competition.Id,
Name = team.Competition.Name,
//dozens of other properties here...
},
Persons = team.Persons == null ? null : team.Persons.Select(person => new PersonDto
{
Id = person.Id,
GivenName = person.GivenName,
//dozens of other properties here...
//excludes TeamDto
}).ToList()
});
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
Am I stupid or would it might be more efficient to simple create two Dtos for each entity in a way that circular references wouldn't be an issue? One that references it fully and other to be used when there's an innumerable that references the same property.
public class TeamDto
{
public Guid? Id { get; set; }
public string Name { get; set; }
public Guid CompetitionId { get; set; }
public CompetitionDto Competition { get; set; }
public List<PersonBaseDto> Persons { get; set; }
}

public class PersonDto : PersonBaseDto
{
public TeamDto Team { get; set; }
}

public class PersonSimpleDto
{
public Guid? Id { get; set; }
public Guid TeamId { get; set; }
public Guid CompetitionId { get; set; }
}

CreateMap<PersonDto, Person>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
CreateMap<Person, PersonDto>();
CreateMap<Person, PersonSimpleDto>();
public class TeamDto
{
public Guid? Id { get; set; }
public string Name { get; set; }
public Guid CompetitionId { get; set; }
public CompetitionDto Competition { get; set; }
public List<PersonBaseDto> Persons { get; set; }
}

public class PersonDto : PersonBaseDto
{
public TeamDto Team { get; set; }
}

public class PersonSimpleDto
{
public Guid? Id { get; set; }
public Guid TeamId { get; set; }
public Guid CompetitionId { get; set; }
}

CreateMap<PersonDto, Person>()
.ForMember(dest => dest.Id, opt => opt.Ignore());
CreateMap<Person, PersonDto>();
CreateMap<Person, PersonSimpleDto>();
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
The thing is that this was a simple example to illustrate the issue. I will have dozens of endpoints, and again, I think there must be a simpler way. For example, if I wanted to do get the Teams as it currenly is I would have to do:
var teamDtos = teams.Select(team => new TeamDto
{
Id = team.Id,
Name = team.Name,
// dozens of other properties here...
CompetitionId = team.CompetitionId,
Competition = team.Competition == null ? null : new CompetitionDto
{
Id = team.Competition.Id,
Name = team.Competition.Name,
//dozens of other properties here...
},
Persons = team.Persons == null ? null : team.Persons.Select(person => new PersonDto
{
Id = person.Id,
GivenName = person.GivenName,
//dozens of other properties here...
}).ToList()
});
var teamDtos = teams.Select(team => new TeamDto
{
Id = team.Id,
Name = team.Name,
// dozens of other properties here...
CompetitionId = team.CompetitionId,
Competition = team.Competition == null ? null : new CompetitionDto
{
Id = team.Competition.Id,
Name = team.Competition.Name,
//dozens of other properties here...
},
Persons = team.Persons == null ? null : team.Persons.Select(person => new PersonDto
{
Id = person.Id,
GivenName = person.GivenName,
//dozens of other properties here...
}).ToList()
});
All because I the mapping can't ignore the circular reference Team - > Person.Team when detected? I'm going to research a little bit further, because I actually think with some effort I can create a mapping algorithm that will just do that automatically... If the property is a class check if it's the same as a parent class, if yes, skip mapping that class.
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
I can't really change the Dtos, I probably will have to forget Dtos and just manually map on each API endpoint. Which is going to be very time consuming.
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
I guess, because if I have reference I'll have $id, $value and $ref all over and it won't map. I'm actually very lost, I can barely believe that this is something that doesn't have an easy fix.
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
So partially I was being stupid. There's really no error on the mapping. I was missing FirstOrDefault on the Get call. So the only error is only: System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons. Adding the Json options:
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); //Check if line's needed?
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
builder.Services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); //Check if line's needed?
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
The API request for teams and persons results in:
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "string",
"shortName": "string",
"tvName": "string",
"organisation": "string",
"gender": "F",
"historical": true,
"teamType": "ORG",
"competitionId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"competition": {
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "string",
"description": "string",
"startDate": "2024-04-19T21:49:25.752Z",
"endDate": "2024-04-19T21:49:25.752Z",
"totalDistance": 0,
"competitionCode": "string",
"documentCode": "string"
},
"persons": [
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"givenName": "string",
"teamId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"team": "string",
"competitionId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
}
]
}
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "string",
"shortName": "string",
"tvName": "string",
"organisation": "string",
"gender": "F",
"historical": true,
"teamType": "ORG",
"competitionId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"competition": {
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"name": "string",
"description": "string",
"startDate": "2024-04-19T21:49:25.752Z",
"endDate": "2024-04-19T21:49:25.752Z",
"totalDistance": 0,
"competitionCode": "string",
"documentCode": "string"
},
"persons": [
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"givenName": "string",
"teamId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"team": "string",
"competitionId": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
}
]
}
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
The exception is currently thrown on: var teamDto = _mapper.Map<TeamDto>(team); AutoMapper.AutoMapperMappingException HResult=0x80131500 Message=Missing type map configuration or unsupported mapping. Source=<Cannot evaluate the exception source> StackTrace: <Cannot evaluate the exception stack trace> If I remove that line and return team I get: System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Id. If I add the Preserve to the JSON the DTOs won't work because JSON adds automatic properties to each entry (id, value). And so on.
32 replies
CC#
Created by pyrodistic on 4/19/2024 in #help
Entity Framework - Circular References
I'm not though, right? I'm using, or trying to use, the DTO for the endpoint and the entity for the actual DB request, no? How would I be able, through manual mapping to overcome this issue? Basically all I really want to do is Team -> Person -> Team for this person is already set, and was the origin so I don't map that one.
32 replies
CC#
Created by pyrodistic on 2/19/2023 in #help
❔ Access Resources File
Yes, it was build as a embedded resource.
4 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
So when I GET this response on the Mobile I want to say to the app that the user received is now the user logged in.
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
On my API Controller for the Mobile:
public async Task<IActionResult> Login(LoginBindingModel model)
{
if (ModelState.IsValid)
{

var result = await _userHelper.LoginAsync(model);

var user = await _userHelper.GetUserByEmailAsync(model.Username);

if (!result.Succeeded)
{
return BadRequest(new ProblemDetails
{
Title = "Failed to login...",
});
}
return Ok(user);
}

return BadRequest();
}
public async Task<IActionResult> Login(LoginBindingModel model)
{
if (ModelState.IsValid)
{

var result = await _userHelper.LoginAsync(model);

var user = await _userHelper.GetUserByEmailAsync(model.Username);

if (!result.Succeeded)
{
return BadRequest(new ProblemDetails
{
Title = "Failed to login...",
});
}
return Ok(user);
}

return BadRequest();
}
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
After this I just use User.Identity to refer to the user logged in.
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _userHelper.LoginAsync(model);


if (result.Succeeded)
{

return RedirectToAction("Index", "Dashboard");
}
}

this.ModelState.AddModelError(string.Empty, "Failed to login");
return View(model);
}
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
var result = await _userHelper.LoginAsync(model);


if (result.Succeeded)
{

return RedirectToAction("Index", "Dashboard");
}
}

this.ModelState.AddModelError(string.Empty, "Failed to login");
return View(model);
}
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
On my Login for the MVC:
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
cfg.SignIn.RequireConfirmedEmail = true;
cfg.User.RequireUniqueEmail = true;
cfg.Password.RequireDigit = false;
cfg.Password.RequiredUniqueChars = 0;
cfg.Password.RequireUppercase = false;
cfg.Password.RequireLowercase = false;
cfg.Password.RequireNonAlphanumeric = false;
cfg.Password.RequiredLength = 6;
})
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<DataContext>();

services.AddAuthentication()
.AddCookie()
.AddJwtBearer(cfg =>
{
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = this.Configuration["Tokens:Issuer"],
ValidAudience = this.Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(this.Configuration["Tokens:Key"]))
};
});
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.Tokens.AuthenticatorTokenProvider = TokenOptions.DefaultAuthenticatorProvider;
cfg.SignIn.RequireConfirmedEmail = true;
cfg.User.RequireUniqueEmail = true;
cfg.Password.RequireDigit = false;
cfg.Password.RequiredUniqueChars = 0;
cfg.Password.RequireUppercase = false;
cfg.Password.RequireLowercase = false;
cfg.Password.RequireNonAlphanumeric = false;
cfg.Password.RequiredLength = 6;
})
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<DataContext>();

services.AddAuthentication()
.AddCookie()
.AddJwtBearer(cfg =>
{
cfg.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = this.Configuration["Tokens:Issuer"],
ValidAudience = this.Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(this.Configuration["Tokens:Key"]))
};
});
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
For example on my MVC Startup:
15 replies
CC#
Created by pyrodistic on 12/5/2022 in #help
❔ Xamarin Authentication
Sorry, regarding API authentication I'm a complete beginner. I didn't use any default Identity option in my MVC project. I am using Jwt for email confirmation on the MVC section.
15 replies
CC#
Created by pyrodistic on 10/9/2022 in #help
Cascading DropDown
Yeah, it should use the id as the property name as far as I'm aware. No problem, thank you for trying!
22 replies
CC#
Created by pyrodistic on 10/9/2022 in #help
Cascading DropDown
My main doubts are: is the JS correctly located inside the section and tags <script>? Where is it expected for the ActionResult GetStates to reside? I've barely ever used JS so I'm not really fully understanding the implementation.
22 replies
CC#
Created by pyrodistic on 10/9/2022 in #help
Cascading DropDown
Just changed the names.
22 replies