Reputation: 3
Prject.csproj, provides the following lines:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0"/>
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.6.0"/>
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0"/>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0"/>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0"/>
<PackageReference Include="CloudinaryDotNet" Version="1.8.0"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.0"/>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1"/>
</ItemGroup>
</Project>
I'm using this model:
namespace Amaz.API.Models
{
public class Cosa
{
public int Id {get; set;}
public int UserId {get; set;}
public string descripcion {get; set;}
}
}
Which is designed this way in the DataContext.cs as far as EntityFramework is used:
using Amaz.API.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Amaz.API.Data
{
public class DataContext : IdentityDbContext<User, Role, int, IdentityUserClaim<int>,
UserRole, IdentityUserLogin<int>, IdentityRoleClaim<int>, IdentityUserToken<int>>
{
public DataContext(DbContextOptions<DataContext> options) : base(options) {}
public DbSet<Value> Values { get; set; }
public DbSet<Photo> Photos { get; set; }
public DbSet<Like> Likes { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<Familiar> Familiars { get; set; }
public DbSet<Situation> Situations { get; set; }
public DbSet<Cosa> Cosas { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserRole>(userRole =>
{
userRole.HasKey(ur => new {ur.UserId, ur.RoleId});
userRole.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
userRole.HasOne(ur => ur.User)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
});
builder.Entity<Like>()
.HasKey(k => new {k.LikerId, k.LikeeId});
builder.Entity<Like>()
.HasOne(u => u.Likee)
.WithMany(u => u.Likers)
.HasForeignKey(u => u.LikeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Like>()
.HasOne(u => u.Liker)
.WithMany(u => u.Likees)
.HasForeignKey(u => u.LikerId)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Message>()
.HasOne(u => u.Sender)
.WithMany(m => m.MessagesSent)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Message>()
.HasOne(u => u.Recipient)
.WithMany(m => m.MessagesReceived)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Photo>().HasQueryFilter(p => p.IsApproved);
builder.Entity<Familiar>()
.HasOne(u => u.Users)
.WithMany(m => m.Familiars)
.OnDelete(DeleteBehavior.Restrict);
builder.Entity<Situation>();
builder.Entity<Cosa>();
}
}
}
And the controller is:
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using CloudinaryDotNet;
using CloudinaryDotNet.Actions;
using Amaz.API.Data;
using Amaz.API.Dtos;
using Amaz.API.Helpers;
using Amaz.API.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace Amaz.API.Controllers
{
[ServiceFilter(typeof(LogUserActivity))]
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class CosaController : ControllerBase
{
private readonly IVVRepository _repo;
private readonly IMapper _mapper;
public CosaController(IVVRepository repo, IMapper mapper)
{
_mapper = mapper;
_repo = repo;
}
[HttpPost]
public async Task<IActionResult> PutCosa(CosaDto CosaDto)
{
var CosaToCreate = _mapper.Map<Cosa>(CosaDto);
_repo.Add(CosaToCreate);
return NoContent();
}
[HttpGet("{id}")]
public async Task<IActionResult> GetCosa (int id)
{
var cosareturn = await _repo.GetCosa(id);
if (cosareturn == null)
return NotFound();
return Ok(cosareturn);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateCosa(int id, CosaDto CosaDto)
{
var cosa = await _repo.GetCosa(id);
_mapper.Map(CosaDto, cosa);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCosa (int id)
{
var CosaFromRepo = await _repo.GetCosa(id);
_repo.Delete(CosaFromRepo);
return NoContent();
}
}
}
But the main problem is that it is not possible to add a new register to the database through this "PutCosa" method. As Its tested in Postman:
What could be failing here and how to solve it?
Upvotes: 0
Views: 999
Reputation: 95
In EF you actually need to Save your changes on the context so that EF will put the changes to the DB. See here https://learn.microsoft.com/en-us/ef/core/saving/basic
[HttpPost]
public async Task<IActionResult> PutCosa(CosaDto CosaDto)
{
var CosaToCreate = _mapper.Map<Cosa>(CosaDto);
_repo.Add(CosaToCreate);
_repo.SaveChanges();
return NoContent();
}
Upvotes: 2