Column name 'ApplicationUserId' in table 'Tickets' is specified more than once - asp.net-mvc

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

Related

How to insert an item into the collection with ef core

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

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

error deleting record from database in Entity Framework

I'm trying to delete a record from database table but I get this error
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.TicketDetail_dbo.Person_PersonID". The conflict occurred in database "AbcDB", table "dbo.TicketDetail", column 'PersonID'
I'm sharing my code please guide me.
TicketDetail:
public class TicketDetail
{
[Key]
public int TicketDetailId { get; set; }
public int? GenericOrderId { get; set; }
public int? PartId { get; set; }
public int? JunkPartId { get; set; }
public int PersonID { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public virtual Part Part { get; set; }
public virtual Ticket Ticket { get; set; }
public virtual Customer Customer { get; set; }
public virtual JunkPart JunkPart { get; set; }
public virtual ICollection<MailSystem> MailSystems { get; set; }
public virtual ICollection<MailLog> MailLogs { get; set; }
}
Person:
public abstract class Person
{
[Key]
public int PersonID { get; set; }
public string City { get; set; }
public DateTimeOffset DateAdded { get; set; }
}
Customer:
public class Customer : Person
{
public int? CountryId { get; set; }
public string Company { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public virtual ICollection<TicketDetail> TicketDetails { get; set; }
public virtual ICollection<MailSystem> MailSystems { get; set; }
public virtual ICollection<MailLog> MailLogs { get; set; }
public virtual Country Country { get; set; }
}
Migration code:
modelBuilder.Entity<Customer>()
.HasMany<TicketDetail>(c => c.TicketDetails)
.WithRequired(x => x.Customer)
.WillCascadeOnDelete(true);
The error happens when I try to delete any customerId.
I have searched internet but couldn't find any appropriate solution, please help me with this error.

ASP.NET MVC finding pending fees

I'm learning ASP.NET MVC 4 with Entity Framework (Database First) and ran into a problem that I'm not able to solve.
I have following tables:
Student: To store students information
Course: To store courses running in an institute
StudentCourse: To store which student selected what course
Fees: To store submitted fees by a student corresponding to StudentCourse
Now, I wan't to show course name, course fees, total fees submitted & the pending fees of students
I'm using Entity Framework in the project and trying to execute a query like this:
var feeslist = entities.Fees
.Where(m => m.StudentCourse.status=="pending")
.GroupBy(m => m.StudentCourse.id)
.Select(m => new { StudentCourseId = m.Key, SumOfFees = m.Sum(v => v.fees) });
I want to get Course object instead of StudentCourseId so that i can display the course, its course fees and total fees submitted by student.
In case, I'm listing all of the four class contents:
Student
public partial class Student
{
public Student()
{
this.Leaves = new HashSet<Leave>();
this.StudentCourses = new HashSet<StudentCourse>();
}
public int id { get; set; }
public string first_name { get; set; }
public string middle_name { get; set; }
public string last_name { get; set; }
public string password { get; set; }
public string email { get; set; }
public string gender { get; set; }
public string facebook { get; set; }
public string contact_number { get; set; }
public string address { get; set; }
public string admin { get; set; }
public string college { get; set; }
public string status { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public string descripton { get; set; }
public virtual ICollection<Leave> Leaves { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}
Course
public partial class Course
{
public Course()
{
this.CourseContents = new HashSet<CourseContent>();
this.StudentCourses = new HashSet<StudentCourse>();
}
public int id { get; set; }
public string course_name { get; set; }
public int course_fees { get; set; }
public int duration { get; set; }
public string admin { get; set; }
public string status { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public virtual ICollection<CourseContent> CourseContents { get; set; }
public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}
StudentCourse
public partial class StudentCourse
{
public StudentCourse()
{
this.Fees1 = new HashSet<Fee>();
this.Issues = new HashSet<Issue>();
}
public int id { get; set; }
public int student_id { get; set; }
public int course_id { get; set; }
public int fees { get; set; }
public System.DateTime join_date { get; set; }
public string admin { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public string status { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Fee> Fees1 { get; set; }
public virtual ICollection<Issue> Issues { get; set; }
public virtual Student Student { get; set; }
}
Fee
public partial class Fee
{
public int id { get; set; }
public int student_course_id { get; set; }
public int fees { get; set; }
public string admin { get; set; }
public System.DateTime createdat { get; set; }
public System.DateTime updatedat { get; set; }
public virtual StudentCourse StudentCourse { get; set; }
}
var feeslist = entities.Fees
.Where(m => m.StudentCourse.status=="pending")
.GroupBy(m => m.StudentCourse.Course)
.Select(m => new { Course = m.Key, SumOfFees = m.Sum(v => v.fees) });

MVC3 many-to-many with additional column Examples

which is the best way or if you have seen an example of which is the best way or a way to display to the user a view with this model and if you have and link example will be better
public class Student
{
public int StudentID { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public decimal? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}

Resources