小编Isk*_*yev的帖子

Entity Framework的Find方法如何工作?

我正在学习实体框架,并且面对一些我无法理解的Find()方法.
摘自Julia Lerman的书"编程实体框架:代码优先"

public class Destination
{
    public int DestinationId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
    public string Description { get; set; }
    public byte?[]  Photo { get; set; }
    public ICollection<Lodging> Lodgings { get; set; }

    public Destination()
    {
        Lodgings = new List<Lodging>();
    }
}

public class Lodging
{
    public int LodgingId { get; set; }
    public string Name { get; set; }
    public string …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

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

如何在同一时间运行ASP.NET MVC和WebAPI项目?

我正在开发一个具有n层架构的复杂Web平台.解决方案中有两个项目:PresentationLayer.WebSite和APILayer.Restful.

本地数据库存储在PresentationLayer.WebSite内的App_Data中.我不知道如何实现以下操作:(目前我在调试模式下工作).

  1. 将Restful项目中的连接字符串设置为Presentation Project.我应该使用绝对路径吗?他们应该使用相同的DB.当然,在发布时刻DB会被放置在某个服务器上,但现在该怎么办?
  2. 如何在同一时间运行两个项目?我知道如何从另一个人那里调用一个项目的控制器,但是如何运行它来实现它?例如:在Chrome中我调用User\MyProfile?....(演示文稿),在Firefox中我调用Api\User\GetUserById ...(Rest).

c# asp.net asp.net-mvc asp.net-web-api

7
推荐指数
1
解决办法
1473
查看次数

与 IMediatR 库一起使用时,无法在类型化客户端中注入 HttpClient

根据MSDN 中ASP.NET Core 2.2 文档提供的示例,可以通过将以下行添加到 Startup.cs 将 HttpClient 注入类型化客户端(服务类):

// Startup.cs
services.AddHttpClient<GitHubService>();
Run Code Online (Sandbox Code Playgroud)

从控制器类来看,它看起来像(从现在开始我将使用 GitHub 作为域模型的简化):

// GitHubController.cs
public class GitHubController : Controller
{
    private readonly GitHubService _service;
    public GitHubController(GitHubService service)
    {
        _service = service;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我在我的项目中使用了MediatR库,所以我的项目结构看起来有点不同。我有 2 个项目 - GitHubFun.Api、GitHubFun.Core - ASP.NET Core 2.2 API 项目和 .NET Core 2.2 类库。

我的控制器:

// GitHubController.cs
public class GitHubController : Controller
{
    private readonly IMediator _mediator;
    public GitHubController(IMediator mediator)
    {
        _mediator= mediator;
    }

    public async Task<IActionResult> GetGitHubRepositoryInfo(
        GetGitHubRepositoryCommand …
Run Code Online (Sandbox Code Playgroud)

c# mediatr asp.net-core-2.0

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

为什么属性只能包含C#中接口类型的setter?

我想知道为什么在C#中不允许在类类型的自动生成属性中仅使用{set}属性,如:

class Person {
    public string Name { set; } // Compile-time error
}
Run Code Online (Sandbox Code Playgroud)

但是,在接口类型中允许:

interface IPerson {
    string Name {set;} //Allowed
}
Run Code Online (Sandbox Code Playgroud)

在这里读到类似的问题,这是非常罕见的做法 - 我理解,但我想知道为什么CLR甚至允许这样做?

c# class

5
推荐指数
1
解决办法
126
查看次数

实体框架中接口的多重继承?

在此清楚地说明如何通过使用Interface在C#中实现“多重继承” 。但是,我不知道如何在Entity Framework Code First Workflow中实现相同的功能。

提供的代码使事情变得清晰:

public abstract class DomainObject {
       // Every business model must have this fields
       public Guid Id {get;set;}
       public string SystemCode {get;set;}

}
Run Code Online (Sandbox Code Playgroud)

这里还有一些可选的抽象类,例如:

public abstract class MultiTitleObject : DomainObject {
       public string TitleRu { get; set; }
       public string TitleEn { get; set; }
}

public abstract class ManageableByAdminObject : DomainObject {
       public bool isVisibleOnSite {get;set;}
       public bool isDeletedByAdmin {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

假设我有一个类,该类通常需要同时具有MultiTitleObject和ManageableByAdminObjectDomainObject的字段。由于C#不支持多重继承,因此我可以执行以下操作: …

c# inheritance entity-framework

5
推荐指数
0
解决办法
561
查看次数

.NET StringBuilder和逐字字符串文字

在我的应用程序中,有一个与PdfSharp一起使用的类可以生成一些PDF报告.我将输出文件夹指定为带有逐字的字符串

string file_path = @"D:\Intranet\Students\DailyMarks\";
Run Code Online (Sandbox Code Playgroud)

还有一个StringBuilder,它根据一些ID和DateTime生成文件名:

... sb.Append(document.Type); sb.Append(document.Id); sb.Append(DateTime.Now.ToShortString());
Run Code Online (Sandbox Code Playgroud)

最后我做了以下几点

file_path + sb.toString();
Run Code Online (Sandbox Code Playgroud)

但是我的应用程序遇到了异常.调试会话后,我看到实际上我的file_path

file_path = "D:\\Intranet\\Students\\DailyMarks\\...";
Run Code Online (Sandbox Code Playgroud)

据我所知,它是在源文件与StringBuilder的toString()调用连接之后发生的.我试图用这样的东西替换file_path字符串:

file_path = file_path.Replace(@"\\",@"\");
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我哪里做错了?

c# stringbuilder verbatim-string

4
推荐指数
1
解决办法
522
查看次数

一对一可选的自引用关系

我面临以下领域模型

public class Transaction
{
    public Guid Id { get; set; }
    public decimal TransactionSum { get; set; }
    public decimal TransactionCurrencyConversionRatio { get; set; }
    public bool IsTransactionApprovedBySystem { get; set; }
    public bool IsTransactionApprovedBySender { get; set; }
    public DateTime TransactionInitiatedDate { get; set; }
    public DateTime ? TransactionApprovedDate { get; set; }
    public TransactionType TransactionType { get; set; }

    public Account SenderAccount { get; set; }
    public Account ReceiverAccount { get; set; }
    public Guid SenderAccountId { …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core

4
推荐指数
1
解决办法
1618
查看次数

HttpRequestMessage的Content-Type标头中的奇怪行为

我试图通过C#(.NET Core 2.2.104)向主体中的外部Web服务发出带有application / json的HTTP POST请求。

我已经阅读了SO中的所有类似问题,并编写了以下代码:

            SignXmlRequestDto requestBody = new SignXmlRequestDto(p12, model.SignCertPin, model.Data);
            string json = JsonConvert.SerializeObject(requestBody);

            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = ncanNodeUrl,
                Headers =
            {
                { HttpRequestHeader.ContentType.ToString(), "application/json" }
            },
                Content = new StringContent(JsonConvert.SerializeObject(json))
            };

            var response = await httpClient.SendAsync(httpRequestMessage);

            string responseString = await response.Content.ReadAsStringAsync();
Run Code Online (Sandbox Code Playgroud)

我从服务中收到一个错误,它说:“无效的标头Content-Type。请将Content-Type设置为application / json”。在这里有趣的是,如果我模拟了来自Postman的请求,那么一切正常,我得到了成功的响应。在此处输入图片说明

更新:按照@KristófTóth的建议,我将代码修改为:

            var httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = ncanNodeUrl,
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };

            var response …
Run Code Online (Sandbox Code Playgroud)

.net c# http postman

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

实体框架:是否可以从基本配置类继承?

我正在通过 EF CodeFirst 为新数据库建模。这里我有一个抽象基类和从该类继承的每个域模型:

public abstract class Entity
{
    public Guid Id { get; set; }
    public byte [] TimeStamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

还有很多课程,例如:

public class Country : Entity
{
    public string Name { get; set; }
    public string InternationalCode { get; set; }
    public Location Location { get; set; }
    public ICollection<City> Cities { get; set; }

    public Country(string name, string internationalCode, decimal latitude, decimal longitude) 
        : this(name, internationalCode)
    {
        Location.Latitude = latitude;
        Location.Longitude = longitude;
    } …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

0
推荐指数
1
解决办法
1804
查看次数