!请不要重定向到本文,因为它没有解决下面描述的问题.
假设我们在数据库中有这样的表:
SomeTable
我们已经配置了Linq2Sql数据上下文.我们为SomeTable配置了一个实体:OnLoaded方法以DT的DateTimeKind变为Utc(最初是未指定的)的方式修改DT.
现在问题是:
如果我们使用整个实体请求数据,则调用OnLoaded方法:
From x In ourDataContext.SomeTable Select x
Run Code Online (Sandbox Code Playgroud)
但是如果我们只请求表的一部分(因此生成一个匿名类型),则不会调用OnLoaded:
From x In ourDataContext.SomeTable Select x.DT
Run Code Online (Sandbox Code Playgroud)
很明显,OnLoaded是在SomeTable实体中定义的,而不是匿名类型.
目前我考虑创建可替代匿名类型的自定义实体.但也许有人有更好的解决方案?
我有一个非常简单的例子:WPF表单应用程序,包含带有数据的字典:
Dim dict As New Collections.Generic.Dictionary(Of String, String)
Private Sub MainWindow_Loaded() Handles Me.Loaded
dict.Add("One", "1")
dict.Add("Two", "2")
dict.Add("Three", "3")
lst1.ItemsSource = dict
End Sub
Run Code Online (Sandbox Code Playgroud)
在表单上我有一个ListBox(名为"lst1"),它使用"dict"作为项目源:
<ListBox x:Name="lst1">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Value}"
TextSearch.Text="{Binding Path=Key, Mode=OneWay}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
我还有一个非绑定的ListBox,手动预先填充值:
<ListBox>
<Label TextSearch.Text="One" Content="1" />
<Label TextSearch.Text="Two" Content="2" />
<Label TextSearch.Text="Three" Content="3" />
</ListBox>
Run Code Online (Sandbox Code Playgroud)
因此,当我启动应用程序时,它看起来像这样:

