Code First Many to Many Mess what is going on here? - asp.net-mvc

Here is a sniplet of my model:
public class ContractParent
{
public int ContractParentID { get; set; }
public string Name { get; set; }
public string Verbiage { get; set; }
public int Position { get; set; }
public bool IncludedByDefault { get; set; }
public virtual IEnumerable<ContractChild> ContractChilds { get; set; }
public virtual bool HasOnlyOneChild { get; set; }
}
public class ContractParentItem
{
public int ContractParentItemID { get; set; }
public virtual int ContractParentID { get; set; }
public virtual ContractParent ContractParent { get; set; }
public int ChargeID { get; set; }
public virtual Charge Charge { get; set; }
}
public class Charge
{
public int ChargeID { get; set; }
public virtual ChargeType ChargeType { get; set; }
public int ChargeTypeID { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<ChargeContract> ChargeContracts { get; set; }
public virtual Company Company { get; set; }
public virtual int? CompanyID { get; set; }
public virtual IEnumerable<ContractParentItem> ContractParentItems { get; set; }
public virtual IEnumerable<ContractChildItem> ContractChildItems { get; set; }
}
When i try to access a Charge with ChargeID = 687:
Charge.ContractParentItem is null.
But its not!
Looking at my table ContractParentItem in SQL
There is an entity:
ContractParentItemID=1
ContractParentID=8
ChargeID=687
I know i am missing something here.
public class Proposals : DbContext
{
public DbSet<Charge> Charges { get; set; }
public DbSet<ContractParent> ContractParents { get; set; }
public DbSet<ContractParentItem> ContractParentItems { get; set; }
}

Instead of
public virtual IEnumerable<ContractParentItem> ContractParentItems { get; set; }
you should have
public virtual ICollection<ContractParentItem> ContractParentItems { get; set; }

Related

how to select from client model all clients thats make an order orderby Count of purchase

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();

Column name 'ApplicationUserId' in table 'Tickets' is specified more than once

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; }
}

MVC Many to many search

i have three tables in my model class
public class Objekat
{
[Key]
public int ObjekatID { get; set; }
[Display(Name="Naziv Objekta")]
public String Naziv { get; set; }
public int? KategorijaID { get; set; }
public int[] idZanrova { get; set; }
public virtual ICollection<Zanr> Zanr { get; set; }
public virtual Kategorija Kategorija { get; set; }
}
public class Zanr
{
public int ZanrID { get; set; }
[Display(Name="Naziv Zanra")]
public string Naziv { get; set; }
public virtual ICollection<Objekat> Objekat { get; set; }
}
public class Kategorija
{
public int KategorijaID { get; set; }
public string Naziv { get; set; }
public ICollection<Objekat> Objekat { get; set; }
}
public class KatalogContext : DbContext
{
public DbSet<Objekat> Objekti { get; set; }
public DbSet<Zanr> Zanrovi { get; set; }
public DbSet<Kategorija> Kategorije { get; set; }
public KatalogContext() : base("MojaBaza") { }
}
Is it possible to make a filter for showing only data from table "Objekat" witch contains specific "Zanrs"?
I suppose the relation between tables "Objekat" and "Zanr" are ok ?

A required field that exists in the view will always make model.state false

I have the following model class:-
public class TMSServer_Validation
{
[Required]
public string ILOIP { get; set; }
}
and the following view :-
#model TMS.ViewModels.ServerJoin
<div >
<span class="f"> ILOIP</span>
#Html.EditorFor(model =>model.Server.ILOIP)
#Html.ValidationMessageFor(model =>model.Server.ILOIP)
</div>
and the following post action method:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ServerJoin sj, FormCollection formValues)
{
//code goes here..
repository.InsertOrUpdateServer(sj.Server, User.Identity.Name, sj.Resource.RESOURCEID);
repository.Save();
return RedirectToAction("Index");
and the following view model class:-
public class ServerJoin
{
public TMSServer Server { get; set; }
public Resource Resource { get; set; }
public Technology Technology { get; set; }
public ComponentDefinition ComponentDefinition { get; set; }
}
but whenever i want to edit an object the model state will be False, and the error message indicates that the ILOP field is required, although there is a value for it on the view?
So what is causing this problem ?
UPDATE
Th model class is :-
[MetadataType(typeof(TMSServer_Validation))]
public partial class TMSServer
{
public TMSServer()
{
this.TMSServers1 = new HashSet<TMSServer>();
this.TMSVirtualMachines = new HashSet<TMSVirtualMachine>();
}
public int ServerID { get; set; }
public Nullable<int> ServerModelID { get; set; }
public int DataCenterID { get; set; }
public string ILOIP { get; set; }
public int RackID { get; set; }
public Nullable<int> StatusID { get; set; }
public Nullable<int> BackUpStatusID { get; set; }
public int RoleID { get; set; }
public Nullable<int> OperatingSystemID { get; set; }
public Nullable<int> VirtualCenterID { get; set; }
public string Comment { get; set; }
public byte[] timestamp { get; set; }
public long IT360SiteID { get; set; }
public virtual DataCenter DataCenter { get; set; }
public virtual OperatingSystem OperatingSystem { get; set; }
public virtual ServerModel ServerModel { get; set; }
public virtual Technology Technology { get; set; }
public virtual TechnologyBackUpStatu TechnologyBackUpStatu { get; set; }
public virtual TechnologyRole TechnologyRole { get; set; }
public virtual TechnologyStatu TechnologyStatu { get; set; }
public virtual TMSRack TMSRack { get; set; }
public virtual ICollection<TMSServer> TMSServers1 { get; set; }
public virtual TMSServer TMSServer1 { get; set; }
public virtual ICollection<TMSVirtualMachine> TMSVirtualMachines { get; set; }
}

Display value in label using values in different table in razor view

I am looking to display Hole_Num value from my Holes class, in my RoundDetails class within the RoundDetails/Create razor view.
Here is my Holes class:
public partial class Hole
{
public Hole()
{
this.RoundDetails = new HashSet();
}
public int HoleId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int Hole_Num { get; set; }
public int Yardage { get; set; }
public byte Par { get; set; }
public short Handicap { get; set; }
public virtual ICollection<RoundDetail> RoundDetails { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my RoundDetails class:
public partial class RoundDetail
{
public int RoundId { get; set; }
public int GolferId { get; set; }
public int FacilityId { get; set; }
public int CourseId { get; set; }
public int TeeTypeId { get; set; }
public int HoleId { get; set; }
public decimal Differential { get; set; }
public Nullable Date { get; set; }
public byte Score { get; set; }
public Nullable Putts { get; set; }
public Nullable GIR { get; set; }
public Nullable FIR { get; set; }
public Nullable Up_And_Down { get; set; }
public Nullable Par_Save { get; set; }
public Nullable Sand_Save { get; set; }
public Nullable Driving_Distince { get; set; }
public Nullable Proximity { get; set; }
public Nullable Green_Fee { get; set; }
public virtual Course Course { get; set; }
public virtual Facility Facility { get; set; }
public virtual Golfer Golfer { get; set; }
public virtual Hole Hole { get; set; }
public virtual TeeType TeeType { get; set; }
}
Here is my razor view:
<td>
#Html.LabelFor(model => model.Hole.Par)
</td>
How am I able to populate this label with the Par information from the Holes table? I assume that some manipulation with the Create method of the RoundDetailController or creating a ViewModel of some sort is the way to go but I"m not sure which one or how start either. Please help!

Resources