我正在使用Entity Framework来填充网格控件.有时当我进行更新时,我收到以下错误:
存储更新,插入或删除语句会影响意外的行数(0).自实体加载后,实体可能已被修改或删除.刷新ObjectStateManager条目.
我无法弄清楚如何重现这一点.但它可能与我进行更新的距离有多大关系.有没有人看过这个或有没有人知道错误信息是指什么?
编辑:不幸的是我不再自由地重现我在这里遇到的问题,因为我离开了这个项目并且不记得我是否最终找到了解决方案,如果另一个开发人员修复它,或者我是否解决了这个问题.因此我不能接受任何答案.
有没有办法枚举,切换,添加虚拟桌面和从代码之间移动桌面之间的窗口?优选地,WinAPI.
我有两个代码示例:
一编译
class C {
public virtual object Method2() => throw new NotImplementedException();
}
class D : C {
public override string Method2() => throw new NotImplementedException();
}
Run Code Online (Sandbox Code Playgroud)
另一种则没有
interface A {
object Method1();
}
class B : A {
public string Method1() => throw new NotImplementedException();
// Error CS0738 'B' does not implement interface member 'A.Method1()'. 'B.Method1()' cannot implement 'A.Method1()' because it does not have the matching return type of 'object'. ConsoleApp2 C:\Projects\Experiments\ConsoleApp2\Program.cs 14 Active
}
Run Code Online (Sandbox Code Playgroud)
协变返回类型在 C# 9.0 …

无法连接到net.tcp:// localhost:5051/user.连接尝试持续时间跨度为00:00:02.0600206.TCP错误代码10061:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:5051.
防火墙是块?有什么想法吗?
当我遇到以下代码时,我正在重构一个简单的脚本文件解析器的旧代码:
StringReader reader = new StringReader(scriptTextToProcess);
StringBuilder scope = new StringBuilder();
string line = reader.ReadLine();
while (line != null)
{
switch (line[0])
{
case '$':
// Process the entire "line" as a variable,
// i.e. add it to a collection of KeyValuePair.
AddToVariables(line);
break;
case '!':
// Depending of what comes after the '!' character,
// process the entire "scope" and/or the command in "line".
if (line == "!execute")
ExecuteScope(scope);
else if (line.StartsWith("!custom_command"))
RunCustomCommand(line, scope);
else if …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用ListBox.DataSource = ObservableCollection,但是当我的OC更新时,我无法弄清楚如何让列表框自动更新.我可以在OC上挂钩CollectionChanged事件,但是我需要对列表框做什么才能让它更新?
根据如何在使用NPOI创建的Excel文档中将列设置为"自动调整大小"?我这样做了:
foreach (DataColumn column in dataTable.Columns)
{
int rowIndex = 0;
foreach (DataRow row in dataTable.Rows)
{
HSSFRow dataRow = sheet.CreateRow(rowIndex);
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
rowIndex++;
}
sheet.AutoSizeColumn(column.Ordinal);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.怎么做对吗?
static void Main() {
void TestLocal() => TestLocal("arg");
TestLocal("arg");
}
static void TestLocal(string argument) {
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,本地函数与另一个我想要重载的方法具有相同的名称.但是这个方法在这个函数内部是不可见的,甚至从内部也是如此Main().lambda也是如此:
static void Main() {
Action TestLambda = () => TestLambda("arg");
}
static void TestLambda(string argument) {
}
Run Code Online (Sandbox Code Playgroud)
我明白,为什么lambda隐藏外部方法 - 因为它是一个局部变量,局部变量总是以这种方式工作.但是我没有看到本地函数隐藏的任何原因并且没有重载外部方法 - 它可以非常有用于减少局部范围中的参数,进行某种携带.
为什么本地函数隐藏方法?
编辑:
我可以想象一个复杂的例子
void Bar(short arg) {
Console.WriteLine("short");
}
void Bar(byte arg) {
Console.WriteLine("byte");
}
void Test() {
void Bar(int arg) {
Console.WriteLine("int");
}
Bar(0);
}
Run Code Online (Sandbox Code Playgroud)
参数类型解析已经足够复杂了.如果他们将重载添加到本地方法,对于求职面试来说这将是一项更愚蠢的任务,对编译器制造商来说则是一项更大的任务.还有方法virtual和new方法......
声明为字段的 ValueTuple 类型可以是可变的:
class Foo {
(int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)
或只读:
class Foo {
readonly (int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)
这种(不变)可变性适用于每个成员。我希望它也能扩展到 const 声明:
class Foo {
const (int, int) bar = (0, 1);
}
Run Code Online (Sandbox Code Playgroud)
但是这个语句不能编译。在某些情况下,它是不受欢迎的功能,还是只是未实现的功能?
编辑 好的,我现在明白了。我的问题是基于这样一个假设,即 C# 编译器对 ValueTuples 的处理方式与其他类型不同,关于关键字 readonly。所以,如果它已经是一个例外,为什么不为 consts 设置另一个例外。但是,实际上,这种逻辑似乎适用于所有结构体。所以这行不通:
class Foo {
readonly ExampleStruct field;
void Method() {
field.structField = 2;
}
}
struct ExampleStruct {
public int structField;
}
Run Code Online (Sandbox Code Playgroud) 我有两个例子.在第一种情况下,调试器捕获未处理的异常:
static void Main(string[] args) {
Exec();
}
static void Exec() {
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
并且异常具有完整的堆栈跟踪:
at ConsoleApplication28.Program.Exec()
at ConsoleApplication28.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
第二种情况:
static void Main(string[] args) {
Exec();
}
static void Exec() {
try { …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×4
c#-7.0 ×1
c#-7.3 ×1
c#-9.0 ×1
covariance ×1
excel ×1
exception ×1
lambda ×1
listbox ×1
npoi ×1
parsing ×1
stack-trace ×1
valuetuple ×1
wcf ×1
windows-10 ×1
winforms ×1