我正在尝试使用Visual Studio中的Entity Framework创建一个数据库优先的ASP.NET MVC应用程序.
从空白项目模板开始,我打开服务器资源管理器并尝试添加数据连接.我使用Microsoft SQL Server作为数据源,使用"(localdb)\ v11.0"作为服务器名称.这给了我错误:"连接字符串中的数据源值指定未安装的SQL Server实例.要解决此问题,请选择安装SQL Server的匹配实例或修改连接中的数据源值串."
我正在关注的教程(以及我用谷歌搜索的其他几个地方)提到localdb是随Visual Studio一起安装的.无论哪种方式,我还安装了在这里找到的LocalDB ,但它没有显示出来.
我已尝试按照此帖中的说明操作,但收到错误"无法创建自动实例".
如何让Visual Studio连接到LocalDB?
我正在使用Identity Core 1.0与ASP.NET MVC Core 1.0和Entity Framework Core 1.0创建一个简单的用户注册系统,以本文为出发点,我正在尝试添加用户角色.我可以添加用户角色,但我无法编辑它们.这是以下Edit行动RolesController:
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(IdentityRole role)
{
try
{
_db.Roles.Attach(role);
_db.Entry(role).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
catch (Exception ex)
{
Console.WriteLine(ex);
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是相应视图中的表单:
@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
@{
ViewBag.Title = "Edit";
}
<h2>Edit Role</h2>
<hr />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id)
<div>Role name</div>
<p>@Html.TextBoxFor(model => model.Name)</p>
<input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)
新角色名称不保存到数据库,我得到以下异常: Database operation expected to affect 1 row(s) but …
c# entity-framework asp.net-identity entity-framework-core asp.net-core
我正在使用.NET Core,Identity Core和MVC Core创建用户注册系统.我能够在数据库中创建用户并创建角色.
这是视图中的表单,可让我选择一个用户并选择要添加的角色:
@using (Html.BeginForm("AddRoleToUser", "Roles"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<p>
Username : @Html.DropDownList("UserName", (IEnumerable<SelectListItem>)ViewBag.Users, "Select ...")
Role Name: @Html.DropDownList("RoleName", (IEnumerable<SelectListItem>)ViewBag.Roles, "Select ...")
</p>
<input type="submit" value="Save" />
}
Run Code Online (Sandbox Code Playgroud)
这些下拉列表中填充了数据库中已存在的用户和角色.它们允许我选择Users,以及我已经创建的角色的名称.例如,我有一个名为"admin"的角色,这个表单让我选择字符串"admin".
这是处理向用户添加角色的操作:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddRoleToUser(string UserName, string RoleName)
{
try
{
ApplicationUser user = _db.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
await _userManager.AddToRoleAsync(user, RoleName);
PrepopulateDropDownMenus();
ViewBag.ResultMessage = "Role created successfully!";
return View("Manage", "Roles");
}
catch (Exception ex)
{
Console.WriteLine(ex);
return View("Manage");
}
}
Run Code Online (Sandbox Code Playgroud)
该操作永远不会将该角色添加到用户,并且该异常显示"Role"ADMIN"不存在".没有内在的例外.我已经尝试将RoleName动作参数转换为全部大写,但它仍然没有找到角色.我也尝试使用角色ID而不是名称,这也是不成功的. …
我正在尝试反序列化从 RestSharp 调用 API 返回的 JSON。
这是一个 C# 控制台应用程序。到目前为止,这是我的代码:
using System;
using RestSharp;
using RestSharp.Authenticators;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using RestSharp.Deserializers;
using System.Collections.Generic;
namespace TwilioTest
{
class Program
{
static void Main(string[] args)
{
var client = new RestClient("https://api.twilio.com/2010-04-01");
var request = new RestRequest("Accounts/{{Account Sid}}/Messages.json", Method.GET);
request.AddParameter("To", "{{phone number}}");
request.AddParameter("From", "{{phone number}}");
client.Authenticator = new HttpBasicAuthenticator("{{account sid}}", "{{auth token}}");
var response = client.Execute(request);
var jsonResponse = JsonConvert.DeserializeObject(response.Content);
Console.WriteLine(jsonResponse);
}
}
}
Run Code Online (Sandbox Code Playgroud)
响应是 JSON 格式的消息列表,键包括"to"、"from"、"body"和 …
我正在尝试将单元测试与 XUnit 一起用于 ASP.NET v5 MVC v6 应用程序。我可以对工作方法进行简单的单元测试。我想测试控制器。现在,我有一个带有 Index 操作的 HomeController,它返回 Home/Index 视图。我想测试 Index 视图是否是返回的视图。
这是我当前的测试文件:
using Microsoft.AspNet.Mvc;
using Xunit;
using XUnitWithMvcSample.Controllers;
namespace XUnitWithMvcSample.Tests
{
public class Tests
{
private HomeController _homeController;
public Tests()
{
_homeController = new HomeController();
}
[Fact]
public void IndexActionReturnsIndexView()
{
var result = _homeController.Index() as ViewResult;
System.Console.WriteLine(result);
Assert.Equal("Index", result.ViewName);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是控制器/HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace XUnitWithMvcSample.Controllers
{
public class HomeController : Controller
{
public IActionResult Index() …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET MVC 5创建一个MVC应用程序,它使用Entity Framework来处理数据库.
这是一个简单的应用程序,具有CRUD功能,可以将项目添加到带有数据库的列表中.我可以创建项目并保存到数据库,现在我想查看特定项目的页面.
我在以前的应用程序中使用对象Find上的方法完成了这个DbSet,但该方法是其中的一部分System.Data.Entity.我能够以这种方式创建应用程序,但是使用ASP.NET 5,看起来我需要以不同方式配置数据库连接,使用以下行Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddMvc();
}
Run Code Online (Sandbox Code Playgroud)
这要求我使用Microsoft.Data.Entity而不是System.Data.Entity.
这是MyDbContext班级:
public class MyDbContext : DbContext
{
public virtual DbSet<TestModel> TestModels { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=EmptyMVCAuthentication01;integrated security=True;");
}
}
Run Code Online (Sandbox Code Playgroud)
我希望该操作能够查看特定页面以执行以下操作:
public IActionResult Details(int id)
{
Course course= db.Courses.Find(id);
return View(course);
}
Run Code Online (Sandbox Code Playgroud)
(db上面是私人实例MyDbContext)
但是,没有Find方法Microsoft.Data.Entity …
SQL Server 新手 - 我正在尝试使用 SQLCMD 实用程序通过 PowerShell 访问我的计算机上的 localdb\MSSQLLocalDB 服务器。我使用的是 PowerShell v5、.NET v5.0,服务器名称是(localdb)\MSSQLLocalDB我在 Microsoft SQL Server Management Studio 2014 中连接到它时的名称。
PS C:\> sqlcmd -S localdb\MSSQLLocalDB并PS C:\> sqlcmd -S .\localdb\MSSQLLocalDB导致此错误:
Sqlcmd:错误:适用于 SQL Server 的 Microsoft ODBC 驱动程序 11:SQL Server 网络接口:定位指定的服务器/实例时出错 [xFFFFFFFF]。
我在 Management Studio 中查询了服务器名称,并在上面的命令SELECT @@ServerName中使用了该名称-S,并得到了相同的错误。
PS C:\> sqlcmd -S localdb给出这个错误:
Sqlcmd:错误:适用于 SQL Server 的 Microsoft ODBC 驱动程序 11:命名管道提供程序:无法打开与 SQL Server 的连接 [53]
其他注意事项:我可以使用以下连接字符串连接到服务器并在 C# 控制台应用程序中使用名为testdb01的数据库:System.Data.SqlClient
"Data Source=(localdb)\\mssqllocaldb;Initial …Run Code Online (Sandbox Code Playgroud) c# ×5
sql-server ×3
asp.net ×2
asp.net-core ×2
localdb ×2
asp.net-mvc ×1
json ×1
powershell ×1
restsharp ×1
sqlcmd ×1
unit-testing ×1
xunit ×1