✅ Renaming Model interferes with API controller creation

I had a model named Venture that I renamed to Portfolio because I needed another model named Venture. when trying to create a new Venture controller however it still takes the settings from my now portfolio model instead of the venture model leading to some errors
No description
1 Reply
restingphantom
restingphantomOP8mo ago
c#
namespace HTXL_Back_end.Models;

public class HTXLPortfolioContext : DbContext
{
public HTXLPortfolioContext()
{
}

public HTXLPortfolioContext(DbContextOptions<HTXLPortfolioContext> options)
: base(options)
{
}

public DbSet<Portfolio> Portfolios { get; set; }

public DbSet<Venture> Ventures { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=LAPTOP-HU4NMC01;Database=HighTechXL;Trusted_Connection=true;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Portfolio>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");

modelBuilder.Entity<Venture>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");

// Configure a many-to-many relationship between Portfolios and Sdgs.
modelBuilder.Entity<Portfolio>()
.HasMany(e => e.Sdgs)
.WithMany(e => e.Portfolios);

// Set Created at value to be automaticly created at creation.
modelBuilder.Entity<Portfolio>().Property(x => x.CreatedAt)
.HasDefaultValueSql("getdate() AT TIME ZONE 'Central European Standard Time'");

// Set Updated at velue when row is updated. Doesn't actually work with sql database because sql is stupid!
modelBuilder.Entity<Portfolio>().Property(x => x.UpdatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);

// Configure the Portfolio entity to use a temporal table, enabling tracking of data changes over time.
modelBuilder.Entity<Portfolio>().ToTable("Portfolio", e => e.IsTemporal());
}
}
c#
namespace HTXL_Back_end.Models;

public class HTXLPortfolioContext : DbContext
{
public HTXLPortfolioContext()
{
}

public HTXLPortfolioContext(DbContextOptions<HTXLPortfolioContext> options)
: base(options)
{
}

public DbSet<Portfolio> Portfolios { get; set; }

public DbSet<Venture> Ventures { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=LAPTOP-HU4NMC01;Database=HighTechXL;Trusted_Connection=true;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Portfolio>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");

modelBuilder.Entity<Venture>()
.Property(s => s.Id)
.HasDefaultValueSql("NEWID()");

// Configure a many-to-many relationship between Portfolios and Sdgs.
modelBuilder.Entity<Portfolio>()
.HasMany(e => e.Sdgs)
.WithMany(e => e.Portfolios);

// Set Created at value to be automaticly created at creation.
modelBuilder.Entity<Portfolio>().Property(x => x.CreatedAt)
.HasDefaultValueSql("getdate() AT TIME ZONE 'Central European Standard Time'");

// Set Updated at velue when row is updated. Doesn't actually work with sql database because sql is stupid!
modelBuilder.Entity<Portfolio>().Property(x => x.UpdatedAt)
.HasDefaultValueSql("getdate()")
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Save);

// Configure the Portfolio entity to use a temporal table, enabling tracking of data changes over time.
modelBuilder.Entity<Portfolio>().ToTable("Portfolio", e => e.IsTemporal());
}
}
c#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

[Table("Venture")]
public class Venture
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

public Portfolio? Portfolio { get; set; }
}
c#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

[Table("Venture")]
public class Venture
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

public Portfolio? Portfolio { get; set; }
}
c#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

/// <summary>
/// Portfolio information
/// </summary>
[Table("Portfolio")]
public class Portfolio
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

[Required] public int CoFounders { get; set; }
[Required] public int CoFoundersFemale { get; set; }
[Required] public string CompanyName { get; set; } = null!;
public int? Employees { get; set; }
public int? EmployeesFemale { get; set; }
public double? Fte { get; set; }
public double? FteFemale { get; set; }
public string? Market { get; set; }
public int? NumberOfPatents { get; set; }
public string? Problem { get; set; }
[Required] public string QuarterlyHighs { get; set; } = null!;
[Required] public string QuarterlyLows { get; set; } = null!;
public double Revenue { get; set; }
public string? Solution { get; set; }
public string? Technology { get; set; }
public string? Website { get; set; }

// Date data has been updated
public DateTime? CreatedAt { get; }
public DateTime? UpdatedAt { get; }

