C
C#10mo ago
Palfi

ASP.NET | Vet Clinic Application

I need help with this seeming as i am stuck
31 Replies
Palfi
PalfiOP10mo ago
Im working on a self oriented project where i want to make a Vet Clinic Application,it shouldnt be too complicated and only needs to have models for owner,pet,and vaccine,and also an authentication system for the vet I started work with the owner model and pretty much finished it by linking it to the database and getting it going But as soon as i tried to add an area where a user has to enter their pets name and age i started getting database errors meaning i had to roll back I am also working on this with a friend,he is doing the front end seperately and im just doing backend
Keswiik
Keswiik10mo ago
You haven't really given us enough to help you. What errors are you getting in the DB? Code?
Palfi
PalfiOP10mo ago
I had to roll back on my code because i couldnt get my database going,so perhaps you can help me go in the right direction with creating it here is my owner model
using System.ComponentModel.DataAnnotations;
namespace Vet.Models
{
public class Owner
{
[Key]
public int OwnerId { get; set; }

[Required,StringLength(50)]
public string Name { get; set; }


[Required,StringLength(50)]
public string Surname { get; set; }

[Range(18,100)]
public int Age { get; set; }

public List<Pet> Pets { get; set; }

public Owner()
{
Pets = new List<Pet>();
}

public string GetFullName()
{
return $"{Name} + {Surname}";
}
}
}
using System.ComponentModel.DataAnnotations;
namespace Vet.Models
{
public class Owner
{
[Key]
public int OwnerId { get; set; }

[Required,StringLength(50)]
public string Name { get; set; }


[Required,StringLength(50)]
public string Surname { get; set; }

[Range(18,100)]
public int Age { get; set; }

public List<Pet> Pets { get; set; }

public Owner()
{
Pets = new List<Pet>();
}

public string GetFullName()
{
return $"{Name} + {Surname}";
}
}
}
pet model
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Vet.Models
{
public class Pet
{
[Key]
public int PetId { get; set; }

[Required,StringLength(50)]
public string PetName { get; set;}

[Range(0,50)]
public int PetAge { get; set; }

public int OwnerId { get; set; }
[ForeignKey("OwnerId")]
public Owner Owner { get; set; }
public List<Vaccine> Vaccines { get; set;}

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

namespace Vet.Models
{
public class Pet
{
[Key]
public int PetId { get; set; }

[Required,StringLength(50)]
public string PetName { get; set;}

[Range(0,50)]
public int PetAge { get; set; }

public int OwnerId { get; set; }
[ForeignKey("OwnerId")]
public Owner Owner { get; set; }
public List<Vaccine> Vaccines { get; set;}

}
}
vaccine model
using System.ComponentModel.DataAnnotations;

namespace Vet.Models
{
public class Vaccine
{
[Key]
public int VaccineId { get; set; }

[Required,StringLength(50)]
public string VaccineName { get; set;}
public List<Pet> Pets { get; set; }
}
}
using System.ComponentModel.DataAnnotations;

namespace Vet.Models
{
public class Vaccine
{
[Key]
public int VaccineId { get; set; }

[Required,StringLength(50)]
public string VaccineName { get; set;}
public List<Pet> Pets { get; set; }
}
}
Palfi
PalfiOP10mo ago
owner controller
Palfi
PalfiOP10mo ago
and my razor view
@model Vet.Models.Owner

@{
ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Owner</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label">Name</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Surname" class="control-label">Last Name</label>
<input asp-for="Surname" class="form-control" />
<span asp-validation-for="Surname" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Age" class="control-label"></label>
<input asp-for="Age" class="form-control" />
<span asp-validation-for="Age" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

<div>
<a asp-action="Index">Back to List</a>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@model Vet.Models.Owner

@{
ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Owner</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label">Name</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Surname" class="control-label">Last Name</label>
<input asp-for="Surname" class="form-control" />
<span asp-validation-for="Surname" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Age" class="control-label"></label>
<input asp-for="Age" class="form-control" />
<span asp-validation-for="Age" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>

<div>
<a asp-action="Index">Back to List</a>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Anu6is
Anu6is10mo ago
You're still not asking an actual question here What specifically are you looking for help with
Palfi
PalfiOP10mo ago
I need help editing my view To create an area where i can add pets to my owner I wanted to extend the default form I added some code to the owners controller and when i ran it i got database errors
Keswiik
Keswiik10mo ago
You still haven't told us what database errors you were running into.
Palfi
PalfiOP10mo ago
I dont have them saved i think it was errors with name and surname in my create action in create.cshtml
Keswiik
Keswiik10mo ago
So try to add it again yourself and come back when you have something we can help you with.
Palfi
PalfiOP10mo ago
Ok then So i extended the view and controller But now it doesnt save it to my database
Keswiik
Keswiik10mo ago
Any errors?
Palfi
PalfiOP10mo ago
nope I tried updating my database trough nuget i got some kind of error
Keswiik
Keswiik10mo ago
Then set a breakpoint on the owner controller create method and make sure the Owner model you're parsing from the request is valid
Palfi
PalfiOP10mo ago
'FK_PetVaccine_Pet_PetsPetId' is not a constraint. Could not drop constraint. See previous errors.
Keswiik
Keswiik10mo ago
You don't update your database through nuget... nuget is a package manager for dependencies, migrations use the dotnet cli Sounds like you have a bad migration
Palfi
PalfiOP10mo ago
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] ALTER TABLE [PetVaccine] DROP CONSTRAINT [FK_PetVaccine_Pet_PetsPetId]; i tried going at it with the cli but still the same error
Keswiik
Keswiik10mo ago
I'd probably look at your table / schema and see if that constraint actually exists but has a different name. If not, you may need to delete that migration or manually update it so that it's valid.
Palfi
PalfiOP10mo ago
how would i do that? i think i found what it is mentioning
Palfi
PalfiOP10mo ago
No description
Keswiik
Keswiik10mo ago
In that case I'd try updating your migration to have the correct FK name and see if that fixes anything Or try to delete that migration first and create a fresh one to see if the problem persists
Palfi
PalfiOP10mo ago
but now if i try to creae a new migration it doesnt have any code at all no altering no nothing
Keswiik
Keswiik10mo ago
Probably due to your modelsnapshot file existing and being up to date with your code / the deleted migration
Palfi
PalfiOP10mo ago
Is there a voicechat where you could help me in?
Keswiik
Keswiik10mo ago
From here you can: 1) Update your model snapshot file by removing the stuff that was added with your most recent migration 2) Nuke your model snapshot and start from a fresh migration
Palfi
PalfiOP10mo ago
So i can share to you
Keswiik
Keswiik10mo ago
I'm not getting in VC
Palfi
PalfiOP10mo ago
How do i nuke my model snapshot actually because i dont have any info in my db at all can i just delete my db?
Keswiik
Keswiik10mo ago
Delete the SomethingSomethingSomethingModelSnapshot file in the folder that has your migrations yes, you can also delete your DB and start completely fresh If you are doing that, you'd also want to delete everything in your Migrations folder
Palfi
PalfiOP10mo ago
Ok,i made a new database but now again for some reason since i added the new info to the controller its not saving my data
Keswiik
Keswiik10mo ago
So do what I suggested earlier and set a debug breakpoint on the controller method that's supposed to be creating owners and see what data it is getting passed
Want results from more Discord servers?
Add your server