是否可以将Microsoft Entity Framework与Oracle数据库一起使用?
在EF6中使用此代码:
public string GetConnectionString(DbContext ctx)
{
ObjectContext _objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
if (_objectContext?.Connection != null)
{
EntityConnection entityConnection = _objectContext.Connection as EntityConnection;
return entityConnection?.StoreConnection?.ConnectionString;
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
如何在EF Core 2.0中执行此操作?
如何myStruct.myField使用DynamicMethod进行反射,将值设置为struct字段?当我调用setter(myStruct, 111)value时没有设置,因为MyStruct是值类型.Console.WriteLine(myStruct.myField)显示值3.
如何修改GetDelegate方法将值设置为myStruct.myField?
public struct MyStruct
{
public int myField;
}
public delegate void SetHandler(object source, object value);
private static SetHandler GetDelegate(Type type, FieldInfo fieldInfo)
{
DynamicMethod dm = new DynamicMethod("setter", typeof(void), new Type[] { typeof(object), typeof(object) }, type, true);
ILGenerator setGenerator = dm.GetILGenerator();
setGenerator.Emit(OpCodes.Ldarg_0);
setGenerator.DeclareLocal(type);
setGenerator.Emit(OpCodes.Unbox_Any, type);
setGenerator.Emit(OpCodes.Stloc_0);
setGenerator.Emit(OpCodes.Ldloca_S, 0);
setGenerator.Emit(OpCodes.Ldarg_1);
setGenerator.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType);
setGenerator.Emit(OpCodes.Stfld, fieldInfo);
setGenerator.Emit(OpCodes.Ldloc, 0);
setGenerator.Emit(OpCodes.Box, type);
setGenerator.Emit(OpCodes.Ret);
return (SetHandler)dm.CreateDelegate(typeof(SetHandler));
}
MyStruct myStruct = new …Run Code Online (Sandbox Code Playgroud) 默认情况下,Web API 2中的JSON和XML序列化程序只是调用ToString()序列化Guid,这意味着它包含破折号:fd190000-5532-8477-e053-9a16ce0af828.我真的想将默认序列化更改为返回的格式ToString("N"),其中不包括短划线:fd19000055328477e0539a16ce0af828.
我发现这篇文章是关于创建一个JsonConverter并重写WriteJson()要使用的方法ToString("N").这适用于JSON,但我没有找到任何类似的XML序列化.
有没有办法只实现一次,无论有多少MediaTypeFormatters?如果没有,我如何覆盖XML序列化?
我有一些代码将字符串保存到文件,如下所示:
string TheFileJS = HttpRuntime.AppDomainAppPath + "\\SomePath\\" + ClientFileName + ".js";
if (File.Exists(TheFileJS) == true)
{
File.Delete(TheFileJS);
}
File.WriteAllText(TheFileJS, TheJS);
Run Code Online (Sandbox Code Playgroud)
我正在使用,File.WriteAllText因为我认为它可以防止文件锁定问题但发生的事情是有时我得到错误File is being used by another process.问题是它很少发生,但是一旦在特定文件上发生此异常,所有客户端调用此文件就会产生404.
我需要在代码中更改哪些内容以确保此错误永远不会发生?
谢谢.
我的简单示例是:
void FixedUnalterableMethod()
{
try
{
throw new Exception("Exception 1"); //line 12.
}
finally
{
throw new Exception("Exception 2"); //line 16.
}
}
void Method1()
{
try
{
FixedUnalterableMethod(); //line 24.
}
catch (Exception ex)
{
var messageWithStackTrace = ex.ToString(); //line 28.
Console.WriteLine(messageWithStackTrace);
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出为:
System.Exception: Exception 2
at Program.FixedUnalterableMethod() in ...\Program.cs:line 16
at Program.Main(String[] args) in ...\Program.cs:line 24
Run Code Online (Sandbox Code Playgroud)
问题是,如何得知Exception 1已发生?有没有一种方法可以Exception 1在我的StackTrace中添加它(第28行)?
当然,我无法修改该FixedUnalterableMethod()方法!
如何从当前位置构造堆栈跟踪?
在.NET 4.5中使用此解决方案:
System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true);
for (int i = 0; i < stackTrace.FrameCount; ++i)
{
System.Diagnostics.StackFrame frame = stackTrace.GetFrame(i);
MethodBase callerMethod = frame.GetMethod();
...
}
Run Code Online (Sandbox Code Playgroud)
但是.NET Core中的解决方案是什么?构造public StackTrace(bool fNeedFileInfo)函数不再存在.