How do i set dependency to ForeignKey in my child - c#

I need the id to fetch data collections from my parent.
The plan is to fetch all friends from the repository by using the ParentId.
To illustrate I have a Parent and Child class and also one Mapper each.
I suppose I must define the key in the ParentMapper.
See comment in my code
{
public class Child {
public int IdChild { get; set; }
public string ChildName {get;set;}
// here is the problem
//I need to define ParentID as ForeignKey in some way but how??
//I belive its done in the ParentMapper
public virtual int ParentId { get; set; } //ForeignKey
public virtual ICollection<Friend> Friends { get; set; }
//The plan is to fetch all friends from the repository by using the ParentId and
//keep the entity as clean as possible.
public virtual ICollection<Friend> FamilyFriends { get; set; }
}
public class Parent {
public int IdParent { get; set; }
public string ParentName {get;set;}
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
}
{
class ParentMapper : EntityTypeConfiguration<Parent>
{
public ParentMapper()
{
HasKey(one => one.IdParent);
//I started out like this but its not possible....
//But this will give an error obviusly
HasMany(c => c.Children).WithRequired(d => d.ParentId).HasForeignKey(one => one.ParentId);
}
}
}
{
class ChildMapper : EntityTypeConfiguration<Child>
{
public ChildMapper()
{
HasKey(one => one.IdChild);
}
}
}

public class Child {
public int ChildId { get; set; }
public string ChildName {get;set;}
[ForeignKey("Parent")]
public int ParentId { get; set; } //ForeignKey
public Parent Parent{get; set;}
public List<Friend> Friends { get; set; }
public List<Friend> FamilyFriends { get; set; }
}
public class Parent {
public int ParentId { get; set; }
public string ParentName {get;set;}
public List<Friend> Friends { get; set; }
public List<Child> Children { get; set; }
}
public class Friend
{
public int FriendId { get; set; }
public string FriendName {get;set;}
[ForeignKey("Parent")]
public int ParentId{get;set;}
public Friend Parent {get;set;}
[ForeignKey("Child")]
public int ChildId{get;set;}
[InverseProperty("Friends")]
public Child Child {get;set;}
[ForeignKey("ChildFam")]
public int ChildFamId{get;set;}
[InverseProperty("FamilyFriends")]
public Child ChildFam {get;set;}
}

Related

how I can reference multiple Entity into one , in Code First c#

I have this two class
public class Category
{
public int Id{ get; set; }
public string Name { get; set; }
}
public class Filter
{
public int Id{ get; set; }
public string Name { get; set; }
}
And I have another Entity like this
public class Menu
{
public int Id{ get; set; }
public string Name { get; set; }
public MenuType MenuType { get; set; }
}
That the MenuType is enum Like this
public enum MenuType
{
Category = 0,
Filter = 1
}
i want to know in class menu how can store Category OR Filter
i mean Menu have relate with one Category or one Filter , now how can I make this relationship?
and one other thing is maybe MenuType will extende and added some other menutype and thats class.
Is not possible to have a column Foreign Key (FK) that can reference two tables.
A option would be:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public List<Menu> Menus { get; set; }
}
public class Filter
{
public int Id { get; set; }
public string Name { get; set; }
public List<Menu> Menus { get; set; }
}
public class Menu
{
public int Id { get; set; }
public string Name { get; set; }
public MenuType MenuType { get; set; }
public int? CategoryId { get; set; }
public int? FilterId { get; set; }
public Category Category { get; set; }
public Filter Filter { get; set; }
}
Inside your context:
modelBuilder.Entity<Menu>().HasOne(x => x.Category).WithMany(x => x.Menus).HasForeignKey(x => x.CategoryId);
modelBuilder.Entity<Menu>().HasOne(x => x.Filter).WithMany(x => x.Menus).HasForeignKey(x => x.FilterId);
And based on MenuType, you either use Category or Filter object.

How does a mapping of an individual relationship in code first become?

