首页 > 其他 > 详细

Entity Framework 连接字符串

时间:2015-06-22 13:39:34      阅读:236      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace CodeFirstNewDatabaseSample
{

    public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class BloggingContext : DbContext
    {
        public BloggingContext()
            : base("name=BlogContext") { } 

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }


  class Program
   {
       static void Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                // Create and save a new Blog 
                Console.Write("Enter a name for a new Blog: ");
                var name = Console.ReadLine();

                var blog = new Blog { Name = name };
                db.Blogs.Add(blog);
                db.SaveChanges();

                // Display all Blogs from the database 
                var query = from b in db.Blogs
                            orderby b.Name
                            select b;

                Console.WriteLine("All blogs in the database:");
                foreach (var item in query)
                {
                    Console.WriteLine(item.Name);
                }

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }
     }
 }
}

添加基于服务的数据库 

 

 
 public BloggingContext()
            : base("name=BlogContext") { }  // 连上字符串

或者

 public BloggingContext()
            : base("BlogContext") { }  // 连上字符串

或者等等



App.config 添加内容

 

  <connectionStrings>
    <add name="BlogContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\SchoolWork\软件\数据库连接\CodeFirstNewDatabaseSample\CodeFirstNewDatabaseSample\BlogDatamdf.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

字符串来源

 

技术分享

 

Entity Framework 连接字符串

原文:http://www.cnblogs.com/luoxu/p/4593125.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!