在下面的示例代码中,我在执行以下操作时遇到以下异常db.Entry(a).Collection(x => x.S).IsModified = true
:
System.InvalidOperationException: '无法跟踪实体类型 'B' 的实例,因为另一个具有键值 '{Id: 0}' 的实例已被跟踪。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。
为什么不添加而不是附加 B 的实例?
奇怪的是,文档IsModified
没有指定InvalidOperationException
为可能的例外。无效的文档或错误?
我知道这段代码很奇怪,但我写它只是为了了解 ef core 在一些奇怪的 egde 情况下是如何工作的。我想要的是解释,而不是解决方法。
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
public class A
{
public int Id { get; set; }
public ICollection<B> S { get; set; } = new List<B>() { new B {}, new B {} };
}
public class B
{
public int Id { get; …
Run Code Online (Sandbox Code Playgroud) 我正在使用 TLSharp。我的目标是将文件发送给用户。我创建了 ASP.NET Core Web API 服务并在需要发送文件时发出 HTTP 请求。
它适用于一个文件,但每次当我在短时间内收到 2 个或更多请求时,我都会收到错误消息:
System.InvalidOperationException:校验和无效!跳过。
控制器:
[Route("upload/{driveId}")]
public async Task<ActionResult> Upload(string driveId)
{
var ms = new MemoryStream();
var file = service.Files.Get(driveId);
string filename = file.Execute().Name;
await file.DownloadAsync(ms);
ms.Position = 0;
new FileExtensionContentTypeProvider().TryGetContentType(filename, out var mime);
var stream = new StreamReader(ms, true);
await _client.SendFileToBot(filename, mime, stream, driveId);
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
SendFileToBot 方法:
public async Task SendFileToBot(string filename, string mime, StreamReader stream)
{
var found = await client.SearchUserAsync("username", 1);
//find user …
Run Code Online (Sandbox Code Playgroud) 我有一个托管在Azure环境中的服务.我正在使用控制台应用程序使用该服务.在这样做时,我得到"请求的服务,'http://xxxx-d.yyyy.be/Services/zzzzInService.svc'无法激活.有关详细信息,请参阅服务器的诊断跟踪日志."
任何人都可以帮我找到我失踪的东西吗?
该服务定义如下 -
<service name="xxxx.AppServer.Host.Services.yyyyy.zzzzPlugInService"
behaviorConfiguration="MetadataBehavior" xdt:Locator="XPath(//service[@name='xxxx.AppServer.Host.Services.yyyy.zzzzPlugInService'])" xdt:Transform="Replace">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="xxxx.Shared.IntegrationServices.yyyy.IzzzzPlugInService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpsBinding" contract="xxxx.Shared.IntegrationServices.yyyy.IzzzzPlugInService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中使用链接http://xxxx-d.yyyy.be/Services/zzzzInService.svc我得到这些消息 -
system.serviceModel/bindings/basicHttpBinding中的绑定没有名为"basicHttpBinding"的已配置绑定.这是bindingConfiguration的无效值.
资源 :
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="xxxx.Shared.IntegrationServices.zzzzz.IzzzzPlugInService">
Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Linq to NHibernate (尽管这不是这个问题的问题)对我的数据库执行查询,我希望能够测试当前结果IQueryable
实例是否已执行。
调试器知道 myIQueryable
尚未被“调用”,因为它告诉我扩展 Results 属性将“枚举”它。有没有一种方法可以让我以编程方式识别这一点。
我希望这是有道理的 :)
我是 MOQ 的新手,我对设置方法有点困惑。下面的示例显示了我需要测试的一种方法。被测方法返回两个日期的最新时间,因此我创建了两个 datetime 对象并将它们传递给我的函数。我感到困惑的部分是返回调用。这会忽略我的方法中的逻辑并返回我告诉它的内容。例如,如果我说返回(date2),那么无论逻辑如何,断言都会通过。难道我做错了什么?
public virtual DateTime LatestTime(DateTime t1, DateTime t2)
{
if (t1.CompareTo(t2) > 0)
return t1;
return t2;
}
[Test]
[Category("Catalogue service")]
public void TestLatestTimeReturnsCorrectResult()
{
//Arrange
DateTime date1 = new DateTime(2014, 07, 25, 13, 30, 01);
DateTime date2 = new DateTime(2014, 07, 25, 13, 30, 00);
MockCatalogueService.Setup(x => x.LatestTime(date1, date2)).Returns(date2);
//Act
DateTime retDate = MockCatalogueService.Object.LatestTime(date1, date2);
//Assert
Assert.That(retDate == date2);
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找的是一个具有多列的列表框,例如一个用于书籍的列表框,其中每一行都有一个标题、价格、作者。
任何可以给我一些关于如何将项目添加到列表中的指针的人都可以获得加分。我正在猜测
listBox1.Items.Add("Harry Potter", "JK Rowling", 5.99);
Run Code Online (Sandbox Code Playgroud)
行不通?
我正在使用带有以下代码的 AgileMapper:
source.Map().OnTo(target, (options) =>
options.IgnoreSources((options) =>
options.If((value) => value is null)
)
);
Run Code Online (Sandbox Code Playgroud)
但是,编译器抱怨:
表达式树可能不包含模式匹配的“is”表达式
如果我使用它会起作用value == null
,但我想了解为什么is
不起作用?
我需要检查 Enum 中存在的字符串值或不使用Contains
.
public enum Days
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
public class ResultObj
{
public int Id { get; set; }
public string Name { get; set; }
}
var filter = "Wed";
var dayList = Enum.GetValues(typeof(Days))
.Cast<Days>()
.Where(x => Enum.IsDefined(typeof(Days), filter))
.Select(d => new ResultObj
{
Id = (int)d,
Name = d.ToString()
}).ToList();
Run Code Online (Sandbox Code Playgroud)
如果给出“周三”的意思,我需要一个 dayList 结果为{ Id …
public class Irritante : Child
{
/*Fields*/
private int ir_numeroBirras;
private double ir_mediaBirras;
/*Properties*/
public int NumeroBirras
{
get { return ir_numeroBirras; }
set { if (value > 0) ir_numeroBirras = value; }
}
public double MediaBirras
{
get { return ir_mediaBirras; }
set { ir_mediaBirras = value; }
}
//Constructor
public Irritante(string nome, int idade, int numBirras, double mediaDasBirras) : base(nome, idade)
{
NumeroBirras = numBirras;
ir_mediaBirras = mediaDasBirras;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用构造器Irritante与物业NumeroBirras它是忽略了,如果(值> 0) ,这意味着我仍然可以使用客户端的代码添加一个0到这个领域,我不应该是可以的,什么建议吗?我在任何地方都找不到
c# ×7
asp.net-core ×1
class ×1
constructor ×1
enumeration ×1
enums ×1
expression ×1
lambda ×1
linq ×1
mocking ×1
moq ×1
operators ×1
properties ×1
string ×1
telegram ×1
unit-testing ×1
vb.net ×1
wcf ×1