如果我尝试通过键入"one","two"或"three"来使用键盘导航项目,我只能在非绑定列表框中成功.绑定列表框失败.
一些评论:1.)如果我在绑定列表框中按"[",焦点会以循环方式从一个项目更改为一个项目:它从1到2,从2到3,从3到1,再从1再到2 2.)我已经使用Snoop检查了应用程序.我在绑定和非绑定列表框之间找到了一个区别.两个列表框都在Label控件上设置了TextSearch.Text属性(在ItemsPresenter内).但对于非约束情况:TextSearch.Text属性的"值源"是"本地".对于约束情况:"value source"是"ParentTemplate".
PS(和NB) 我知道我可以在列表框中使用TextSearch.TextPath,但这不是我需要的:)另外,为ListViewItem设置TextSearch.Text属性(通过使用Style)也无济于事.
在这里和那里,人们一直在谈论由于未发布的事件监听器而发生的内存泄漏.我认为这是一个非常重要的问题.非常严肃而且非常重要......如果确实存在的话.
我曾尝试自己来重现问题,但我所有的努力都失败了:我不能让我的应用程序泄漏内存:(虽然这听起来不错,我还是很担心:也许我失去了一些东西.
那么也许有人可以提供一个非常简单的源代码示例,导致内存泄漏?
我创建了一个小型VB.NET应用程序作为演示:它包含一个Windows窗体和一个类.
Windows窗体:它有一个集合对象(名为"c")和两个按钮:一个用于向集合添加10个项目,另一个用于清除集合:
Public Class Form1
Dim c As New Collection
Private Sub btnAddItem_Click(sender As System.Object, e As System.EventArgs) Handles btnAddItem.Click
For i As Integer = 1 To 10
Dim m As New MyType
c.Add(m)
Next
Me.Text = c.Count
End Sub
Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
For Each item As MyType In c
item.Dispose()
Next
c.Clear()
Me.Text = c.Count
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
MyType类:它有很大的m_Image对象,这个对象很大,所以你可以看到你的内存真正被MyType实例占用了:)
Imports System.Drawing
Public Class MyType
Implements IDisposable
Private m_Image …Run Code Online (Sandbox Code Playgroud) 通过使用include标签,我试图将我的代码的注释放在单独的文件"docs.xml"中.但它不起作用.我一直在尝试C#和VB.NET项目.
这是我的评论文件:
<?xml version="1.0" encoding="utf-8" ?>
<d>
<summary>Demo summary</summary>
</d>
Run Code Online (Sandbox Code Playgroud)
我有一个ABC类,有一个属性Demo.在我写这个属性之前:
/// <include file="docs.xml" path="d/*" />
Run Code Online (Sandbox Code Playgroud)
或者在VB.NET中:
''' <include file="docs.xml" path="d/*" />
Run Code Online (Sandbox Code Playgroud)
但是,ABC.Demo的摘要永远不会出现在InteliSense /对象浏览器/其他项目中(如果我引用我的项目).
我有一种强烈的感觉,我在这里遗漏了一些东西.
PS我试过遵循XML文件的"path [@ name =]"模式,但它没有帮助.
我正在构建一个编辑器模板,它将接受htmlAttributes对象。
例子很简单。
在主视图中,我有类似的东西
@Html.EditorFor(Function(x) x.SomeProperty, New With {.htmlAttributes = New With {.class = "form-control"}}).
Run Code Online (Sandbox Code Playgroud)
在编辑器模板中,我需要class="form-control"编写属性。
如果在模板中我使用了类似的东西@Html.TextBoxFor(Function(x) x, ViewData("htmlAttributes")),那么一切都很好。但是如果我手动构建一个标签,我没有办法输出htmlAttributes,即我构建<input type="text" {htmlAttributes should be here} />
是否有任何公共方法可以正确呈现标签内的 HTML 属性?
这是Automapper专业人士的问题.我已经尝试了几天的查询映射 - 并且没有运气.似乎Automapper并不打算以我想要的方式使用它.但也许我错了.所以这是问题......
我有这样的课程:
我想在我的数据访问层中编写一个通用的读取函数,如下所示:
IEnumerable<CatDto> Read(IQueryable<CatDto> query) {
// here "query" is converted
// to Entity Framework query by means of AutoMapper,
// EF query gets executed,
// I convert EF entities (Cat) back to CatDto - this is not essential
// result is returned
}
Run Code Online (Sandbox Code Playgroud)
我将以不同的方式调用此函数.例:
var q = new ObjectModel.Collection(Of CatDto)).AsQueryable();
q = q.Where(c => c.Toys.Count() > 1);
var someResultVar = Read(q);
Run Code Online (Sandbox Code Playgroud)
到目前为止,任何实现此类行为的尝试都失败了.我想知道Automapper是否是这里的帮手还是我完全错了?
我有一个Asp.net Core方法具有以下定义。
[HttpPost]
public IActionResult Upload(IFormFile file)
{
if (file == null || file.Length == 0)
throw new Exception("file should not be null");
var originalFileName = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
file.SaveAs("your_file_full_address");
}
Run Code Online (Sandbox Code Playgroud)
我想为此功能创建XUnit Test,该如何模拟IFormFile?
更新:
控制器:
[HttpPost]
public async Task<ActionResult> Post(IFormFile file)
{
var path = Path.Combine(@"E:\path", file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
Xunit测试
[Fact]
public async void Test1()
{
var file = new Mock<IFormFile>();
var sourceImg = File.OpenRead(@"source …Run Code Online (Sandbox Code Playgroud) 我使用实体框架核心 2.0.1。在我的 EF 查询中,我需要将几个简单的查询组合成一个 UNION 查询。所以,对于一个非常基本的例子,我有几个类似的查询
using (var dbc = new ItemsDbContext("some-connection-string"))
{
var q1 = dbc.Items.Where(a => a.ItemType == "A");
var q2 = dbc.Items.Where(a => a.ItemType == "B");
var myList = q1.Union(q2).ToList();
}
Run Code Online (Sandbox Code Playgroud)
我希望将这样的 SQL 语句作为单个查询对我的 SQL Server 数据库执行:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'A'
UNION
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'B'
Run Code Online (Sandbox Code Playgroud)
但是,正如我在 SQL Server Profiler 中看到的那样,执行了两个单独的查询:
查询 1:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType = N'A'
Run Code Online (Sandbox Code Playgroud)
查询 2:
SELECT I.*
FROM [dbo].[Items]
WHERE I.ItemType …Run Code Online (Sandbox Code Playgroud) .net ×4
c# ×3
asp.net-core ×1
asp.net-mvc ×1
automapper ×1
binding ×1
datatemplate ×1
dto ×1
ef-core-2.0 ×1
entities ×1
events ×1
include ×1
linq-to-sql ×1
memory ×1
memory-leaks ×1
moq ×1
razor ×1
sql-server ×1
text-search ×1
unit-testing ×1
vb.net ×1
wpf ×1
xml ×1
xunit ×1