gpt4 book ai didi

c# - 无法连接到 EntityFramework

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:48 25 4
gpt4 key购买 nike

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;

using MyQuotesApp.models;

using Microsoft.Framework.Runtime;
using Microsoft.Framework.Configuration;
using Microsoft.Data.Entity;

namespace MyQuotesApp
{
public class Startup
{
public IConfiguration Configuration { get; set; }

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath);
configurationBuilder.AddJsonFile("config.json");
configurationBuilder.AddEnvironmentVariables();
Configuration = configurationBuilder.Build();
}

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();

services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<QuotesAppContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"]));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
}

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}

project.json 依赖项

  "dependencies": {
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"EntityFramework.SqlServer": "7.0.0-beta5",
"EntityFramework.Commands": "7.0.0-beta5",
"Microsoft.Framework.Configuration.Json": "1.0.0-beta5",
"Microsoft.Framework.Configuration.EnvironmentVariables": "1.0.0-beta5",
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final"
},

如果我使用 EntityFramework.Core v7.0.0.0-rc1-final,我会收到以下错误:

我在 options.UseSqlServer 下有一条红线:

.AddDbContext<QuotesAppContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"]));

错误说:

The type 'EntityOptionsBuilder' is defined in an assembly that is not referenced. You must add a reference to assembly 'EntityFramework.Core, Version=7.0.0.0, Culture=neutral, PublicKeyToken=null'.

如果我使用 EntityFramework.Core v7.0.0-beta5,我会收到以下错误:

这一行AddEntityFramework下的一条红线:

services.AddEntityFramework()

错误如下:

'IServiceCollection' does not contain a definition for 'AddEntityFramework' and no extension method 'AddEntityFramework' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

当我在网上查看时,似乎我找到的所有用于设置连接的教程都涉及一条与我的类似的行,它会产生该错误。也许这是一个简单的问题,我缺乏 c# 经验使我无法知道明显的答案?我怎样才能让这个愚蠢的东西连接到我的 SQL 数据库?

最佳答案

尝试删除与 EF 相关的所有内容,然后将其放入:

"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",

EF 应该自行解决其他依赖项...同样在您的包配置中,您为某些 EF 依赖项运行了不同的版本,这也可能是一个问题...很高兴我能提供帮助:)

关于c# - 无法连接到 EntityFramework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com