学习 ef 6 的碰到的一些问题


环境 : vs2010  / nuget 2.8 /  ef6.1.3 / 数据库环境 为 windows sqlserver 2008 标准版



在安装NuGet时,遇到了一个错误


Error: The Package Manager Console requires PowerShell 2.0 runtime, which is not detected on this machine.
Please install the PowerShell 2.0 from http://support.microsoft.com/kb/968929 and restart Visual Studio.




---------------------------
Windows Management Framework Core Setup Error
---------------------------
Setup cannot continue because your system contains an incompatible version of PowerShell or WinRM. Please uninstall PowerShell and WinRM and run setup again.
---------------------------
确定   
---------------------------

解决办法 

解决方案:卸载PowerShell 1.0  注意 显示更新 要打勾


官网示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace StudyProject
{
   #region CodeFirst
   class Program
   {
      static void Main(string[] args)
      {
         #region  read and write 

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

         #endregion
      }
   }

   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 DbSet<Blog> Blogs { get; set; }
      public DbSet<Post> Posts { get; set; }
   } 


   #endregion
   
}


运行出现 如下错误:



在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider:

 SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)




解决方案:

   public class BloggingContext : DbContext
   {
      public BloggingContext() : base("BloggingCompactDatabase") {  }

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

app.config 更改为如下


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
       
      </parameters>
    </defaultConnectionFactory>-->
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>


  <connectionStrings>
   
    <add name="BloggingCompactDatabase"
             providerName="System.Data.SqlClient"
         connectionString="Data Source=localhost;Initial Catalog=ef6;Persist Security Info=false;User ID=sa; Password=sa.123" />
  </connectionStrings>


 
  
</configuration>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值