Chr*_*ark 1 c# mysql wpf entity-framework
我有一个在Intranet上发布的应用程序.当我调试我的应用程序时,它运行顺利.但是当我发布我的应用程序和我的visual studio作为调试器时,从实体中检索变为null.
对象引用未设置为对象的实例.
从这一行(使用发布版本的app调试器):
user_mstr vwUser = ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
Run Code Online (Sandbox Code Playgroud)
我正在使用MySQL数据库与实体框架.请注意,我正在使用相同的PC但不同的版本,已发布而不是.
更新:
这null仅在发布时返回,而不是在调试器上运行时返回.调试器上一切正常.
这是连接字符串:
connectionString="metadata=res://*/DataAccess.entityName.csdl|res://*/DataAccess.entityName.ssdl|res://*/DataAccess.entityName.msl;provider=MySql.Data.MySqlClient;provider connection string="server=servername;user id=username;password=password;persistsecurityinfo=True;Convert Zero Datetime=True;database=default_db""
Run Code Online (Sandbox Code Playgroud)
我认为值得一提的是我正在研究WPF.
这是一个小提琴,它演示了从异常消息中获取更多信息的快速而肮脏的方法.
创建以下方法.它递归地收集来自其Exception所有InnerException孩子的相关信息.
public string ExceptionDetails(StringBuilder b, Exception e)
{
if (e != null)
{
b.AppendLine("\n\n-----");
b.AppendLine("Data:".PadRight(20) + e.Data);
b.AppendLine("Message:".PadRight(20) + e.Message);
b.AppendLine("StackTrace:".PadRight(17) + e.StackTrace);
b.AppendLine("TargetSite:".PadRight(20) + e.TargetSite.ToString());
b.AppendLine("-----");
ExceptionDetails(b, e.InnerException);
}
return b.ToString();
}
Run Code Online (Sandbox Code Playgroud)
接下来,将代码包装在以下try-catch块中.
try
{
user_mstr vwUser =
ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
}
catch (Exception e)
{
var builder = new StringBuilder();
var details = ExceptionDetails(builder, e);
throw new Exception(details);
}
Run Code Online (Sandbox Code Playgroud)
这将为您提供更多通用NullReferenceException消息.有了更多信息,您可能不再需要任何步骤.
您已经知道NullReferenceException的含义.您正在尝试访问值为null的类型的成员.要进行故障排除,您需要确定哪个值为null,然后您需要确定它为null的原因.
这是一个小提琴,缩小了什么是空的.从那个小提琴,你可以看到它是ctxnull或者第一个项List<user_mstr>是null(只是假装a List是a DbSet).
这是小提琴的代码.
using System;
using System.Collections.Generic;
using System.Linq;
public class user_mstr
{
public string user_cd;
}
public class FakeContext
{
public List<user_mstr> user_mstr;
}
public class Program
{
private static string strUserCD = "foo";
private static void FirstUserAsNull()
{
try
{
Console.WriteLine("FirstUserAsNull");
FakeContext ctx = new FakeContext();
ctx.user_mstr = new List<user_mstr>() { null, new user_mstr(), new user_mstr() };
user_mstr vwUser = ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
}
catch (NullReferenceException e)
{
Console.WriteLine(e.Message);
}
catch (ArgumentNullException e)
{
Console.WriteLine(e.Message);
Console.WriteLine();
}
}
private static void UserListAsNull()
{
try
{
Console.WriteLine("UserListAsNull");
FakeContext ctx = new FakeContext();
ctx.user_mstr = null;
user_mstr vwUser = ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
}
catch (NullReferenceException e)
{
Console.WriteLine(e.Message);
}
catch (ArgumentNullException e)
{
Console.WriteLine(e.Message);
Console.WriteLine();
}
}
private static void CtxAsNull()
{
try
{
Console.WriteLine("CtxAsNull");
FakeContext ctx = null;
user_mstr vwUser = ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
}
catch (NullReferenceException e)
{
Console.WriteLine(e.Message);
Console.WriteLine();
}
}
public static void Main()
{
CtxAsNull();
UserListAsNull();
FirstUserAsNull();
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出.
CtxAsNull
Object reference not set to an instance of an object.
UserListAsNull
Value cannot be null.
Parameter name: source
FirstUserAsNull
Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
从第一步开始,我们知道该中的ctx第一项或者是第一项List<user_mstr>是null(其中List就像a DbSet).现在我们需要进一步缩小范围.一种方法是将代码更改为:
user_mstr vwUser = null;
if(ctx != null)
{
vwUser = ctx.user_mstr.FirstOrDefault(x => x.user_cd == strUserCD);
}
Run Code Online (Sandbox Code Playgroud)
如果您仍然收到NullReferenceException,那么您知道有问题的空值是该中的第一项List<user_mstr>.如果您没有收到该异常,那么您就知道问题是null ctx.
ctx为null.尝试对连接字符串进行硬编码.这是一种方法.
var providerName = "MySql.Data.MySqlClient";
var builder = new StringBuilder();
builder.Append("server=servername;");
builder.Append("user id=username;");
builder.Append("password=password;");
builder.Append("persistsecurityinfo=True;");
builder.Append("Convert Zero Datetime=True;");
builder.Append("database=default_db");
var providerString = builder.ToString();
var entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = providerName;
entityBuilder.ProviderConnectionString = providerString;
entityBuilder.Metadata = @"res://*/DataAccess.entityName.csdl|
res://*/DataAccess.entityName.ssdl|
res://*/DataAccess.entityName.msl";
using (var conn = new EntityConnection(entityBuilder.ToString()))
{
conn.Open();
// do something
conn.Close();
}
Run Code Online (Sandbox Code Playgroud)
您使用的是什么版本的Entity Framework?dev.mysql.com站点有关于MySQL Connector for .NET开发的单独章节:第9章EF 5支持和第10章EF 6支持.
您使用的是什么版本的.NET?检查您是否在调试和发布时使用相同的.NET版本.你至少需要4.0版本.
您的发布/ bin是否包含MySql.Data.Entity.dll?如果没有,则将其复制粘贴到那里(如有必要,还可以复制MySql.Web.dll.)实体框架中的MySql存在这些问题.
发布和调试是使用相同的数据库构建的吗?
你在使用app.config转换吗?这可能不是这种情况,因为你正在使用WPF和app.config转换不像web.config转换那样开箱即用.
您使用的是web.config转换吗?如果您要作为WPF浏览器应用程序发布,那么web.config文件将是合适的,并且web.config转换可以更改连接字符串.
你有没有重建(清理然后建立)你的发布?你可能想更进一步,手动删除/bin和/obj你的下一个构建之前.
正如其他人所提到的,你真的需要更多的信息.
InnerException很好.实体框架代码First + MySQL ... NullReferenceException
| 归档时间: |
|
| 查看次数: |
1135 次 |
| 最近记录: |