I'm a junior developer trying to figure out Code First (EF 4.1).
Can someone explain in simple terms how to create a simple one to many relation between two classes (tables)?
I found the following code in one of Julie Lerman's tutorials. But there is apparently so much "automagic" happening under the hood, that I guess I need it spelled out better for me, if I'm going to know how to create my own classes. Can someone explain
exactly what is going on here, as far as how the relationships are coded in the class? (I downloaded the sample project with this, but it's giving errors -
http://msdn.microsoft.com/en-us/data/gg685467.)
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string BloggerName { get; set;}
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int PostId { get; set; }
public Post Post { get; set; }
}
The "automagic" stuff you are referring to is called "convention over configuration", although I like your name better.
Basically, instead of having to explicitly define the foreign key relationships, you can follow certain naming conventions when creating your
classes and Code First will create them for you "automagically". The two conventions that come into play for relationships are the primary key convention and the foreign key convention.
The primary key convention will identifiy any property with the name Id or [Class Name]+Id as the primary key for a given class. The foreign key convention will identify any property named [Some Other Class Name]+Id as a foreign key reference. So using the
example you posted, Code First recognizes the Blog.Id property as the primary key for the Blog class and it also recognizes that the Post.BlogId property is a foreign key reference to the Blog class.
Here's the documentation on EF4.1 Model Configuration Conventions:
hapax_legome...
Member
316 Points
357 Posts
Code First - how to create simple relationship
Jun 22, 2011 04:03 PM|LINK
I'm a junior developer trying to figure out Code First (EF 4.1).
Can someone explain in simple terms how to create a simple one to many relation between two classes (tables)?
I found the following code in one of Julie Lerman's tutorials. But there is apparently so much "automagic" happening under the hood, that I guess I need it spelled out better for me, if I'm going to know how to create my own classes. Can someone explain exactly what is going on here, as far as how the relationships are coded in the class? (I downloaded the sample project with this, but it's giving errors - http://msdn.microsoft.com/en-us/data/gg685467.)
public class Blog { public int Id { get; set; } public string Title { get; set; } public string BloggerName { get; set;} public virtual ICollection<Post> Posts { get; set; } } public class Post { public int Id { get; set; } public string Title { get; set; } public DateTime DateCreated { get; set; } public string Content { get; set; } public int BlogId { get; set; } public ICollection<Comment> Comments { get; set; } } public class Comment { public int Id { get; set; } public DateTime DateCreated { get; set; } public string Content { get; set; } public int PostId { get; set; } public Post Post { get; set; } }princeG
Star
9612 Points
1602 Posts
Re: Code First - how to create simple relationship
Jun 22, 2011 04:11 PM|LINK
Relation between these tables
Blog 1:M Post 1:M Comment
please check http://www.dotnetfunda.com/forums/thread4448-how-to-work-with-aspnet-mvc3-give-me-some-example-code-projects.aspx
here you can find some code snipped
chohmann
Star
9385 Points
1644 Posts
Re: Code First - how to create simple relationship
Jun 22, 2011 06:43 PM|LINK
The "automagic" stuff you are referring to is called "convention over configuration", although I like your name better.
Basically, instead of having to explicitly define the foreign key relationships, you can follow certain naming conventions when creating your
classes and Code First will create them for you "automagically". The two conventions that come into play for relationships are the primary key convention and the foreign key convention.
The primary key convention will identifiy any property with the name Id or [Class Name]+Id as the primary key for a given class. The foreign key convention will identify any property named [Some Other Class Name]+Id as a foreign key reference. So using the example you posted, Code First recognizes the Blog.Id property as the primary key for the Blog class and it also recognizes that the Post.BlogId property is a foreign key reference to the Blog class.
Here's the documentation on EF4.1 Model Configuration Conventions:
http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions%28v=vs.103%29.aspx
hapax_legome...
Member
316 Points
357 Posts
Re: Code First - how to create simple relationship
Jun 22, 2011 07:00 PM|LINK
chohmann, that was super helpful. Thanks a ton!
chohmann
Star
9385 Points
1644 Posts
Re: Code First - how to create simple relationship
Jun 22, 2011 07:31 PM|LINK
My pleasure. Happy to help.