// Location
[Required] public string Country { get; set; } = null!;
[Required] public string PostalCode { get; set; } = null!;
[Required] public string City { get; set; } = null!;
[Required] public string Address { get; set; } = null!;

// Other tables
public Series? Series { get; set; }
public Industry? Industry { get; set; }
public Tech1? Tech1 { get; set; }
public Tech2? Tech2 { get; set; }
public ICollection<Sdg> Sdgs { get; set; } = new List<Sdg>();
public ICollection<Investment> Investments { get; set; } = new List<Investment>();
}
c#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace HTXL_Back_end.Models;

/// <summary>
/// Portfolio information
/// </summary>
[Table("Portfolio")]
public class Portfolio
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid? Id { get; set; }

[Required] public int CoFounders { get; set; }
[Required] public int CoFoundersFemale { get; set; }
[Required] public string CompanyName { get; set; } = null!;
public int? Employees { get; set; }
public int? EmployeesFemale { get; set; }
public double? Fte { get; set; }
public double? FteFemale { get; set; }
public string? Market { get; set; }
public int? NumberOfPatents { get; set; }
public string? Problem { get; set; }
[Required] public string QuarterlyHighs { get; set; } = null!;
[Required] public string QuarterlyLows { get; set; } = null!;
public double Revenue { get; set; }
public string? Solution { get; set; }
public string? Technology { get; set; }
public string? Website { get; set; }

// Date data has been updated
public DateTime? CreatedAt { get; }
public DateTime? UpdatedAt { get; }

// Location
[Required] public string Country { get; set; } = null!;
[Required] public string PostalCode { get; set; } = null!;
[Required] public string City { get; set; } = null!;
[Required] public string Address { get; set; } = null!;

// Other tables
public Series? Series { get; set; }
public Industry? Industry { get; set; }
public Tech1? Tech1 { get; set; }
public Tech2? Tech2 { get; set; }
public ICollection<Sdg> Sdgs { get; set; } = new List<Sdg>();
public ICollection<Investment> Investments { get; set; } = new List<Investment>();
}
Half of the new API controller I made
c#
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace HTXL_Back_end.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class VentureController : ControllerBase
{
private readonly HTXLPortfolioContext _context;

public VentureController(HTXLPortfolioContext context)
{
_context = context;
}

// GET: api/Portfolio
[HttpGet]
public async Task<ActionResult<IEnumerable<Portfolio>>> GetVentures()
{
return await _context.Ventures.ToListAsync();
}

// GET: api/Portfolio/5
[HttpGet("{id}")]
public async Task<ActionResult<Portfolio>> GetVenture(Guid id)
{
var venture = await _context.Ventures.FindAsync(id);

if (venture == null)
{
return NotFound();
}

return venture;
}

// PUT: api/Portfolio/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutVenture(Guid id, Portfolio portfolio)
{
if (id != portfolio.Id)
{
return BadRequest();
}

_context.Entry(portfolio).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!VentureExists(id))
{
return NotFound();
}
else
{
throw;
}
}

return NoContent();
}
}
c#
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace HTXL_Back_end.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class VentureController : ControllerBase
{
private readonly HTXLPortfolioContext _context;

public VentureController(HTXLPortfolioContext context)
{
_context = context;
}

// GET: api/Portfolio
[HttpGet]
public async Task<ActionResult<IEnumerable<Portfolio>>> GetVentures()
{
return await _context.Ventures.ToListAsync();
}

// GET: api/Portfolio/5
[HttpGet("{id}")]
public async Task<ActionResult<Portfolio>> GetVenture(Guid id)
{
var venture = await _context.Ventures.FindAsync(id);

if (venture == null)
{
return NotFound();
}

return venture;
}

// PUT: api/Portfolio/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutVenture(Guid id, Portfolio portfolio)
{
if (id != portfolio.Id)
{
return BadRequest();
}

_context.Entry(portfolio).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!VentureExists(id))
{
return NotFound();
}
else
{
throw;
}
}

return NoContent();
}
}
aaaaaaannnnnndddd it fixed itself cool nevermind then I just restarted the IDE for anyone wondering
Want results from more Discord servers?
Add your server