Entityframework navigation property insert - c#

I have these models that ParentID foreign key for CustomerID
I want to insert an address for customer via navigation property
what should I do?
public partial class Customer
{
public long CustomerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Cart> Carts { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
public partial class Address
{
public long AddressID { get; set; }
public long ParentID { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Address1 { get; set; }
public string PostalCode { get; set; }
public virtual Customer Customer { get; set; }
public virtual Supplier Supplier { get; set; }
}

Related

How to get data from 3 related entities using LINQ?

I have a student class, program class and session class. I want to select a student on the basis of his rollno, sessionName, programName and password.
program and session classes have one to many relationship with student class.
This is Session Class.
public class Session
{
[Key]
public int SessionID { get; set; }
[Required]
[MaxLength(30)]
public string SessionName { get; set; }
//Relationship ------- Navigational Properties -------------------------------
public virtual List<Student> Students { get; set; }
public virtual List<Teacher> Teachers { get; set; }
public virtual List<Subject> Subjects { get; set; }
public virtual List<Program> Programs { get; set; }
}
This is program class.
public class Program
{
[Key]
public int ProgramID { get; set; }
[Required]
[MaxLength(30)]
public string ProgramName { get; set; }
//Relationship ------- Navigational Properties -------------------------------
public virtual List<Student> Students { get; set; }
public virtual List<Teacher> Teachers { get; set; }
public virtual List<Subject> Subjects { get; set; }
}
And this is Student Class.
public class Student
{
[Key]
[Column(Order = 0)]
public int StudentID { get; set; }
[Required]
[MaxLength(30)]
public string FirstName { get; set; }
[Required]
[MaxLength(30)]
public string LastName { get; set; }
[Required]
[MaxLength(30)]
public string UserName { get; set; }
[Required]
[MaxLength(35)]
public string Email { get; set; }
[Required]
[MaxLength(30)]
public string Password { get; set; }
[Required]
[MaxLength(30)]
public string FatherName { get; set; }
[Required]
public DateTime DOB { get; set; }
[Required]
[MaxLength(15)]
public string CNIC { get; set; }
[Required]
public int RollNo { get; set; }
public bool Active { get; set; }
public bool Graduated { get; set; }
public bool Expelled { get; set; }
//Relationship ------- Navigational Properties -------------------------------
public virtual List<Teacher> Teachers { get; set; }
public virtual List<Subject> Subjects { get; set; }
public virtual List<StudentMessage> Messages { get; set; }
public Session Session { get; set; }
public Program Program { get; set; }
public Grade Grade { get; set; }
public Attendance Attendance { get; set; }
public StudentContact StudentContact { get; set; }
public StudentMessage StudentMessage { get; set; }
}
Now how do I select student's username who has given specific rollno, sessionName, programName, password using LINQ Query Syntax and using LINQ Method syntax?
In database, Student Table contains SessionID and ProgramID.
I know "Join" is used to extract data from multiple tables but i don't know how to use it in LINQ Syntax.
You can use following code:
var list = dbContext.Students
.Where(e=>e.RollNo == 10)
.Where(e=>e.Password == "password")
.Where(e=>e.Session.SessionName == "SessionName")
.Where(e=>e.Session.ProgramName == "ProgramName")
.Select(e=> e.UserName)
.ToList();

entity hasoptional foreign keyone to one

I have this class:
public class CommunityUser : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int CustomerId { get; set; }
public DateTime CreatedOnUtc { get; set; }
public int ForumPostsNumber { get; set; }
public virtual Customer Customer { get; set; }
}
How to use entity framework mapping to say that a CommunityUser has an optional CustomerId that is a foreign key on the Customer Table?
You must make your foreign key nullable by making it int?
public class CommunityUser : BaseEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int? CustomerId { get; set; }
public DateTime CreatedOnUtc { get; set; }
public int ForumPostsNumber { get; set; }
public virtual Customer Customer { get; set; }
}

Entity Framework 6 asp.net Code First Setup

Hi guys I am spinning wheels on this one. I am using EF6 and ASP.Net 4.6. I have a given table which has student information and parent information. A student can have many parents and a parent can have many students. Call this table 'Contact'. I am to create a table called 'Request' which will hold information for a parent submitting a request for his student. I will create a lookup table with two columns, one for student id and one for parent id called 'StudentParents'. I want to be able to have the parent log in, select his student from a drop down of all of his students and submit the request. The many to many relationship is throwing me off as far as my include statements. How can I get EF to set up this structure so that when I GetRequest(id) I can get the Student info and the Parent info to be included? Here is my code that wont Include anything other than the request.
public class Contact
{
[Key]
public int id { get; set; }
public string student_id { get; set; }//This is the Student ID
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public string grade { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_first_name { get; set; }
[Display(Name = "Parent Middle Name")]
public string contact_middle_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
[Display(Name = "Parent Email")]
public string contact_email { get; set; }
[Display(Name = "Parent Address")]
public string login { get; set; }//This is the Parent ID
public string Classif_description { get; set; }
}
public class Request
{
[Key]
public int id { get; set; }
public Student student_id { get; set; }
public Contact login { get; set; }
[Display(Name = "First School Choice")]
public string firstSchool { get; set; }
[Display(Name = "Second School Choice")]
public string secSchool { get; set; }
[Display(Name = "Rising Grade")]
public string rising_grade { get; set; }
public DateTime ReqSubmitted { get; set; }
public string ReqStatus { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string ModifBy { get; set; }
}
public class Parent
{
public int id { get; set; }
public string contact_first_name { get; set; }
public string contact_middle_name { get; set; }
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
public string contact_email { get; set; }
public string contact_address { get; set; }
public string contact_city { get; set; }
public string contact_zip { get; set; }
[Key]
public string login { get; set; }
public string contact_pw { get; set; }
public string phone { get; set; }
public string phone_type { get; set; }
public Parent() { }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
[Key]
public int id { get; set; }
public int student_id { get; set; }
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public DateTime birthdate { get; set; }
public string gender { get; set; }
public string grade { get; set; }
public string Fed_race_description { get; set; }
public string Classif_description { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
public int home_school_id { get; set; }
public string home_school_name { get; set; }
public Student()
{
this.Parents = new HashSet<Parent>();
}
public virtual ICollection<Parent> Parents { get; set; }
}
public class OEContext : DbContext
{
public OEContext() : base("name=OEContext")
{
}
public DbSet<Request> Requests { get; set; }
public DbSet<Parent> Parents { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(s => s.Students)
.WithMany()
.Map(x =>
{
x.MapLeftKey("login");
x.MapRightKey("student_id");
x.ToTable("StudentParents");
}
);
base.OnModelCreating(modelBuilder);
}
}
Changed the strategy. Made Request have many Contacts. So I added a constructor to the request:
public Request()
{
Contacts = new List<Contact>();
}
public virtual ICollection<Contact> Contacts { get; set; }
Next I changed the contact class:
public int ContactId { get; set; }
public Contact() { }
public virtual Request Request { get; set; }
With this relationship I can pull both Parents and a student from the Contacts associated with the Request.

Foreign Key References to Primary Key in the Same Table EF6

I have class in my MVC project and I used Entity Framework 6. Every person has a Master (master_Id) and it references to the same table (primary key in Person table). My way does not work... what's the solution?
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
[ForeignKey("Person")]
public int Master_Id { get; set; }
public virtual Person Master { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Person
{
[Key]
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public int? MasterId { get; set; }
[ForeignKey("MasterId")]
public virtual Person Master { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}

Combining Entities into ViewModels and using Entity Framework

Most tutorials don't really cover this at all. They just say link your entity to a controller and you're done.
In my business model I have Customers and I have Customer Contacts 1 Customer to >1 Customer Contacts. How do I create a view model for these that will allow them to be edited/created/whatever from the same view?
public class Customer
{
public Customer()
{
this.CustomerContacts = new List<CustomerContact>();
this.Systems = new List<System>();
this.CreatedByCustomerTickets = new List<Ticket>();
this.CustomerTickets = new List<Ticket>();
}
public long CustomerID { get; set; }
public Nullable<bool> BusinessCustomer { get; set; }
public string CustomerName { get; set; }
public string CustomerNotes { get; set; }
public virtual ICollection<CustomerContact> CustomerContacts { get; set; }
public virtual ICollection<System> Systems { get; set; }
public virtual ICollection<Ticket> CreatedByCustomerTickets { get; set; }
public virtual ICollection<Ticket> CustomerTickets { get; set; }
}
public class CustomerContact
{
public long CustomerContactID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Phone { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public Nullable<int> Zip { get; set; }
public Nullable<long> CustomerID { get; set; }
public string Email { get; set; }
public bool PromotionalEmails { get; set; }
public virtual Customer Customer { get; set; }
}
Well I'd start with this
public class CustomerViewModel
{
public Customer Customer {get; set;}
public CustomerContact CustomerContact {get; set;}
}
and work from there.
If you don't need all the properties from the domain objects, you may consider something more like:
public class CustomerViewModel
{
public long CustomerID { get; set; }
public ICollection<CustomerContact> CustomerContacts { get; set; }
}
It's really up to you to construct your view models in a way that will meet the needs of your specific project.

Categories

Resources