Multi-Level One to Many in ASP MVC5 - asp.net

I am working on a small project, where I have multi-level one-to-many model. But for some reason, the EF doesn't seem to understand this relationship and I receive an error when I create the MVC controller with EF.
Here are my model classes:
public class BusinessDomain
{
[Key]
public int BusinessDomainID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessDomainID { get; set; }
public virtual BusinessDomain BusinessDomain { get; set; }
}
public class BusinessSubProcess
{
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public int BusinessProcessID { get; set; }
public virtual BusinessProcess BusinessProcess { get; set; }
}
When I create a EF controller, I get following error: Dependent role refers to the key properties, the upper bound of the multiplicity of the Dependent role must be 1.
If I keep only two models (BusinessLine and BusinessProcess) it seems to work. I begin to wonder if ASP MVC doesnt support multilevel of one to many models?
Please tell me what am I doing wrong?
Thanks in advance!

The only difference that I see is that you do not include a reference to the BusinessLine object in the BusinessProcess class. You should include that and also mark it as a foreign key.
Also, you marked the BusinessSubprocessID as a foreign key to the BusinessProcess, which I believe is a mistake since you also hold a BusinessProcessID in your class.
As a side note, do you need the BusinessLineID in your subprocess class, since you can easily access it through its parent?
public class BusinessLine
{
[Key]
public int BusinessLineID { get; set; }
[Display(Name = "Business Domain Name")]
public string DomainName { get; set; }
[Display(Name = "Domain Manager Name")]
public string DomainManager { get; set; }
public virtual ICollection<BusinessProcess> BusinessProcesses { get; set; }
}
public class BusinessProcess
{
[Key]
public int BusinessProcessID { get; set; }
[Display(Name = "Business Processs Name")]
public string ProcessName { get; set; }
[Display(Name = "Business Process Owner Name")]
public string ProcessOwner { get; set; }
public virtual ICollection<BusinessSubProcess> BusinessSubProcesses { get; set; }
public int BusinessLineID { get; set; }
[ForeignKey("BusinessLineID")]
public virtual BusinessLine BusinessLine { get; set; }
}
public class BusinessSubProcess
{
[Key]
public int BusinessSubProcessId { get; set; }
public string SubProcessName { get; set; }
public string SubProcessOwnerName { get; set; }
public DateTime Created { get; set; }
public DateTime LastChange { get; set; }
public int BusinessProcessID { get; set; }
[ForeignKey("BusinessProcessID")]
public virtual BusinessProcess BusinessProcess { get; set; }
}

Related

How to make DTOs for complex models

