I'm new to ASP.NET MVC and I have been trying to create a school application in which I have these three classes:
public class student
{
public int id { get; set; }
public string name { get; set; }
}
public class subject
{
public int id { get; set; }
public int Math { get; set; }
public int Computer { get; set; }
public int Chemistry { get; set; }
}
public class result
{
public int id { get; set; }
public int studentId { get; set; }
public int subjectId { get; set; }
public int Total { get; set; }
public virtual student students { get; set; }
public virtual subject subjects { get; set; }
}
I want to get all the properties of subject in result like math, computer, chemistry and so I can edit, create or delete them in result page.
Can somebody point me to any tutorial or help me with this?
A good starting point could be
here
Related
I have two Models. A Bike, and Reservation.
Bike:
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
and Reservation:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual List<Bike> Bikes { get; set; }
public Reservation()
{
Bikes = new List<Bike>();
}
}
When I create the database from Code First it adds a column in the 'Bikes' table called: 'Reservation_Id'.
Click to view image of the problem
The problem comes when I create a new Reservation, The bike which I choose gets the 'Reservation_Id' of the Id from Reservation. So when I try to delete this Reservation I get an error:
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_dbo.Bikes_dbo.Reservations_Reservation_Id". The
conflict occurred in database "BikeRental.Model.BikeShopDataModel",
table "dbo.Bikes", column 'Reservation_Id'.
The code that I use to delete the reservation:
Click here to view the reservation delete code
How can I fix that I can just delete the reservation without interfering with the unique key? Or how do I delete the whole unique key from the table?
There is a One to Many relationship between Bike and Reservation.
EfCore or EF needs a ForeignKey on the Many side. When u did not define the ForeignKey on the rigth side, EF is gonna block you because that Reservation object dependent on the Bike object. Your mistake is defining that relationship in the wrong side.
Bike class should be like :
public class Bike
{
public int Id { get; set; }
public BikeType Type { get; set; }
public BikeGender Gender { get; set; }
public BikeSize Size { get; set; }
public string Brand { get; set; }
public double HourRate { get; set; }
public double DailyRate { get; set; }
public virtual Store Store { get; set; }
public List<Reservation> Reservations{get;set;}
[ForeignKey("Store")]
public int Store_Id { get; set; }
}
Reservation class should be like:
public class Reservation
{
public int Id { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual Customer Customer { get; set; }
[ForeignKey("Customer")]
public int Customer_Id { get; set; }
public virtual Bike Bike { get; set; }
[ForeignKey("Bike")]
public int Bike_Id { get; set; }
}
I am building a holiday tracking website.
I have two data connections in my mvc application.
DeafultConnection which contains aspnetusers which I am using for user logins and Registrations.
Then LotusWorksEntities which I am using to track Employee details such as holiday requests, hours taking etc.
In DefaultConnections under aspnetusers, there's a column for email.
In LotusWorksEntitles under Employee Table, there's also a column for email.
On my Admin view page, I have a list of all Employees which has a column for Site.
I want Admins who have been assigned certain sites to only see employees from those assigned sites.
I have done this manually by
public ActionResult Index()
{
var employees = db.Employees.Include(e => e.Area).Include(e => e.Discipline).Include(e => e.Shift).Include(e => e.Site).Where(e => e.SiteID == 2);
return View(employees.ToList());
}
Is there a way that I could connect these two connections, so that when an admin logs in, it knows what site they've been assigned to and displays those employees under that site.
Here are my models:
public partial class Employee
{
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public System.DateTime StartDate { get; set; }
public int RoleID { get; set; }
public int ShiftID { get; set; }
public int AreaID { get; set; }
public int DisciplineID { get; set; }
public int SiteID { get; set; }
public int ALCategory { get; set; }
public int HoursTaken { get; set; }
public Nullable<int> AwardedLeave { get; set; }
public Nullable<int> TotalHoursThisYear { get; set; }
public int HoursCarriedForward { get; set; }
public Nullable<int> EntitlementRemainingThisYear { get; set; }
public string Comments { get; set; }
public int SickLeaveTaken { get; set; }
public Nullable<int> SickLeaveEntitlement { get; set; }
public Nullable<int> SickLeaveEntitlementRemaining { get; set; }
public int StudyLeaveEntitlement { get; set; }
public int StudyLeaveTaken { get; set; }
public Nullable<int> StudyLeaveRemaining { get; set; }
public int ExamLeaveTaken { get; set; }
public int ForceMajeure { get; set; }
public int BereavementLeaveTaken { get; set; }
public int MaternityLeaveTaken { get; set; }
public int ParentalLeaveTaken { get; set; }
public int AdoptionLeaveTaken { get; set; }
public string ManagerEmail { get; set; }
public string AreaManagerEmail { get; set; }
public virtual Area Area { get; set; }
public virtual Discipline Discipline { get; set; }
public virtual Shift Shift { get; set; }
public virtual Site Site { get; set; }
}
Then my Site Model is:
public partial class Site
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Site()
{
this.Employees = new HashSet<Employee>();
}
public int SiteID { get; set; }
public string SiteName { get; set; }
public string SiteManager { get; set; }
public string SiteDelegate { get; set; }
public string SiteAdmin { get; set; }
public string SiteLocation { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Employee> Employees { get; set; }
}
Employee Table and Site Table are connected through a foreign key.
Not completely understood which two connections you have is it database connection you are talking about?
But anyways you can use Linq joins to connect two separate list of objects in C#. filter data. Basically your two list should have some relation for join to work.
Here it is explained how to join two list in memory.
I have these tables:
public class udb:DbContext
{
public DbSet<klasa> Keuni { get; set; }
public DbSet<student> Seuni { get; set; }
}
public class student
{
[Key]
public int studentid { get; set; }
public string emristudent { get; set; }
public int nota { get; set; }
[ForeignKey("klasaid"), Column(Order = 0)]
public int klasaid { get; set; }
}}
public class klasa
{ [Key]
public int klasaid { get; set; }
public string emriklases { get; set; }
public string vendodhja { get; set; }
public virtual ICollection<student> students { get; set; }
}
The problem is that I want the klasaid to be a foreign key in students so when the user adds a new student the klasaid should exist in klasa model.I use Asp.net mvc 4.Can sb help me?
It should look like this:
public class student
{
[Key]
public int studentid { get; set; }
public string emristudent { get; set; }
public int nota { get; set; }
public int klasaid {get;set;}
[ForeignKey("klasaid")]
public klasa Klasa{ get; set; }
}
You can use various database initialization strategies as follows:
public class udb:DbContext
{
public udb()
{
Database.SetInitializer<udb>(new CreateDatabaseIfNotExists<udb>());
}
public DbSet<klasa> Keuni { get; set; }
public DbSet<student> Seuni { get; set; }
}
One suggestion- follow proper naming conventions. Please, have a look into this link for more details.
I'm new to creating a Windows Store app and it requires a use of a database. I've settled for sqlite and am using the sqlite-net package. However, I'm unsure of how to create a m2m relationship between two models.
class ModelA
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string name { get; set; }
<relationship to ModelB>
}
class ModelB
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string name { get; set; }
}
Would I have to use a List? or a byte[]? How can I guarantee the property will be constrained to ModelB?
You might be able to use sqlite-net-extensions, it has a ManyToMany attribute that seems perfect for your needs. This is an example of its use from their website.
public class Student
{
[PrimaryKey, AutoIncrement]
public int StudentId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
[ManyToMany(typeof(StudentsGroups))]
public List<Group> Groups { get; set; }
public int TutorId { get; set; }
[ManyToOne("TutorId")] // Foreign key may be specified in the relationship
public Teacher Tutor { get; set; }
}
public class Group
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string GroupName { get; set; }
[ForeignKey(typeof(Teacher))]
public int TeacherId { get; set; }
[ManyToOne]
public Teacher Teacher { get; set; }
[ManyToMany(typeof(StudentsGroups))]
public List<Student> Students { get; set; }
}
public class StudentGroups
{
[ForeignKey(typeof(Student))]
public int StudentId { get; set; }
[ForeignKey(typeof(Group))]
public int GroupId { get; set; }
}
You can see my previous question which related with many to many relation but with auto generated mapping table.
I have 2 model, HrTraining and HrPerson. Any people can be assigned to one or more Trainings. You can see my model as below
public class HrTraining
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
public class HrMapTrainingPerson
{
public int Id { get; set; }
public string Status { get; set; }
public int HrTrainingId { get; set; }
public int HrPersonId { get; set; }
public virtual HrTraining HrTraining { get; set; }
public virtual HrPerson HrPerson { get; set; }
}
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
How can i take all training objects which assingned to a person with efficient way.
So you want to find a person, and get all the trainings assigned to it? There are lot of ways.. but using your models, this could be something like this
var trPersons = dbContext.HrPerson.Find(idPerson).HrMapTrainingPerson.ToList();
foreach(var trPerson in trPersons) {
var training = trPerson.HrTraining;
//do what you want, here you can get trPerson.HrTraining.Name for instance
}