环境 : 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>