使用mvc4 razor c#连接mysql数据库
本文关键字:mysql 数据库 连接 mvc4 razor 使用 | 更新日期: 2023-09-27 18:11:25
嗨,伙计,我已经看到这个错误代码:执行命令定义时发生错误。有关详细信息,请参阅内部异常。在搜索我的文本到数据库..
private bool IsValidRegisterNewAcct(string acctname, string dispname, string email, string password)
{
var crypto = new SimpleCrypto.PBKDF2();
bool IsValid = false;
IsValidErrorMessage = "";
IsValidErrorMessageInt = 0;
string Newacctname = crypto.Compute(acctname.ToString());
//try
//{
using (var db = new MainDbContext())
{
//db.Connection.Open();
var user = db.user.FirstOrDefault(u => u.AcctNameSalt == Newacctname);
//var user = db.osoa_user.FirstOrDefault(u => u.Email == email);
if (user != null)
{
IsValid = false;
//db.Connection.Close();
IsValidErrorMessage = "Account Name already Exist!";
}
else
{
var InsertNewAcctName = db.osoa_user.CreateObject();
InsertNewAcctName.UserId = Convert.ToString( Guid.NewGuid());
InsertNewAcctName.AcctName = acctname;
InsertNewAcctName.AcctNameSalt = crypto.Compute(acctname);
InsertNewAcctName.DisplayName = dispname;
InsertNewAcctName.Email = email;
InsertNewAcctName.EmailSalt = crypto.Compute(email);
InsertNewAcctName.Password = crypto.Compute(password);
InsertNewAcctName.PasswordSalt = crypto.Salt;
InsertNewAcctName.UserLevel = 2;
db.user.AddObject(InsertNewAcctName);
db.SaveChanges();
//db.Connection.Close();
IsValid = true;
}
}
/*
}
catch (Exception e)
{
IsValidErrorMessageInt = 1;
IsValidErrorMessage = e.Message + ". Pls. contact your Administrator.";
IsValid = false;
}
*/
return IsValid;
}
,错误指向以下代码:
var user = db.user.FirstOrDefault(u => u.AcctNameSalt == Newacctname);
UPDATE关于Inner异常。我不知道给你的是不是正确的…我尝试使用这个代码:
try
{
}
catch (Exception e)
{
IsValidErrorMessageInt = 1;
IsValidErrorMessage = e.InnerException + ". Pls. contact your Administrator.";
IsValid = false;
}
MySql.Data.MySqlClient。MySqlException (0x80004005):未知列Extent1。MySql.Data.MySqlClient.MySqlStream.ReadPacket()在MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&affectedRow Int64&MySql.Data.MySqlClient.Driver. insertedId)gettresult (Int32语句id, Int32&affectedRows Int64&MySql.Data.MySqlClient.Driver. insertedId)在MySql.Data.MySqlClient.MySqlDataReader.NextResult()在MySql.Data.MySqlClient.MySqlCommand. NextResult()MySql.Data.Entity.EFMySqlCommand. ExecuteReader(CommandBehavior行为)。ExecuteDbDataReader(CommandBehavior行为)在System.Data.Common.DbCommand。ExecuteReader(CommandBehavior行为)在System.Data.EntityClient.EntityCommandDefinition。ExecuteStoreCommands(EntityCommand EntityCommand, CommandBehavior行为)
从你的内部异常看来,好像你的MainDbContext
已经与你的数据库不同步了(看起来Extent1
有一个不存在于DB中的列)。
尝试从模型中删除该属性,然后再试一次。