标签:
随着EntityFramework的发展,原先的三种方式,{Code First ,Model First,Database First } CodeFirst基本摆脱了另外两种方式
public class Blog { public string ID { get; set; } public string BlogName { get; set; } public string BlogAuthor { get; set; } public virtual List < Post> Posts { get ; set ; } //导航属性 } public class Post { public string ID { get; set; } public string PostName { get; set; } public string Content { get; set; } public virtual Blog BelongBlog { get; set; } //导航属性 }
public class BlogContext : DbContext { private readonly static string CONNECTIONSTRING = "Data Source=.;Initial Catalog=EFBlog;User ID=sa;Password=11111;Integrated Security=True;Pooling=False"; public BlogContext():base (CONNECTIONSTRING) { } public DbSet < Blog> Blogs { get ; set ; } public DbSet < Post> Posts { get ; set ; } }
public static void Main( string [] args) { BlogContext ctx = new BlogContext (); ctx.Blogs.Add( new Blog { ID = "1", BlogName = "Jason‘s Blog" , BlogAuthor = "Jason", Posts = new List < Post> { new Post {ID="1" ,PostName= "shenwei",Content= "今天的阳光真好,要晒被子!" }, new Post {ID="2" ,PostName= "shenwei",Content= "今天下雨了,都不能出去玩!" } } }); ctx.Blogs.Add( new Blog { ID="2" ,BlogName="Code Home",BlogAuthor= "zhangxiaomao"}); ctx.SaveChanges(); Console .ReadKey();
标签:
原文地址:http://www.cnblogs.com/JasonShenW/p/5084066.html