C
C#16mo ago
daminko

Implicit insert

29 Replies
daminko
daminkoOP16mo ago
this is the exception I get ^
hyperator
hyperator16mo ago
an error duplicate key makes me think you are writing nested object which should have been removed and that are already in the db
daminko
daminkoOP16mo ago
what should I do in this case?
Keswiik
Keswiik16mo ago
Does it tell you what field contains the duplicate key? oh, didn't see the expand button on that error log let me read @daminko what does your voyage entity look like?
daminko
daminkoOP16mo ago
public class Voyage
{
[Key]
public string IdVoyage { get; set; } = string.Empty;
public DateTime DateSortieBon { get; set; }
public DateTime DateDepartVoyage { get; set; }

public string Etat { get; set; } = string.Empty;
public double Poids { get; set; }
public int NbrePalette { get; set; }

//Demandeur
public Demandeur? Demandeur { get; set; } = null!;
public string? DemandeurMatriculeSopal { get; set; } = string.Empty;
[Column(TypeName = "decimal(30, 25)")]
public decimal Priorite { get; set; }
public Magasinier? MagasinierDemandeur { get; set; } = null!;
public string? MagasinierDemandeurMatriculeSopal { get; set; } = string.Empty;
public Magasinier Magasinier { get; set; } = null!;
public string MagasinierMatriculeSopal { get; set; } = string.Empty;
public TypeVoyage TypeVoyage { get; set; } = null!;
public int TypeVoyageId { get; set; }
public List<Transporteur> Transporteurs { get; set; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
public class Voyage
{
[Key]
public string IdVoyage { get; set; } = string.Empty;
public DateTime DateSortieBon { get; set; }
public DateTime DateDepartVoyage { get; set; }

public string Etat { get; set; } = string.Empty;
public double Poids { get; set; }
public int NbrePalette { get; set; }

//Demandeur
public Demandeur? Demandeur { get; set; } = null!;
public string? DemandeurMatriculeSopal { get; set; } = string.Empty;
[Column(TypeName = "decimal(30, 25)")]
public decimal Priorite { get; set; }
public Magasinier? MagasinierDemandeur { get; set; } = null!;
public string? MagasinierDemandeurMatriculeSopal { get; set; } = string.Empty;
public Magasinier Magasinier { get; set; } = null!;
public string MagasinierMatriculeSopal { get; set; } = string.Empty;
public TypeVoyage TypeVoyage { get; set; } = null!;
public int TypeVoyageId { get; set; }
public List<Transporteur> Transporteurs { get; set; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
}
Keswiik
Keswiik16mo ago
show your voyage repo as well
daminko
daminkoOP16mo ago
public class VoyageRepo : IVoyageRepo
{
private readonly ApplicationDBContext _context;
public VoyageRepo(ApplicationDBContext context)
{
_context = context;
}

public async Task<Voyage> Create(Voyage voyage)
{
await _context.Voyage.AddAsync(voyage);
await _context.SaveChangesAsync();
return voyage;
}

public async Task<List<Voyage>> GetAll(string? transporteurId, string? magasinierId, string? demandeurId)
{
IQueryable<Voyage> query = _context.Voyage.Include(voyage => voyage.Transporteurs);

if (transporteurId != null)
{
query = query.Where(voyage => voyage.Transporteurs.Any(t => t.UserId == transporteurId));
}

if (magasinierId != null)
{
query = query.Where(voyage => voyage.MagasinierMatriculeSopal == magasinierId);
}
if (demandeurId != null)
{
query = query.Where(voyage => voyage.DemandeurMatriculeSopal == demandeurId);
}


return await query.ToListAsync();
}

public Voyage GetVoyageById(int id)
{
throw new NotImplementedException();
}

public Voyage Update(Voyage voyage)
{
throw new NotImplementedException();
}
}
public class VoyageRepo : IVoyageRepo
{
private readonly ApplicationDBContext _context;
public VoyageRepo(ApplicationDBContext context)
{
_context = context;
}

public async Task<Voyage> Create(Voyage voyage)
{
await _context.Voyage.AddAsync(voyage);
await _context.SaveChangesAsync();
return voyage;
}

public async Task<List<Voyage>> GetAll(string? transporteurId, string? magasinierId, string? demandeurId)
{
IQueryable<Voyage> query = _context.Voyage.Include(voyage => voyage.Transporteurs);

if (transporteurId != null)
{
query = query.Where(voyage => voyage.Transporteurs.Any(t => t.UserId == transporteurId));
}

if (magasinierId != null)
{
query = query.Where(voyage => voyage.MagasinierMatriculeSopal == magasinierId);
}
if (demandeurId != null)
{
query = query.Where(voyage => voyage.DemandeurMatriculeSopal == demandeurId);
}


return await query.ToListAsync();
}

public Voyage GetVoyageById(int id)
{
throw new NotImplementedException();
}

public Voyage Update(Voyage voyage)
{
throw new NotImplementedException();
}
}
Keswiik
Keswiik16mo ago
pls code format that
daminko
daminkoOP16mo ago
sure sorry
Keswiik
Keswiik16mo ago
ty
daminko
daminkoOP16mo ago
np
Keswiik
Keswiik16mo ago
Is Transporteur the entity that references ASP users?
daminko
daminkoOP16mo ago
public class Transporteur
{
[Key]
public string UserId { get; set; } = string.Empty;
public Utilisateur Utilisateur { get; set; } = null!;
public Vehicule? Vehicule { get; set; }
public string? VehiculeMatricule { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
public Localisation? Localisation { get; set; } = null;
public int? LocalisationId { get; set; }
public bool Active { get; set; } = true;
}
public class Transporteur
{
[Key]
public string UserId { get; set; } = string.Empty;
public Utilisateur Utilisateur { get; set; } = null!;
public Vehicule? Vehicule { get; set; }
public string? VehiculeMatricule { get; set; } = string.Empty;
public bool ExterneDuService { get; set; } = false;
public List<Voyage> Voyages { get; } = [];
public ICollection<TransporteurVoyage> TransporteurVoyages { get; } = [];
public Localisation? Localisation { get; set; } = null;
public int? LocalisationId { get; set; }
public bool Active { get; set; } = true;
}
public class Utilisateur : IdentityUser
{
public string MatriculeSopal { get; set; } = string.Empty;
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;

}
public class Utilisateur : IdentityUser
{
public string MatriculeSopal { get; set; } = string.Empty;
public string Nom { get; set; } = string.Empty;
public string Prenom { get; set; } = string.Empty;

}
Keswiik
Keswiik16mo ago
ok, and are you trying to create a new voyage for an existing user?
daminko
daminkoOP16mo ago
yep existing users the transporteur, and two magasiniers or the transporteur, and a magasinier and also a demandeur
Keswiik
Keswiik16mo ago
normally to avoid stuff like this I have an explicit foreign key property for my navigations
daminko
daminkoOP16mo ago
I've already done this, no?
Keswiik
Keswiik16mo ago
but I'd try setting Utilisateur to something that's like
something.Utilisateur = new Utilisateur {
WhateverYourPrimaryKeyIs = existingUser.WhateverYourPrimaryKeyIs
}
something.Utilisateur = new Utilisateur {
WhateverYourPrimaryKeyIs = existingUser.WhateverYourPrimaryKeyIs
}
I didn't see one for Utilisateur (which I think is what's causing your current error)
daminko
daminkoOP16mo ago
ow OWWWW I c
Keswiik
Keswiik16mo ago
so what's happening is that EF sees it as a NEW user and is trying to add a new row to the DB rather than just setting the foreign key column on the new Transporteur row
daminko
daminkoOP16mo ago
so it's not binded? cuz when creating a transporteur I create at the sametime a utilisateur
Keswiik
Keswiik16mo ago
not sure what you mean by binded in this case
daminko
daminkoOP16mo ago
I mean when I get the send a create query to the db I send two queries the first creates an utilisateur and the second creates a transporteur
daminko
daminkoOP16mo ago
No description
daminko
daminkoOP16mo ago
this is how i'm doing
Keswiik
Keswiik16mo ago
what does that CreateUtilisateur method look like?
daminko
daminkoOP16mo ago
Sec
public async Task<IdentityResult> CreateAsync(Utilisateur utilisateur)
{
return await _userManager.CreateAsync(utilisateur);

}
public async Task<IdentityResult> CreateAsync(Utilisateur utilisateur)
{
return await _userManager.CreateAsync(utilisateur);

}
Keswiik
Keswiik16mo ago
show the user manager code too my guess is you're creating this with a different dbcontext instance that is disposed of, so the original entity is no longer being tracked but again that's just a guess
daminko
daminkoOP16mo ago
aight thanks

Did you find this page helpful?