Foreign Key References to Primary Key in the Same Table EF6 - c#

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

Related

Mapping 4 tables in Entity Framework using Fluent API

I am having difficulties in creating custom mapping table in EF6.
I have 4 tables:
Student
Instructor
Questions
Course
What I want is to create a table that will include all primary keys of those 4 tables and add a score column to it.
What is the command that should I use in Entity Framework Fluent API?
This is the classes that i have created
public class Student
{
public Student()
{
this.Courses = new HashSet<Course>();
}
public int StudentId { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Instructor
{
public Instructor()
{
this.Courses = new HashSet<Course>();
}
public int InstructorId { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public Course()
{
this.Instructors = new HashSet<Instructor>();
this.Students = new HashSet<Student>();
}
public int CourseId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public virtual ICollection<Instructor> Instructors { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Question
{
public int QuestionId { get; set; }
public string Description { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}

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

Unexpected foreign key generated for one to many relationship

I have declared two classes - Person and Vehicle as shown below
public class Person
{
public Person()
{
this.Vehicles = new HashSet<Vehicle>();
}
[Key]
public int PersonID { get; set; }
[Required, MaxLength(50)]
public string FirstName { get; set; }
[Required, MaxLength(50)]
public string MiddleName { get; set; }
[Required, MaxLength(50)]
public string LastName { get; set; }
[Required, MaxLength(10)]
public string MobileNo1 { get; set; }
[MaxLength(10)]
public string MobileNo2 { get; set; }
[MaxLength(50)]
public string Email1 { get; set; }
[MaxLength(50)]
public string Email2 { get; set; }
public virtual ICollection<Vehicle> Vehicles { get; set; }
}
public class Vehicle
{
[Key]
public int VehicleID { get; set; }
[MaxLength(20)]
public string VehicleNumber { get; set; }
[ForeignKey("VehicleOwner")]
public int? VehicleOwnerID { get; set; }
[ForeignKey("VehicleOwnerID")]
public virtual Person VehicleOwner { get; set; }
[ForeignKey("VehicleDriver")]
public int? VehicleDriverID { get; set; }
[ForeignKey("VehicleDriverID")]
public virtual Person VehicleDriver { get; set; }
[ForeignKey("Person")]
public int? PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
}
This generates two foreign keys on Vehicles table as
.ForeignKey("dbo.Person", t => t.PersonID)
.ForeignKey("dbo.Person", t => t.Person_PersonID)
whereas what i expect is only
.ForeignKey("dbo.Person", t => t.PersonID)
Initially i thought it might be because i missed out declaring the entities as virtual but that was not the case. I am not able to detect the problem with this code.
Like Vehicles, i have another class - Documents with somewhat the same structure and relationship with Person. But for Documents the foreign key is generated as expected.
You've got 3 classes pointing at Person, so configure as:
public class Vehicle
{
[Key]
public int VehicleID { get; set; }
[MaxLength(20)]
public string VehicleNumber { get; set; }
public int? VehicleOwnerID { get; set; }
[ForeignKey("VehicleOwnerID")]
public virtual Person VehicleOwner { get; set; }
public int? VehicleDriverID { get; set; }
[ForeignKey("VehicleDriverID")]
public virtual Person VehicleDriver { get; set; }
public int? PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
}
While that is incorrect syntax, the 2nd foreign key comes from the collection of vehicles on the person and EF not being able to resolve which FK it belongs to.
So in your Person class on your collection you need to point to the corresponding nav in the vehicle:
[InverseProperty("Person")]
public virtual ICollection<Vehicle> Vehicles { get; set; }
http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx
As Steve has said, this is because you have references to Person from your vehicle class. So, Entity Framework creates 3 keys one for Person, VehicleOwner, and VehicleDriver. Yet, it creates another key because of the collection of vehicles from person. You must use the inverse property attribute here to tell Entity Framework that you mean to build a relationship between Vehicle and Person on the actual Vehicle.Person property. You can do this by
[InverseProperty("Person")]
public virtual ICollection<Vehicle> Vehicles { get; set; }
Also, you being redundant with:
[ForeignKey("Person")]
public int? PersonID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Person { get; set; }
Remove the ForeignKey from the int? PersonID in your Vehicle class.

Entity Framework Multi Table Relation

I have three tables
EVENT - PERSON - COMPANY
I need to have a relation many-to-many using those tables. An event can have one or more "clients",which can be either person or company. Normally, using no ORM, using sql , it would be something like :
EVENT
----
id
name
CLIENTEVENT
-----------
id
clientid
clienttype -- person or company
PERSON
-----------
id
name
lastname
...
COMPANY
-------
id
name
How does this approach can be replicated using entity framework? I am pretty new using EF so I would appreciate all help you can give me.I am using repository pattern, following this approach http://www.codeproject.com/Articles/838097/CRUD-Operations-Using-the-Generic-Repository-Pat.
I advice you instead of clientid and clienttype make another two columns: personID and companyID. Models will look this way:
public class Event
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class Company
{
public int ID { get; set; }
public string name { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class PERSON
{
public int ID { get; set; }
public string name { get; set; }
public string lastname { get; set; }
public virtual ICollection<CLIENTEVENT> links { get; set; }
}
public class CLIENTEVENT
{
public int ID { get; set; }
public virtual PERSON person { get; set; }
public int? personID { get; set; }
public virtual Company company { get; set; }
public int? companyID { get; set; }
public virtual Event event1 { get; set; }
public int? event1ID { get; set; }
}
With EF Code First, you don't have to deal with joining tables like ClientEvent. You can simply write:
public class Event
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Company> Companies { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public string Lastname { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
Try this code, you'll see EF creating two linking tables (EventPerson and EventCompany). Now, the client type is the collection you read (someEvent.Persons or someEvent.Companies), you can also easily get the events linked to a specific Person or a Company.
About many-to-many with EF.

Entityframework navigation property insert

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

Categories

Resources