Entity Framework 连接字符串

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>

字符串来源

时间: 2024-12-16 02:57:22

Entity Framework 连接字符串的相关文章

Entity Framework连接MySQL

Entity Framework连接MySQL时:由于出现以下异常,无法生成模型:"表"TableDetails"中列"IsPrimaryKey"的值为DBNull. 解决如下 use 数据库名; set global optimizer_switch='derived_merge=OFF';  

Entity Framework 连接MySQL数据库

1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, V

Entity Framework连接Mysql数据库并生成Model和DAL层

一,安装: 1.开发环境: VS2013与EF6 2.Mysql数据库为:Mysql Server 6.0 3.安装:Mysql for Visual Studio 1.1.1 4.安装 Mysql Connector/Net 6.8.3 GA 二,引用dll: 1.采用Nuget安装EF6.0.2: 2.采用Nuget安装MySql.Data.Entity.EF6 注意:要采用Nuget进行安装,否则可能会缺少相应的dll或者是配置信息 三.配置 web.config或app.config 1

Entity Framework连接MySQL时:由于出现以下异常,无法生成模型:“表“TableDetails”中列“IsPrimaryKey”的值为DBNull.

1.cmd 2.c:\Users\Administrator>cd c:\Program Files\MySQL\MySQL Server 5.7\bin 3c:\Program Files\MySQL\MySQL Server 5.7\bin>mysql -h localhost -u root -p 4.Enter password: 数据库密码 5.use  表名; 6.set global optimizer_switch='derived_merge=OFF'; 7.终于成功!

Entity Framework 6 自定义连接字符串ConnectionString连接MySQL

在开始介绍之前,首先来看看官方对Entity Framework的解释:Entity Framework (EF) is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code that developers

在Entity Framework中重用现有的数据库连接字符串

本文转载:http://www.cnblogs.com/dudu/archive/2011/01/29/entity_framework_connection_string.html Entity Framework使用的连接字符串与ADO.NET是不同的,见下图: 相比于ADO.NET,Entity Framework的连接字符串不仅要存放metadata配置信息,还要存放完整的数据库连接字符串(上图中的"provider connection string"部分). 这样的设计有两

Entity Framework 6+ 连接Mysql

好吧.这个博客开不开的 我感觉.. 都一样了. 前言: 公司改造Sqlserver ->Mysql Sql2016老夫对不住你啊.. 好 前沿结束. 需要的家伙: 1.mysql-for-visualstudio 2.mysql-connector-net 3.mysql-connector-odbc 4.Vs(史上最强大的IDE没有之一 爱谁谁没有面子) 操作步骤: 1.安装我上面说的这些家伙(如果有就跳过) 2.冲一杯茶开始采坑 3.系统提示 没有Ef6+ 邮件Nuget管理搜索Entity

让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.

MVC5 Entity Framework学习之弹性连接和命令拦截

到目前为止,应用程序一直在本地IIS Express上运行.如果你想让别人通过互联网访问你的应用程序,你必须将它部署到WEB服务器同时将数据库部署到数据库服务器 本篇文章中将教你如何使用在将你的应用程序部署到云环境时的Entity Framework 6的非常有价值的两个特性:弹性连接(瞬时错误的自动重试)和命令拦截(捕获所有发送到数据库的SQL查询语句并记录至日志中). 1.启用弹性连接 当你将应用程序部署到Windows Azure时,相应的数据库部也应被部署到Windows Azure S