asp.net-core-1.0

Asp.net Core Entity Framework cannot find IndexAttribute

时间秒杀一切 提交于 2019-12-03 17:41:05
问题 I receive the following from Visual Studio Code on my Mac, both in IDE and console window after executing "dotnet run": The type or namespace name 'IndexAttribute' could not be found I have a class called Story which I want to use for generating a database with Code First. This class has a primary key marked with KeyAttribute and Author string marked with MaxLengthAttribute, so both of those work (using System.ComponentModel.DataAnnotations). Two more fields, DateTime Date and bool

Post a List<Interface> .net core 1.0

被刻印的时光 ゝ 提交于 2019-12-03 16:13:51
I'm building a dynamic form creator in .net core. A "form" will consist of many different form elements. So the form model will look something like this: public class FormModel { public string FormName {get;set;} public List<IElements> Elements{get;set;} } I have classes for TextBoxElement , TextAreaElement , CheckBoxElement that all implement the IElemets interface. And I have EditorTemplates for each element. The code to render the form works great. Though posting the form does not work because of the List of Interfaces. I've been looking on how to implement a custom model binder, and seen

Create an User in a Console .NET Core Application

浪子不回头ぞ 提交于 2019-12-03 14:10:55
I have a ASP.NET Core 1.0 Solution with 3 projects (Web, Console Application, DataAccessLayer). I use ASP.NET Core Identity and Entity Framework Core (SQL Server - Code First). In my Console Application (Used for background tasks), I want to create users, but how I can have access to UserManager object in a Console Application (Or in a .NET Core Class Library) ? In a controller class, it's easy with Dependency Injection : public class AccountController : Controller { private readonly UserManager<ApplicationUser> _userManager; public AccountController(UserManager<ApplicationUser> userManager,

Dependency Injection with XUnit and ASP.NET Core 1.0

…衆ロ難τιáo~ 提交于 2019-12-03 13:02:29
I am trying to figure out how I can use dependency injection with XUnit. My goal is to be able to inject my ProductRepository into my test class. Here is the code I am trying: public class DatabaseFixture : IDisposable { private readonly TestServer _server; public DatabaseFixture() { _server = new TestServer(TestServer.CreateBuilder().UseStartup<Startup>()); } public void Dispose() { // ... clean up test data from the database ... } } public class MyTests : IClassFixture<DatabaseFixture> { DatabaseFixture _fixture; public ICustomerRepository _repository { get; set; } public MyTests

Use a Identity 2.0 Database to Authenticate a ASP.NET Core 1.0 application

不羁岁月 提交于 2019-12-03 12:10:34
问题 I am trying to a create a new ASP.NET Core 1.0 web application and I want it to use the Authentication tables that I already have set up. These tables were originally created by a ASP.NET 4.6 web application that used Microsoft.ASPNet.Identity.EntityFramework 2.2.0 It looks like things have change in Microsoft.AspNetCore.Identity.EntityFrameworkCore because the new Core 1.0 application throws this error when trying to log in: A database operation failed while processing the request.

Middleware to set response ContentType

不想你离开。 提交于 2019-12-03 10:31:01
In our ASP.NET Core based web application, we want the following: certain requested file types should get custom ContentType's in response. E.g. .map should map to application/json . In "full" ASP.NET 4.x and in combination with IIS it was possible to utilize web.config <staticContent>/<mimeMap> for this and I want to replace this behavior with a custom ASP.NET Core middleware. So I tried the following (simplified for brevity): public async Task Invoke(HttpContext context) { await nextMiddleware.Invoke(context); if (context.Response.StatusCode == (int)HttpStatusCode.OK) { if (context.Request

EF Core Add function returns negative id

戏子无情 提交于 2019-12-03 09:48:34
I noticed a weird thing today when I tried to save an entity and return its id in EF Core Before: After: I was thinking about if it was before calling saveChanges() but it works with another entity with a similar setup. ps: I use unit of work to save all changes at the end. What was the reason? Dawid Rutkowski It will be negative until you save your changes. Just call Save on the context. _dbContext.Locations.Add(location); _dbContext.Save(); After the save, you will have the ID which is in the database. You can use transactions, which you can roll back in case there's a problem after you get

Run NUnit tests in dotnet core

自作多情 提交于 2019-12-03 09:29:11
I am trying to run unit tests for my c# project with dotnet core. I am using docker container for runtime. Dockerfile FROM microsoft/dotnet:0.0.1-alpha RUN mkdir /src WORKDIR /src ADD . /src RUN dotnet restore "NUnit" and "NUnit.Runners" have been added into project.json "version": "1.0.0-*", "compilationOptions": { "emitEntryPoint": true }, "dependencies": { "NETStandard.Library": "1.0.0-rc2-23811", "NUnit": "3.2.0", "NUnit.Runners": "3.2.0" }, "frameworks": { "dnxcore50": { } } Run dotnet restore successfully with the following output ... log : Installing NUnit.ConsoleRunner 3.2.0. log :

Entity Framework Core 1.0 DbContext not scoped to http request

孤者浪人 提交于 2019-12-03 08:42:16
I understood by watching this video with Rowan Miller https://channel9.msdn.com/Series/Whats-New-with-ASPNET-5/06 (at minute 22) that the way of configuring Entity Framework Core (previously known as EF7) into an ASP.NET Core 1.0 app (previously known as ASP.NET 5) in Startup.cs is as follows: public void ConfigureServices(IServiceCollection services) { //Entity Framework 7 scoped per request?? services.AddEntityFramework() .AddSqlServer() .AddDbContext<MyDbContext>(options => { options .UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]); }); //MVC 6 services.AddMvc(); }

Asp.net Core Entity Framework cannot find IndexAttribute

大兔子大兔子 提交于 2019-12-03 06:42:12
I receive the following from Visual Studio Code on my Mac, both in IDE and console window after executing "dotnet run": The type or namespace name 'IndexAttribute' could not be found I have a class called Story which I want to use for generating a database with Code First. This class has a primary key marked with KeyAttribute and Author string marked with MaxLengthAttribute, so both of those work (using System.ComponentModel.DataAnnotations). Two more fields, DateTime Date and bool IsPublished, have IndexAttribute applied (it's a two-column index). I explicitly named it IX_DatePublished,