MVC Many to many search - asp.net-mvc

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 ?

Related

Eager loading with include method does not return data from collection

I wanted to eager load a collection but it does not returns any data
I have tried nothing
//here is my controller
var accountGroup = await db.AccountGroups.Include(a=>a.AccountGroupTypes)
.Include(a=>a.AccountPrimaryGroups).ToListAsync();
//here is my model
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
It returns AccountGroupData but It does not return AccountGroupTypes and AccountPrimaryGroup data
Mark your AccountGroupTypes and AccountPrimaryGroups as virtual properties and be sure that you have a parameterless constructor which is initializing those properties.
public int? Id { get; set; }
public string Name { get; set; }
public int AccountGroupTypeId { get; set; }
public vitual ICollection<AccountGroupType> AccountGroupTypes { get; set; }
public int AccountPrimaryGroupId { get; set; }
public virtual ICollection<AccountPrimaryGroup> AccountPrimaryGroups { get; set; }
public DateTime CreationDateUtcNow{ get; set; }
public AccountGroup()
{
this.AccountGroupTypes = new HashSet<AccountGroupType>();
this.AccountPrimaryGroups = new HashSet<AccountPrimaryGroups>();
}

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

Code First Many to Many Mess what is going on here?

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

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!

EF Code First giving problems in foreign keys

public class ParikshaContext :DbContext
{
public ParikshaContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ParikshaContext>());
}
public DbSet<UserDetail> UserDetails { get; set; }
public DbSet<Standard> Standards { get; set; }
public DbSet<Subject> Subjects { get; set; }
public DbSet<QuestionDescriptor> QuestionDescriptors { get; set; }
public DbSet<QuestionBrief> QuestionBriefs { get; set; }
public DbSet<QuestionCustom> QuestionCustoms { get; set; }
public DbSet<QuestionChoice> QuestionChoices { get; set; }
public DbSet<QuestionMatch> QuestionMatches { get; set; }
public DbSet<Test> Tests { get; set; }
public DbSet<Test_Question> Test_Questions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<QuestionCustom>().ToTable("Custom");
modelBuilder.Entity<QuestionBrief>().ToTable("Brief");
modelBuilder.Entity<QuestionMatch>().ToTable("Match");
modelBuilder.Entity<QuestionChoice>().ToTable("Choice");
}
}
public class QuestionDescriptor
{
public int QuestionDescriptorId { get; set; }
public int StandardId { get; set; }
[ForeignKey("StandardId")]
public virtual Standard Standard { get; set; }
public int SubjectId { get; set; }
[ForeignKey("SubjectId")]
public virtual Subject Subject { get; set; }
public int Rating { get; set; }
public int Difficulty { get; set; }
public DateTime DateOfCreation{get;set;}
public int UserDetailId { get; set; }
[ForeignKeyAttribute("UserDetailId")]
public virtual UserDetail Creator { get; set; }
}
public class QuestionBrief : QuestionDescriptor
{
public String QuestionText { get; set; }
public String Answer { get; set; }
//true for fill in the blanks and false for a loing answers
public bool Short { get; set; }
}
public class Standard
{
public int StandardId { get; set; }
public String StandardName { get; set; }
}
public class Subject
{
public int SubjectId { get; set; }
public String SubjectName { get; set; }
public String SubjectCategory { get; set; }
// public int StandardId { get; set; }
// [ForeignKey("StandardId")]
// public virtual Standard Standard { get; set; }
}
public class Test
{
public int TestID { get; set; }
public DateTime DateOfCreation { get; set; }
public String StandardName { get; set; }
public String SubjectName { get; set; }
public String SubjectCategory { get; set; }
// public int UserDetailId { get; set; }
// [ForeignKey("UserDetailId")]
// public virtual UserDetail Creator { get; set; }
}
public class Test_Question
{
public int Test_QuestionID { get; set; }
public int TestId { get; set; }
[ForeignKey("TestId")]
public virtual Test Test { get; set; }
public int QuestionDescriptorId { get; set; }
[ForeignKey("QuestionDescriptorId")]
public virtual QuestionDescriptor Question { get; set; }
}
public class UserDetail
{
public int UserDetailId { get; set; }
[Required]
[MaxLength(10, ErrorMessage = "UserName must be 10 characters or less"), MinLength(5)]
public String Name { get; set; }
[Required]
public String Password { get; set; }
public String UserRole { get; set; }
public DateTime DateOfCreation{ get; set;}
}
//Match,Custom,Choice classes have been omitted for lack of space (which sounds stupid when i look at the amount of code i have pasted )
I have two problems:-
I cant get a foreign key relation between standard and subjects,it says the relation will cause several cascade delete paths...
if I make a foreign key rlation between test and usedetail it gives me the above problem for mapping the tst_question table .
Also since I am new to EF code first ,please point out my mistakes.all help and disccussion is welcome.
By default EF will create foreign keys will cascade delete. In your model if you delete a Standard there are multiple paths to delete the QuestionDescriptor.
Standard -> QuestionDescriptor
and
Standard -> Subject -> QuestionDescriptor
That is why SQL server does not allow you to do this. See this answer for more details
What you can do is explicitly tell EF to create foreign keys without cascade delete. But this may create data integrity problems. So make sure you understand the consequences.
What you can do is configure the relationships using fluent API with WillCascadeOnDelete(false).
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//other mappings
modelBuilder.Entity<Subject>()
.HasRequired(subject => subject.Standard).WithMany()
.HasForeignKey(subject => subject.StandardId)
.WillCascadeOnDelete(false);
}

Resources