I am currently working with Asp.NEt Core 6. I would like to create CRUD with one to many relationship between two Models (Staffs and Tax). The idea is to have one staff have several tax entries.
Staff Model
namespace Join.Models
{
public class Staff
{
[Key]
public int staffid { get; set; }
public string staffname { get; set; }
public int salary { get; set; }
}
}
Tax Model
namespace Join.Models
{
public class Paye
{
public int Id { get; set; }
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
public int amount { get; set; }
[Required]
[ForeignKey("Staff")]
public int staffid { get; set; }
public virtual Staff Staff { get; set; }
}
}
I do not know how to code the controllers and views
I think it should be like this:
namespace Join.Models
{
public class Staff
{
[Key]
public int staffid { get; set; }
public string staffname { get; set; }
public int salary { get; set; }
public virtual List<Paye> taxEntries {get; set; }
}
}
namespace Join.Models
{
public class Paye
{
public int Id { get; set; }
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
public int amount { get; set; }
[Required]
public int staffid { get; set; }
[ForeignKey("staffid")]
public virtual Staff Staff { get; set; }
}
}
And i would recommend you make a migration after these changes
Related
I have these tables: Sales, product, stock and salesProduct. I'm using a database first approach, they were created using EF scaffold. Sales table looks like this:
public partial class Sales
{
public Sales()
{
SalesProduct = new HashSet<SalesProduct>();
}
public int Id { get; set; }
public DateTime? CreationDate { get; set; }
public bool IndActive { get; set; }
public virtual ICollection<SalesProduct> SalesProduct { get; set; }
}
}
I need the post method on sales creation to create a SalesProduct object and add it to the database. I also need it to update the stock of that specific product.
Here are the other tables:
public partial class SalesProduct
{
public int Id { get; set; }
public int SaleId { get; set; }
public int ProductId { get; set; }
public decimal? Value { get; set; }
public bool IndActive { get; set; }
public virtual Produto IdProductNavigation { get; set; } = null!;
public virtual Vendum IdSalesNavigation { get; set; } = null!;
}
}
public partial class Stock
{
public int Id { get; set; }
public int IdProduct { get; set; }
public int? Quantitu { get; set; }
public bool IndActive { get; set; }
public virtual Product IdProductNavigation { get; set; } = null!;
}
}
How can i do that? I'm confused on how to deal with entity relationships in http methods!
I'm trying to estabilish relationship between 3 tables in c#
My models are:
Guitar:
public class Guitar
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
[MaxLength(30)]
[Required]
public string Model { get; set; }
public int? Price { get; set; }
[NotMapped]
public virtual Brand Brand { get; set; }
public int BrandId { get; set; }
Brand:
public class Brand
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[NotMapped]
public virtual ICollection<Guitar> Guitars { get; set; }
public Brand()
{
Guitars = new HashSet<Guitar>();
}
Purchase:(Some properties might be missing here)
public class Purchase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
[Required]
[MaxLength(40)]
public string BuyerName { get; set; }
[NotMapped]
public virtual Guitar Guitar { get; set; }
public int GuitarId { get; set; }
}
I've managed to link the guitar and the brand tables together, but I can't deal with the third one.
Here's my code so far:
modelBuilder.Entity<Guitar>(entity =>
{
entity.HasOne(guitar => guitar.Brand)
.WithMany(brand => brand.Guitars)
.HasForeignKey(guitar => guitar.BrandId)
.OnDelete(DeleteBehavior.ClientSetNull);
});
Foreign key in Purchases table should be the ID of the guitar, but if you have a better idea I'm open to it.
So, how do I do the same with the Purchases?
remove all [NotMapped] from your classes, And I would add a Buyer class too
public class Guitar
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
....
[Required]
public int? BrandId { get; set; }
public virtual Brand Brand { get; set; }
}
public class Brand
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
.....
public virtual ICollection<Guitar> Guitars { get; set; }
public Brand()
{
Guitars = new HashSet<Guitar>();
}
}
public class Buyer
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
[Required]
[MaxLength(40)]
public string Name { get; set; }
public virtual ICollection<Purchase> Purchases { get; set; }
public Bayer()
{
Purchases = new HashSet<Purchase>();
}
}
public class Purchase
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public int Id { get; set; }
[Required]
public int? BuyerId { get; set; }
public virtual Buyer Buyer { get; set; }
[Required]
public int? GuitarId { get; set; }
public virtual Guitar Guitar { get; set; }
}
and if your are using net core 5+ you don't need any fluent apis
I have been reading about Entity Framework during the pass few weeks. I came across TPT subject. I am a little bit confused and have difficulty to differentiate between TPT and Navigation. When should we choose one over another. Please take a look at the code below.
A. Navication
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public List<ContractEmployee> ContractEmployees { get; set; }
public List<PermanentEmployee> PermanentEmployees { get; set; }
}
public class ContractEmployee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
public Employee Employee { get; set; }
}
public class PermanentEmployee
{
public int AnnualSalary { get; set; }
public Employee Employee { get; set; }
}
B. TPT
[Table("Employee")]
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
}
[Table("ContractEmployee")]
public class ContractEmployee : Employee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
}
[Table("ContractEmployee")]
public class PermanentEmployee : Employee
{
public int AnnualSalary { get; set; }
}
I have an ASP.NET MVC project supporting multiple languages.
the tables (User,Gender,GenderTranslate) had nested inheritance
User inherit from Gender and Gender inherit from GenderTranslate
I have these tables
User table :
public partial class User
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> GenderID { get; set; }
public virtual Gender Gender { get; set; }
}
and Gender table:
public partial class Gender
{
public Gender()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
this.Users = new HashSet<User>();
}
public int Id { get; set; }
public string Icon { get; set; }
public string Value { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
public virtual ICollection<User> Users { get; set; }
}
and Language table:
public partial class Lang
{
public Lang()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
}
and GenderTranslate table :
public partial class GenderTranslate
{
public int LangID { get; set; }
public int GenderID { get; set; }
public string Desc { get; set; }
public virtual Gender Gender { get; set; }
public virtual Lang Lang { get; set; }
}
When I get users I need to get GenderTranslate.Desc, not Gender.Value.
How can I do that using Entity Framework? Please help me
I have a Useraccess Class:
public class UserAccess : CloneableBaseEntity<UserAccess>
{
public int UserId { get; set; }
public User User { get; set; }
public int? SiteId { get; set; }
public virtual Site Site { get; set; }
public int? CostCenterId { get; set; }
public virtual CostCenter CostCenter { get; set; }
public int? ProductId { get; set; }
public virtual Product Product { get; set; }
}
That Class derives from Base Entity:
public class BaseEntity : ObservableObject
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime Created { get; set; }
public int CreatedUserId { get; set; }
public DateTime Changed { get; set; }
public int ChangedUserId { get; set; }
public BaseEntity()
{
}
}
Site, Product and CostCenter expose this Property:
public virtual List<UserAccess> UserAccesses { get; private set; }
When I update my Database the columns in the UserAccess table are still not nullable. What causes this behaviour?