I have the following tables:
**Table Person**
public long Id { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public string NameDesportivo { get; set; }
public string Title { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Birthdate { get; set; }
public Nullable<int> CC { get; set; }
public Nullable<int> NIF { get; set; }
public Nullable<bool> Active { get; set; }
public string Natural { get; set; }
public string Nacionality { get; set; }
public string fatherName { get; set; }
public string motherName { get; set; }
public string street { get; set; }
public string numberDoor { get; set; }
public string local { get; set; }
public string parish { get; set; }
public int Id_Contacto { get; set; }
public virtual ICollection<Contact> Contact { get; set; }
public HabLiterarias HabLiterarias { get; set; }
**Table ContactPerson**
public int ContactID
public int PersonId
**Table ContactType**
public long ID { get; set; }
public string Name { get; set; }
public Nullable<bool> Active { get; set; }
public string UserIDModification { get; set; }
public Nullable<System.DateTime> DataModification { get; set; }
public virtual ICollection<Contact> Contact { get; set; }
**Table Contact**
public long ID { get; set; }
public Nullable<long> ContactTypeID { get; set; }
public string ContactValue { get; set; }
public virtual ContactType ContactType { get; set; }
public virtual ICollection<Person> Person { get; set; }
What I'm trying to do is when I create an Person insert more than one contact of that person at the some time: For example Insert type contact mail and it´s value and contact type phone and it's value into the table contact.
Those any one have any ideias. Thank you
Related
It's a class where i need to add item in Subtasks with Ef Core. Have no idea how to do it. Please help!
public class Do
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public string Executors { get; set; }
public DoStatus Status { get; set; }
public DateTime Created { get; set; } = DateTime.Now;
[Required]
public int Plan { get; set; }
public int Fact { get; set; }
public DateTime? Finished { get; set; }
public virtual ICollection<Do> SubTasks { get; set; } = new List<Do>();
}
you need to learn about the recursive CTE relationship
for the date configure there with Api
builder.Property(p => p.Created).HasDefaultValueSql("(newid())")
public class Task
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
public string Executors { get; set; }
public DoStatus Status { get; set; }
public DateTime Created { get; set; };
[Required]
public int Plan { get; set; }
public int Fact { get; set; }
public DateTime? Finished { get; set; }
public int DoId { get; set; } // id parent
public virtual List<Task> SubTasks { get; set; };
}
var id = 1 // id Do
var subTasks = db.Task.include(s => s.SubTasks).where(s => e.DoId = id).ToList()
I want to select from client model all client without duplicate client and order them by count of the purchases they already did
This is model of client :
public class Client
{
public int ID { get; set; }
public string FacebookName { get; set; }
public string RealName { get; set; }
public string Mobile { get; set; }
public int RegionID { get; set; }
public string Addressdetails { get; set; }
public string Email { get; set; }
[DataType(DataType.Date)]
public DateTime DateOfStart { get; set; }
public virtual Region Region { get; set; }
public virtual List<Order> order { get; set; }
}
this is Order model :
public class Order
{
public int ID { get; set; }
public int ClientID { get; set; }
public Nullable<int> ShippingID { get; set; }
public Nullable<int> SharepointID { get; set; }
public Nullable<int> CallStatuID { get; set; }
public Nullable<int> ShippingStatuID { get; set; }
public Nullable<int> ShippingStatuReasonsID { get; set; }
[DataType(DataType.Date)]
public DateTime Date { get; set; }
// public int OrderStatusID { get; set; }
public Nullable<decimal> ShippingCost { get; set; }
public string purchasetype { get; set; }
public string Notes { get; set; }
public Nullable<decimal> Discount { get; set; }
public decimal Total { get; set; }
[DefaultValue(false)]
public bool Prepared { get; set; }
[DefaultValue(false)]
public bool Done { get; set; }
[DefaultValue(false)]
public bool Shipe { get; set; }
[DefaultValue(false)]
public bool Collected { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> DateOfCall { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> StockCheckDate { get; set; }
[DataType(DataType.Date)]
public Nullable<DateTime> ShippingDate { get; set; }
public virtual List<OrderDetail> orderdetails { get; set; }
public virtual Client Client { get; set; }
public virtual Shipping shipping { get; set; }
public virtual Sharepoint sharepoint { get; set; }
public virtual CallStatu callStatu { get; set; }
public virtual ShippingStatu ShippingStatus { get; set; }
public virtual ShippingStatuReason ShippingStatuReasons { get; set; }
}
and finally this is OrderDetails model:
public class OrderDetail
{
public int ID { get; set; }
public int OrderID { get; set; }
public Nullable<int> ItemID { get; set; }
public Nullable<int> SizeID { get; set; }
public int ColorID { get; set; }
public decimal Price { get; set; }
public Nullable<int> StockStatuID { get; set; }
public Nullable<int> StockStatuReasonsID { get; set; }
public int Quantity { get; set; }
public decimal Total { get; set; }
public virtual Order order { get; set; }
public virtual Item item { get; set; }
public virtual Size size { get; set; }
public virtual Colors Colors { get; set; }
public virtual StockStatu stockStatus { get; set; }
public virtual StockStatuReason StockStatuReasons { get; set; }
}
I tried this way but it the select is repeat Clients If they make more than one purchase:
public JsonResult TopClientCount()
{
var index = (from dex in db.Clients
join O in db.Orders on dex.ID equals O.ClientID
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = O.orderdetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();
return Json(index, JsonRequestBehavior.AllowGet);
}
You can use let statement and you can use navigation properties i.e (order ,orderdetails) instead of joins
var index = (from dex in db.Clients
//This will give you all the order details for a client
let orderDetails = dex.order.SelectMany(p=> p.orderdetails)
select new
{
RealName = dex.RealName,
FacebookName = dex.FacebookName,
GovernorateName = dex.Region.governorate.GovernorateName,
RegionName = dex.Region.RegionName,
OrderCount = orderDetails.Count()
}).AsEnumerable().OrderByDescending(o => o.OrderCount)
.Distinct()
.ToList();
DB relationship:
1ApplicationUser : n Tickets
1ApplicationUser: n Activities
1Tickets:n Activites
I would like to design above database using code first. However,there's error warning
"Column name 'ApplicationUserId' in table 'Tickets' is specified more
than once."
However, I checked there's only one ApplicationUserId in Table Ticket as shown in below class.
Would anyone please help how to fix it? Thanks.
The entity class are as following:
Ticket Class
public class Ticket
{
public int ID { get; set; }
public Ticket()
{ TicketCreateDate = DateTime.Now; }
public DateTime TicketCreateDate { get; set; }
public int TicketCreateBy { get; set; }
public DateTime TicketCloseDate { get; set; }
public int TicketCloseBy { get; set; }
public DateTime LastTicketUpdateDate { get; set; }
public int LastUpdateBy { get; set; }//Ticket Last Update by
public DateTime TicketAssignedDate { get; set; }
public int TicketAssignedTo { get; set; }
public string TicketType { get; set; }
public string Priority { get; set; }// Ticket priority
public string TicketStatus { get; set; }
public string TicketDescription { get; set; }
public int DeviceUserID { get; set; }
public string DeviceBrand { get; set; }
public string DeviceType { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
Acitivity Class
public class Activity
{
public int ID { get; set; }
public Activity()
{
CreatedTime = DateTime.Now;
}
public DateTime CreatedTime { get; set; }
public string ActivityDetails { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public int TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
}
ApplicationUser Class
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
}
I've been using this tutorial as a guide for code first n:m. I usually go with db first but for a school project I have to use code first...
My 2 classes are as follows:
public class Product
{
public Product()
{
Stocks = new HashSet<Stock>();
ServingUnits = new HashSet<Unit>();
Orders = new HashSet<Order>();
}
public int ProductId { get; set; }
[StringLength(128, MinimumLength = 1)]
[Required]
public string Title { get; set; }
[StringLength(512, MinimumLength = 1)]
public string Description { get; set; }
public int ExperationPeriod { get; set; }
public byte[] Image { get; set; }
[Required]
public DateTime DateCreated { get; set; }
//foreign keys
[Required]
public int StockUnitId { get; set; }
[Required]
public string CreatedById { get; set; }
[Required]
public int ProductStateId { get; set; }
[Required]
public int ProductTypeId { get; set; }
//navigation properties
[ForeignKey("StockUnitId")]
public virtual Unit StockUnit { get; set; }
[ForeignKey("CreatedById")]
public virtual ApplicationUser CreatedBy { get; set; }
[ForeignKey("ProductStateId")]
public virtual ProductState ProductState { get; set; }
[ForeignKey("ProductTypeId")]
public virtual ProductType ProductType { get; set; }
public virtual ICollection<Stock> Stocks { get; set; }
public virtual ICollection<Unit> ServingUnits { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
and the other:
public class Unit
{
public Unit()
{
Orders = new HashSet<Order>();
ProductsStock = new HashSet<Product>();
ProductsServe = new HashSet<Product>();
}
public int UnitId { get; set; }
[StringLength(255, MinimumLength = 1)]
[Required]
public string Title { get; set; }
[StringLength(128, MinimumLength = 1)]
public string ShortName { get; set; }
public string Description { get; set; }
[Required]
public double BasisFactor { get; set; }
[Required]
public DateTime DateCreated { get; set; }
//foreign keys
[Required]
public string CreatedById { get; set; }
//navigation properties
[ForeignKey("CreatedById")]
public virtual ApplicationUser CreatedBy { get; set; }
public virtual ICollection<Product> ProductsStock { get; set; }
public virtual ICollection<Product> ProductsServe { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
But the join table doesn't exist in my database. Can anyone see what I'm doing wrong?
Thanks
I know this question has already been asked repeatedly, but I can't seem to find an answer that will fit my needs:
I have this model:
[Table("AppUser", Schema = "AppUsers")]
public class AppUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AppUserID { get; set; } //Primary key
[Required]
public string UserIdentificationNumber { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNo { get; set; }
public string Mobile { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
[Required]
public string Gender { get; set; }
public string AboutMeDescription { get; set; }
[ScaffoldColumn(false)]
public string HashedPassword { get; set; }
[ScaffoldColumn(false)]
public string PasswordSalt { get; set; }
[ScaffoldColumn(false)]
public int? VolunteeringRadius { get; set; }
[ScaffoldColumn(false)]
public float? RankAvg { get; set; }
[ScaffoldColumn(false)]
public bool? IsApproved { get; set; }
[ScaffoldColumn(false)]
public DbGeography Location { get; set; }
[ScaffoldColumn(false)]
public bool? IsEmailApproved { get; set; }
[ScaffoldColumn(false)]
public bool? IsSMSApproved { get; set; }
[ScaffoldColumn(false)]
public int? PrefferedContactMethodID { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? RegisterationStatusID { get; set; } //Foreign key
[ScaffoldColumn(false)]
public bool? IsActive { get; set; }
[ScaffoldColumn(false)]
public bool? IsFirstLogin { get; set; }
[ScaffoldColumn(false)]
public bool? IsWebRegister { get; set; }
[ScaffoldColumn(false)]
public int? LanguageID { get; set; }
[Required]
[ScaffoldColumn(false)]
public int AppUserTypeID { get; set; } //Foreign key
[Required]
[ScaffoldColumn(false)]
public int CityId { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? OrganizationID { get; set; } //Foreign key
[ForeignKey("AppUserTypeID")]
public virtual AppUserType AppUserType { get; set; } //Navigation property
[ForeignKey("CityId")]
public virtual City City { get; set; } //Navigation property
[ForeignKey("OrganizationID")]
public virtual Organization Organization { get; set; }
}
which, using entity framework code first approach, renders as a new table in my DbContext and in turn in my SQL Database.
I also have this ViewModel
public class AppUserCreateViewModel
{
[Key]
public int AppUserID { get; set; } //Primary key
public string UserIdentificationNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public int StreetNo { get; set; }
public string Mobile { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
public string Gender { get; set; }
[ScaffoldColumn(false)]
public int AppUserTypeID { get; set; } //Foreign key
[Required]
[ScaffoldColumn(false)]
public int CityId { get; set; } //Foreign key
[ScaffoldColumn(false)]
public int? OrganizationID { get; set; } //Foreign key
public IEnumerable<SelectListItem> AppUserTypes { get; set; } //Navigation property
public IEnumerable<SelectListItem> Cities { get; set; } //Navigation property
public IEnumerable<SelectListItem> Organizations { get; set; }
}
I probably am doing something wrong here, since I want to create a "Create" view from this ViewModel, but to my understanding, I cannot scaffold it, since it will than create an Empty table in the Database (named "AppUserCreateViewModel"). I really have no need in that table (the table I wish to use is "AppUser".
So my questions are:
What should I do if I wish to create a strongly-typed view, by using my viewModel, but without creating a corresponding table in my DB, and to bind this view to my AppUser table?
Should I be using the Repository pattern? Or is it an overkill?
I want the CreateViewModel will render 3 DropDownLists (AppUserTypes, Cities and Organizations) - What's the best way to achieve this?
Thank you very much