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