小编Yur*_* N.的帖子

在Entity Framework Core中使用SQL视图

例如,我有这样的模型:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
} 
Run Code Online (Sandbox Code Playgroud)

我想在ImageView视图中返回UrlImage.

我在哪里需要创建和定义SQL视图?

c# entity-framework entity-framework-core

43
推荐指数
5
解决办法
6万
查看次数

在Entity Framework Core中包含集合

例如,我有这些实体:

public class Book
{
    [Key]
    public string BookId { get; set; }
    public List<BookPage> Pages { get; set; }
    public string Text { get; set; }
} 

public class BookPage
{
    [Key]
    public string BookPageId { get; set; }
    public PageTitle PageTitle { get; set; }
    public int Number { get; set; }
}

public class PageTitle
{
    [Key]
    public string PageTitleId { get; set; }
    public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我只知道BookId,我应该如何加载所有PageTitles?

这是我试图这样做的方式:

using (var dbContext …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core

23
推荐指数
1
解决办法
1万
查看次数

在Entity Framework Core中保存多对多关系

例如,我有3个类,我用它们用于多对多关系:

public class Library
{
    [Key]
    public string LibraryId { get; set; }
    public List<Library2Book> Library2Books { get; set; }
}

public class Book
{
   [Key]
   public string BookId { get; set; }
   public List<Library2Book> Library2Books { get; set; }
}

public class Library2Book
{
    public string BookId { get; set; }
    public Book Book { get; set; }

    public string LibraryId { get; set; }
    public Library Library { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

它们配置为ApplicationDbContext:

protected override void …
Run Code Online (Sandbox Code Playgroud)

c# many-to-many entity-framework-core

23
推荐指数
1
解决办法
2万
查看次数

ASP.NET Core中外部类库的本地化

我有两个项目:

  • MyWebApp - ASP.NET核心Web API
  • MyServices - .NET Core类库,包含上述项目的有用服务

如何IStringLocalizerMyServices中添加本地化?.resx文件必须在哪里?

c# dll .net-core asp.net-core asp.net-core-webapi

15
推荐指数
3
解决办法
5410
查看次数

在Asp.Net Core中动态更改连接字符串

我想在控制器中更改sql连接字符串,而不是在ApplicationDbContext中.我正在使用Asp.Net Core和Entity Framework Core.

例如:

public class MyController : Controller {
    private readonly ApplicationDbContext _dbContext
    public MyController(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    private void ChangeConnectionString()
    {
    // So, what should be here?
    } }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

c# asp.net entity-framework-core asp.net-core

14
推荐指数
6
解决办法
3万
查看次数

JavaScript中基于相对路径导入文件

我有这样的项目结构:

- MyApp
`- firstFolder
 `- firstFile.js
`- secondFolder
 `- thirdFolder
  `- thirdFile.js
Run Code Online (Sandbox Code Playgroud)

我如何firstFile.js从导入thirdFile.js

import myFunc from '../firstFolder/firstFile';in 之类的东西thirdFile.js不起作用。

javascript commonjs requirejs

11
推荐指数
1
解决办法
2万
查看次数

无法为 ApplicationDbContext 中的类型解析服务

当我尝试添加迁移时dnx ef migrations add Mig,控制台中出现以下异常:

尝试激活“NewLibrary.Models.ApplicationDbContext”时无法解析“Microsoft.AspNet.Http.IHttpContextAccccessor”类型的服务。

我的ApplicationDbContext

public class ApplicationDbContext : DbContext
{
    private readonly IHttpContextAccessor _accessor;

    public ApplicationDbContext(IHttpContextAccessor accessor)
    {
        _accessor = accessor;
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么问题?

我应该如何正确地向ApplicationDbContext构造函数添加依赖项?

c# entity-framework-core asp.net-core

6
推荐指数
1
解决办法
1万
查看次数

跳过并接受 Entity Framework Core

我有简单的 POCO 类:

public class Library
{
    [Key]
    public string LibraryId { get; set; }

    public string Name { get; set; }

    public List<Book> Books { get; set; }
}

public class Book
{
    [Key]
    public string BookId { get; set; }

    public string Name { get; set; }

    public string Text { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有查询,返回包含已包含书籍的图书馆:

dbContext.Set<Library>.Include(x => x.Books);
Run Code Online (Sandbox Code Playgroud)

我试图跳过 5 个库,然后选择其中的 10 个:

await dbContext.Set<Library>.Include(x => x.Books).Skip(5).Take(10).ToListAsync();
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试在此查询上执行SkipTake方法时,它返回没有包含书籍列表的图书馆。

如何使用SkipTake保存以前包含的实体?

c# entity-framework entity-framework-core

6
推荐指数
1
解决办法
2万
查看次数

使用提交按钮传递附加数据

我有表格,有两个按钮:

 <form action="/Book/Crud" method="post">
        <input type="text" name="bookInput" />  
        <button type="submit">Save</button>
        <button type="submit">Save and exit</button>
    </form>
Run Code Online (Sandbox Code Playgroud)

我如何传递给我的控制器,或者其他值,这取决于按钮?
<button type="submit" ?????? >Save</button>这里应该有什么?

我正在使用 Asp.Net Core 和这个控制器:

public IActionResult Crud(string bookInput)
{
    //removed code for brevity
}
Run Code Online (Sandbox Code Playgroud)

html asp.net-core

6
推荐指数
1
解决办法
1万
查看次数

在 Asp.Net Core 中使用 POST 方法重定向到 URL

我有简单的 url 重写器:

    private static void RedirectToAPI(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        if (request.Path.Value.StartsWith("/apiendpoint", StringComparison.OrdinalIgnoreCase))
        {           
            var json = JsonConvert.SerializeObject(request.Path.Value
                .Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                .Skip(1));
            var response = context.HttpContext.Response;

            response.Headers[HeaderNames.Location] = $"/custom";
            response.StatusCode = StatusCodes.Status301MovedPermanently;
            context.Result = RuleResult.EndResponse;
            using (var bodyWriter = new StreamWriter(response.Body))
            {
                bodyWriter.Write(json);
                bodyWriter.Flush();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是,当它重定向到/custom url 时,请求有方法 GET,而这个方法需要 POST。

比如发送 GET 请求到 url /apiendpoint/first/second/third,然后 rewriter 响应重定向,相应地,下面的请求必须是 POST 方法,但现在,它是 GET。

如何更改请求方法,即在 url 重写器响应之后?

c# url redirect url-rewriting asp.net-core

6
推荐指数
1
解决办法
1万
查看次数