My target with the below mentioned classes is to make one DTOs class out of these classes and register AppUser in sqlite database. I use Identity core. I thank you for every body who has given me help on solving this problem.
By the way: is that possible to have virutal properties inside DTO classes? Other thing which i am struggling with is on how to handle the incoming user input (in this case DTO). I get the following error time and again Cannot implicitly convert type int to Backend.Data.Gender. I Cannot implicitly convert type Backend.Data.Work_Experience to System.Collections.Generic.List<Backend.Data.Work_Experience>
public class AppUser : IdentityUser<int>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Description { get; set; }
virtual public Gender Gender { get; set; }
virtual public ICollection<Interest> Interests {get; set;}
virtual public List<Education> Education { get; set; }
virtual public List<Work_Experience> Work_Experiences { get; set; }
virtual public List<Answer> Answers { get; set; }
}
public class Interest
{
[Key]
public int Id { get; set; }
public string InterestName{ get; set; }
public virtual ICollection<AppUser> Users { get; set; }
}
public class Education
{
[Key]
public int EducationsId { get; set; }
public string SchoolName { get; set; }
public string Field_Of_Study { get; set; }
public string StudyPath { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int DegreeId { get; set; }
public virtual Degree Degree { get; set; }
public int UserId { get; set; }
public virtual AppUser User { get; set; }
}
public class Degree
{
[Key]
public int DegreesId { get; set; }
public string DegreeName { get; set; }
}
public class Gender
{
[Key]
public int GenderId { get; set; }
public string GenderName { get; set; }
}
public class Work_Experience
{
[Key]
public int WorkExperiencesId { get; set; }
public string JobTitle { get; set; }
public string CompanyName { get; set; }
public string Industry { get; set; }
public string Field_Of_Work { get; set; }
public int UserId { get; set; }
public virtual AppUser User { get; set; }
}
public class Question
{
[Key]
public int QuestionsId { get; set; }
public string Question_text { get; set; }
}
public class Answer
{
[Key]
public int AnswersId { get; set; }
public string Answer_text { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
public int UserId { get; set; }
public virtual AppUser User { get; set; }
}

The entity type 'Customer' cannot be mapped to a table because it is derived from 'ApplicationUser'

I am trying to use separate identity classes that inherit from ApplicationUser, and create a relationship between them. Business and Customer where a Business can have many Customers. I get an error when adding relationships. What is the proper way to achieve adding identity between classes with relationships?
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public virtual Business Business { get; set; }
public virtual Customer Customer { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
Business.cs
public class Business : ApplicationUser
{
public string BusinessUserId { get; set; }
[Required]
public string BusinessName { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
public ICollection<Customer> Customers { get; set; }
....
}
Customer.cs
public class Customer : ApplicationUser
{
public string CustomerUserId { get; set; }
public override Business Business { get; set; }
}
You are using TPH which will go into the same table(AspNetUser) for your above entities.
To use TPT, you could configure models like
public class ApplicationUser : IdentityUser
{
public string BusinessId { get; set; }
[ForeignKey("BusinessId")]
public virtual Business Business { get; set; }
public string CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customer Customer { get; set; }
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
}
public class Business
{
[Key]
public string BusinessUserId { get; set; }
[Required]
public string BusinessName { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
public virtual ApplicationUser User { get; set; }
public ICollection<Customer> Customers { get; set; }
}
public class Customer
{
[Key]
public string CustomerUserId { get; set; }
public string BusinessId { get; set; }
[ForeignKey("BusinessId")]
public Business Business { get; set; }
public virtual ApplicationUser User { get; set; }
}
DbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<Business> Businesses { get; set; }
}

Entity Framework many to many relation error

I'm trying to create a many-to-many relationship between my two tables, but when I run Update-Database command I get this error:
Introducing FOREIGN KEY constraint 'FK_dbo.ExamQuestions_dbo.Questions_Question_Id' on table 'ExamQuestions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
My first entity is :
public class Question
{
public Question()
{
this.Exams = new HashSet<Exam>();
}
[Key]
public int Id { get; set; }
[Required(ErrorMessage="Question is Required")]
[Display(Name="Question")]
[AllowHtml]
public string QuestionText { get; set; }
// public bool IsMultiSelect { get; set; }
public string Hint { get; set; }
public string HelpLink { get; set; }
public int Marks { get; set; }
public byte[] ImageData { get; set; }
[StringLength(50)]
public string MimeType { get; set; }
public byte[] Audio { get; set; }
public int QuestionTypeId { get; set; }
public int TopicId { get; set; }
public int DifficulityLevelId { get; set; }
public int SubjectId { get; set; }
public DifficultyLevel QuestionDifficulity { get; set; }
public Topic Topic { get; set; }
public virtual ICollection<Option> Options { get; set; }
public ICollection<Exam> Exams { get; set; }
}
And the second entity is:
public class Exam
{
public Exam()
{
this.Questions = new HashSet<Question>();
}
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int Duration { get; set; }
[Required]
public int TotalQuestion { get; set; }
[Required]
public int TotalMarks { get; set; }
public bool SectionWiseTime { get; set; }
public bool QuestionWiseTime { get; set; }
public bool AllQustionRequired { get; set; }
public bool AllowBackForward { get; set; }
public bool SuffleSubjectWise { get; set; }
public bool SuffleOptionWise { get; set; }
public bool GroupSubjectWise { get; set; }
[Required]
public int ExamTypeId { get; set; }
[Required]
public int ExamInstructionId { get; set; }
[Required]
public int DifficultyLevelId { get; set; }
public virtual ExamType ExamType { get; set; }
public virtual ExamInstruction ExamInstruction { get; set; }
public virtual DifficultyLevel DifficultyLevel { get; set; }
public virtual ICollection<Question> Questions { get; set; }
//public virtual ICollection<ExamSchedule> ExamSchedules { get; set; }
}
Can someone tell me where I'm going wrong?
By default, EF has cascading deletes set. This error is warning you that this can cause cascading deletes with many to many relationships. And is probably not what you intend to have happen on a delete/update.
You can remove the OneToManyCascadeDeleteConvention in the OnModelCreating method, or on the fluent mapping for each entity.
Details are provided in this SO Answer

Mapping existing database with code first approch

I have implemented asp .net application without Code first approach. now i want to build different system with existing database and code first.
public class EmployeeEntity
{
public int Id { get; set; }
public string ResourceCode { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public int Gender { get; set; }
public int Status { get; set; }
public string NIC { get; set; }
}
now i want to add code first entities.
[Table("ExternalApplicantEntity")]
public class ExternalApplicantEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicantID { get; set; }
//[ForeignKey("EmployeeEntity")]
//public int Created_By { get; set; }
//public virtual EmployeeEntity EmployeeEntity { get; set; }
public string NIC { get; set; }
public string Mobile { get; set; }
public string Institute { get; set; }
how i combine these codes?

Creating Relational Tables in Entity Framework

I am trying to build a pretty extensive database heavy web application and doing so with little experience. What I am trying to figure out is creating a relational table in entity framework through scaffolding.
It seems I have accidentally done this already with these two models, but I have no idea how it happened:
namespace FlavorPing.Models
{
public class MenuItem
{
[Key]
public int MenuItemID { get; set; }
public string ItemName { get; set; }
public string Description { get; set; }
//Category
//May need to put this back and add to controllers and views.
//[ForeignKey("Merchant")]
//public int MerchantID { get; set; }
public virtual Merchant Merchant { get; set; }
public ICollection<Follower> Followers { get; set; }
}
}
public class Merchant
{
//Meant to inherit identity.
[ForeignKey("ApplicationUserId")]
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
[Key]
public int MerchantID { get; set; }
[Required]
[Display(Name = "Business Name")]
public string MerchantName { get; set; }
[Required]
[Display(Name = "Email")]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
//need to create formatting here.
[Required]
[Display(Name = "Web Site Link")]
public string website { get; set; }
public int MenuItemID { get; set; }
public virtual List<MenuItem> MenuItems { get; set; }
public virtual MerchantDetails MerchantDetails { get; set; }
}
The above models have their own dedicated tables, but a second table MenuItemFollowers was created with the MenuItemID and FollowerID as columuns, which I want, but I have no idea how I did this and need to know so I could add another ID to this table.

Resources