New Posts New Posts RSS Feed: Still need foreign key properties in CodeFirst?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Still need foreign key properties in CodeFirst?

 Post Reply Post Reply
Author
s_tristan View Drop Down
Newbie
Newbie
Avatar

Joined: 10-Mar-2011
Location: Ukraine
Posts: 18
Post Options Post Options   Quote s_tristan Quote  Post ReplyReply Direct Link To This Post Topic: Still need foreign key properties in CodeFirst?
    Posted: 15-Oct-2011 at 11:35am
In Code First DbContect we can simply avoid using fk properties with that fluent mapping:
modelBuilder.Entity<Parent>().HasMany(parent=>parent.Children).WithRequired(child=>child.Parent);

Is this enough or DevForce needs this fk properties for it's internal infrastructure? Are additional benefits of using fk properties present?
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2011 at 12:14pm
Hi s_tristan;

What do you mean by using "fk properties"? Do you mean using the ForeignKeyAttribute?
Back to Top
s_tristan View Drop Down
Newbie
Newbie
Avatar

Joined: 10-Mar-2011
Location: Ukraine
Posts: 18
Post Options Post Options   Quote s_tristan Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2011 at 12:31pm
    public class Parent
    {
        public RelatedEntityList<Child> Children { getset; }
    }
 
    public class Child
    {
        public Parent Parent { getset; }
        public int ParentId { getset; } // can we omit this "buddy" property according this blog post http://msdn.microsoft.com/en-us/data/hh134698
    }
 
    public class MyDbContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>().HasMany(parent => parent.Children).WithRequired(child => child.Parent);
        }
    }
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2011 at 1:06pm
Yes, you can omit the fk property if you have specify the fluent mapping. What EF Code First will do behind the scene is generate a Parent_ParentId field on the db table.
Back to Top
s_tristan View Drop Down
Newbie
Newbie
Avatar

Joined: 10-Mar-2011
Location: Ukraine
Posts: 18
Post Options Post Options   Quote s_tristan Quote  Post ReplyReply Direct Link To This Post Posted: 17-Oct-2011 at 1:08pm
Thank you!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down