Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3619

HTTP POST specific method with if-condition

$
0
0
public class User{    public int Uid { get; set; }    public string? Name { get; set; }    public virtual ICollection<UserDetails> Details { get; set; } = new List<UserDetails>();}
public class UserDetails{    public int Did { get; set; }    public virtual User? User { get; set; }    public string? ShipToAddress { get; set; }}

There are 2 Model class User & UserDetails with 1 to many relation.I need to Post User & User Details but if Uid is already present in database then Post only UserDetails data.

[HttpPost]public async Task<ActionResult<User>> AddUser(User ud){    await _context.User.AddAsync(ud);    await _context.SaveChangesAsync();    return Ok(ud);}
[HttpPost]public async Task<ActionResult<UserDetails>> AddUserDetails(UserDetails ud){    await _context.UserDetails.AddAsync(ud);    await _context.SaveChangesAsync();    return Ok(ud);}

I need to apply below logic in my API.

var x = _context.User.FirstOrDefault(x => x.Uid == ud.Uid);if (x.Value == null){   AddUser();}else{    AddUserDetails();}

I tried above logic inside AddUser but everytime if there are data present it gives error

"The instance of entity type 'User' cannot be tracked because another instance with the same key value for {'Uid'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values."


Viewing all articles
Browse latest Browse all 3619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>