您有一些在多线程程序中使用EF的好的建议吗?
我有2层:
如何在每个线程中同步对象上下文?您知道使其运作的好模式吗?
在表中插入数据之前,是否可以获取Id(IDENTITY)的新值?
有可能写出类似的东西:
INSERT INTO Table1
SELECT *GET_NEW_IDENTITY*, Field1, Field2 FROM Table2
Run Code Online (Sandbox Code Playgroud)
我需要Id的值,因为我想在Table1中插入数据,然后,在另一个表中插入数据,该表具有链接到Table1的外键(带有Id)
我想与线程(而不是多进程)并行化本机c ++代码,该代码使用不是线程安全的DLL(Compute.dll).
实际上,我有一种可以并行化的循环:
for(int i = 0; i < x; i++) {
ComputeDLL.DoWork(i); // DLL API Call not thread-safe
}
Run Code Online (Sandbox Code Playgroud)
我找到了一种并行化我的本机代码的方法:克隆并重命名Compute1.dll中的Compute.dll,Compute2.dll,...,ComputeN.dll并通过线程使用一个dll.所以对于链接,以同样的方式,我必须复制Compute1.lib中的Compute.lib,Compute2.lib,...,ComputeN.lib
有了这个解决方案,我必须在我的应用程序中复制代码以定义多个ComputeDLL类:ComputeDLL1,ComputeDLL2,...带有显式静态链接的ComputeDLLN:
#pragma comment(lib,'Compute1.lib'); // for ComputeDLL1.h
#pragma comment(lib,'Compute2.lib'); // for ComputeDLL2.h
etc.
Run Code Online (Sandbox Code Playgroud)
你能告诉我这个解决方案是否有效吗?
在此解决方案中:
还有另一种更清洁的方法来解决我的问题吗?我可以使用LoadLibrary()吗?
谢谢
Nb:我不想使用muli处理,因为在我的实际情况中,我必须将参数中的大数据发送到我的DLL(所以我不想使用文件进行通信,因为我需要性能)而且DoWork是非常快(10毫秒).我希望在没有套接字,Windows消息队列等的情况下轻松地在内存中工作......并且在将来,我可能需要线程之间的自定义同步=>多线程模式对我来说是最好的
我编写了这样的服务:
public interface IMyInterface
{
...
}
[Export(typeof(IMyInterface))]
internal class MyService : IMyInterface
{
...
}
Run Code Online (Sandbox Code Playgroud)
现在,我想MyService在我的主程序中导入几个MEF 实例.
我怎样才能做到这一点 ?
随着[Import] private IMyInterface MyService { get; set; }我只得到1个实例MyService.在我的主程序中,我想动态指定MyServiceMEF组合之前导入的实例数.
我不想使用,[ImportMany]因为我不想在我的MyService实现中指定导出数.
你能帮助我吗 ?
解决方案1:
foreach (var item in itemList)
{
myContext.ExecuteStoreCommand("EXEC MyProc {0};", item); // Insertion
}
Run Code Online (Sandbox Code Playgroud)
要么
解决方案2:
StringBuilder sb = new StringBuilder();
foreach (var item in itemList)
{
sb.AppendLine(String.Format("EXEC MyProc {0};", item)); // Insertion
}
myContext.ExecuteStoreCommand(sb.ToString());
Run Code Online (Sandbox Code Playgroud) 在Parallel.For中,是否可以使用'WaitAll'同步每个线程?
Parallel.For(0, maxIter, i =>
{
// Do stuffs
// Synchronisation : wait for all threads => ???
// Do another stuffs
});
Run Code Online (Sandbox Code Playgroud) 看看这个代码:
public class Program
{
[import]IMain Main {get; set;}
...
private Compose() {...}
}
internal interface IMain
{
...
}
[Export(typeof(IMain)]
public class Main : IMain
{
[import]
Interace1 Object1 {get;set;}
[import]
Interace2 Object2 {get;set;}
}
...
Run Code Online (Sandbox Code Playgroud)
我想在 Program 和 Main 之间的组合之后延迟加载 Object2。当我在 Program 中编写时,出现 MEF 错误,因为 MEF 也尝试编写 Object2(但 Interface2 的实现在开始时不可用......我想在之后加载它)。
我怎样才能做到这一点 ?
我试图把:
[import]
Lazy<Interace2> Object2 {get;set;}
Run Code Online (Sandbox Code Playgroud)
但我仍然有同样的麻烦。
什么是.NET中最好的免费库:
我不想使用EF(非常糟糕的性能)
谢谢
c# ×7
sql ×3
sql-server ×3
.net ×2
import ×2
mef ×2
.net-4.0 ×1
c++ ×1
foreign-keys ×1
identity ×1
interface ×1
lazy-loading ×1
linker ×1
locking ×1
parallel.for ×1
windows ×1