These are my classes.
public class Teacher
{
public Teacher()
{
this.isPassive = false;
}
public int ID { get; set; }
public Nullable<int> LessonID { get; set; }
//Navigation Property
public virtual Lesson Lesson { get; set; }
}
public class Lesson
{
public int ID { get; set; }
public string Name { get; set; }
}
Below are the codes I mapped.
public TeacherMap()
{
HasKey(t => t.ID).Property(t => t.ID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
//Relationship
HasOptional(t => t.Lesson)
.WithMany()
.HasForeignKey(t => t.LessonID);
}
These are my classes and teacher classes. A teacher should have a lesson and a lesson must have a teacher. How should I do this mapping?
you should have a relation one to many , for example a lesson can have one or many teacher you can use this code :
public class Teacher
{
public Teacher()
{
this.isPassive = false;
}
public int ID { get; set; }
public Nullable<int> LessonID { get; set; }
[InverseProperty("Teachers")]
[ForeignKey("LessonID")]
public virtual Lesson Lesson { get; set; }
}
public class Lesson
{
public int ID { get; set; }
public string Name { get; set; }
[InverseProperty("Lesson")]
public virtual ICollection<Teacher> Teachers { get; set;}
}
for this secenario you dont need use fluent api.
UPDATE :
for one to one relation :
public class Teacher
{
public Teacher()
{
this.isPassive = false;
}
public int ID { get; set; }
public virtual Lesson Lesson { get; set; }
}
public class Lesson
{
[Key, ForeignKey("Teacher")]
public int ID { get; set; }
public string Name { get; set; }
public virtual Teacher Teacher { get; set;}
}
For more information see this .

EntityFramework navigation property not working as expected

I have the following 3 (simplified) classes setup in entity framework
public class Service
{
[Key]
public int ServiceId { get; set; }
public int WorkformId { get; set; }
[ForeignKey("WorkformId")]
public virtual Workform Workform { get; set; }
}
public class Workform
{
[Key]
public int WorkformId { get; set; }
[ForeignKey("WorkformId")]
public virtual ICollection<FieldMap> FieldMaps { get; set; }
}
public class FieldMap
{
[Key]
public int FieldMapId{ get; set; }
public int WorkformId{ get; set; }
}
And if I try to do something like this
var service = db.Services.Include("Workform.FieldMaps")
.FirstOrDefault(p => p.ServiceId == serviceId);
service.Workform.FieldMaps is pulling the entire collection, not just the records with the matching relational id.
Am I missing some here?

Retrieve multiple list from grandchild entity using lambda

I've three entities, say Parent, Child & GrandChild. This GrandChild has got three lists(a,b & c).
How to retrive all these lists using "Include" in Entity Framework?
I can retrive one list like this: Include("Parent.Child.GrandChild.a"),
but when I add another Include like : Include("Parent.Child.GrandChild.b"), it throws an error.
How will i retrive all these lists in a single query?
My code is as follows:
objUserNew = objContaxt.ContextRegistration
.Include("listing.postlist.commentslist")
.Include(‌​"listing.postlist.taglist")
.Include("listing.postlist.knocklist")
.FirstOrDefault(‌​x => x.id == objUser.id);
public class registration
{
public int id { get; set; }
public string firstname { get; set; }
public forgtPass forgtPass { get; set; }
public List<listing> listing { get; set; }
}
public class listing
{
public int id { get; set; }
public string address { get; set; }
public List<post> postlist { get; set; }
}
public class post
{
public int id { get; set; }
public string imgUrl { get; set; }
public List<tag> taglist { get; set; }
public List<comments> commentslist { get; set; }
public List<knoqs> knoqs { get; set; }
}
public class tag
{
public string name { get; set; }
}
public class comments
{
public int id { get; set; }
public string comment { get; set; }
public status status { get; set; }
}
public class knoqs
{
public int id { get; set; }
public virtual int registration_id { get; set; }
[ForeignKey("registration_id")] public registration registration { get; set; }
public status status { get; set; }
}

Entity Framework - Store parent reference on child relationship (one -> many)

I have a setup like this:
[Table("tablename...")]
public class Branch
{
public Branch()
{
Users = new List<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
}
[Table("tablename...")]
public class User
{
[Key]
public int Id {get; set; }
public string Username { get; set; }
public string Password { get; set; }
[ForeignKey("ParentBranch")]
public int? ParentBranchId { get; set; } // Is this possible?
public Branch ParentBranch { get; set; } // ???
}
Is it possible for the User to know what parent branch it belongs to? The code above does not populate the ParentBranch.
Entity Framework version 5.0
.NET 4.0
c#
Try making navigation properties virtual,
[Table("tablename...")]
public class Branch
{
public Branch()
{
Users = new List<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<User> Users { get; set; }
}
[Table("tablename...")]
public class User
{
[Key]
public int Id {get; set; }
public string Username { get; set; }
public string Password { get; set; }
[ForeignKey("ParentBranch")]
public int? ParentBranchId { get; set; } // Is this possible?
public virtual Branch ParentBranch { get; set; } // ???
}

Categories