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!
Related
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
In my store I have an orders table that captures the orders made by each customer. I want to be able to show the user what quantity of an item they have in their cart. Here is my orders model:
public class Order
{
public int Id { get; set; }
public ApplicationUser Customer { get; set; }
public string ShirtCause {get; set;}
public DateTime CreateDate { get; set; }
public ShirtSize ShirtSize { get; set; }
public ShirtQuantity ShirtQuantity { get; set; }
public decimal Total { get; set; }
public string ShirtYear { get; set; }
public int OrderNum { get; set; }
public bool? OrderCompleted { get; set; }
public bool? RecievedShirt { get; set; }
}
Using code first ShirtQuantity is created in a table and is reflected in the column ShirtQuantity_Id. For reference purposes, here is my quantity model:
public class ShirtQuantity
{
public int Id { get; set; }
public int Name { get; set; }
}
The cart should be output and enumerated in the action with:
return View(db.Orders.ToList());
All except for the Quantity_Id will list when calling
#model IEnumerable<ProjectName.Models.Order>
in the view. How do I get these quantities to list? I have tried creating a view model combining orders and shirtQuantities and calling it in the view:
public class ShirtOrdersViewModel
{
public int ShirtSizeId { get; set; }
public IEnumerable<ShirtSize> ShirtSizes { get; set; }
public int ShirtQuantityId { get; set; }
public IEnumerable<ShirtQuantity> ShirtQuantities { get; set; }
public int ShirtId { get; set; }
public IEnumerable<Shirt> Shirts { get; set; }
public int FileId { get; set; }
public IEnumerable<File> Files { get; set; }
public int OrderId { get; set; }
public IEnumerable<Order> Orders { get; set; }
public decimal Total { get; set; }
}
and this also does not work.
I am running my initial EF migration trying to create the database.
But only 3 of my 5 domains are being created as tables in the database. No errors occur during migration.
I try to run SeedData methods on run, but of course it stops when it tries to save data to the table that does not exist.
I am using EF7. I use DNX to run migrations.
Does anyone know why only 3 of the tables are being created?
Here are the 2 tables that did NOT get created:
// Enumclass, this is not a table
public enum MatchTypeEnum
{
Group, RoundOf32, RoundOf16, QuarterFinals, SemiFinals, BronzeFinal, Final
}
public partial class Match
{
[Key]
public Guid MatchId { get; set; }
public DateTime KickOffTime { get; set; }
public int? HomeScore { get; set; }
public int? AwayScore { get; set; }
public Guid HomeTeamId { get; set; }
public Guid AwayTeamId { get; set; }
public MatchTypeEnum MatchType { get; set; }
public Guid? GroupId { get; set; }
public virtual Team HomeTeam { get; set; }
public virtual Team AwayTeam { get; set; }
[ForeignKey("GroupId")]
public virtual Group Group { get; set; }
}
public enum MatchOutcomeEnum
{
H,A,D
}
public partial class Bet
{
[Key]
public Guid BetId { get; set; }
public Guid UserId { get; set; }
public Guid MatchId { get; set; }
public int? HomeScore { get; set; }
public int? AwayScore { get; set; }
public MatchOutcomeEnum? MatchOutcome{ get; set; }
public int? PointsCorrectScore { get; set; }
public int? PointsBonus { get; set; }
public int? MultiplyScoreWith { get; set; }
public int? TotalPointsBet { get; set; }
[ForeignKey("MatchId")]
public virtual Match MatchBettedOn { get; set; }
}
And here are the 3 classes that do get created:
public class Team
{
[Key]
public Guid TeamId { get; set; }
public string TeamName { get; set; }
public string TeamCountry { get; set; }
public Guid GroupId { get; set; }
public string LogoUrl { get; set; }
[ForeignKey("GroupId")]
public virtual Group TeamGroup { get; set; }
}
public enum GroupEnum
{
GroupA, GroupB, GrooupC, GroupD,GroupE,GroupF, GroupG, GroupH, GroupI
}
/// <summary>
/// The group.
/// </summary>
public partial class Group
{
[Key]
public Guid GroupId { get; set; }
public GroupEnum GroupName { get; set; }
public int? NumberTeamsProceed { get; set; }
public virtual ICollection<Team> TeamsInGroup { get; set; }
public virtual ICollection<Match> MatchesInGroup { get; set; }
}
public class League
{
[Key]
public Guid LeagueId { get; set; }
public string LeagueName { get; set; }
public bool Public { get; set; }
public int LeagueNumber { get; set; }
public string LeagueLink { get; set; }
}
The DbContext:
public class CupBettingContext : DbContext
{
public CupBettingContext()
{
Database.EnsureCreated();
}
public DbSet<Group> Groups { get; set; }
public DbSet<Match> Matches { get; set; }
public DbSet<Bet> Bets { get; set; }
public DbSet<League> Leagues { get; set; }
public DbSet<Team> Teams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = Startup.Configuration["Data:CupBettingContextConnection"];
optionsBuilder.UseSqlServer(connectionString);
base.OnConfiguring(optionsBuilder);
}
}
Thank you for your time :)
UPDATE:
It worked after I I removed the two lines from the class Match:
public virtual Team HomeTeam { get; set; }
public virtual Team AwayTeam { get; set; }
Strange that it ran the migration :/
If anyone is still reading this question, how can I add those virtual object without getting an error?
I'm trying to implement an sorting table in MVC with Razor but i'm having such a pain to make the sorting work with some fields which are non primitive. My class model is as follows
public class Turma
{
public int ID { get; set; }
public string Ano { get; set; }
[ForeignKey("Professor")]
public int Professor_ID { get; set; }
public virtual Professor Professor { get; set; }
[ForeignKey("Disciplina")]
public int Disciplina_ID { get; set; }
public virtual Disciplina Disciplina { get; set; }
}
public class Professor
{
public int ID { get; set; }
public string Nome { get; set; }
public string Email { get; set; }
public string Telefone { get; set; }
}
public class Disciplina
{
public int ID { get; set; }
public string Nome { get; set; }
}
I'm tring to, with an DbSet, sorting it like this:
turmas = turmas.OrderByDescending(s => s.Disciplina.Nome);
Is that even possible?
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?