没有任何内容写入数据库

Nothing is getting written to database

我是 ASP.Net 的初学者。我正在使用 Entity Framework 数据库优先方法。我需要将数据存储到我的 MUSIC 数据库中,在名为 LogTable 的 table 中。我尝试了很多方法。什么都不管用。最后我试了这个:

在Login.aspx

using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using MusicShop.Models;

namespace MusicShop.Pages.Accounts
{
public partial class Login : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnLogin_OnClick(object sender, EventArgs e)
    {
        var userStore = new UserStore<IdentityUser>();

        userStore.Context.Database.Connection.ConnectionString =
            ConfigurationManager.ConnectionStrings["MUSICConnectionString"].ConnectionString;

        var manager = new UserManager<IdentityUser>(userStore);

        var user = manager.Find(txtUserName.Text, txtPassword.Text);


        if (user != null)
        {
            InsertUserLog();
            var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            var userIdentity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

            authenticationManager.SignIn(new AuthenticationProperties
            {
                IsPersistent = false
            }, userIdentity);


            Response.Redirect("~/Index.aspx");
        }
        else
        {
            litStatus.Text = "Invalid username or password";
        }
    }

    private void InsertUserLog()
    {
            LogModel model = new LogModel(); //Add a new model
            var logtable = new LogTable
            {
                IP = Request.ServerVariables["REMOTE_ADDR"].ToString(),
                //You could set a break point to check the value.
                AGENT = Request.ServerVariables["http_user_agent"].ToString(),
                REQM = Request.ServerVariables["request_method"].ToString(),
                SERVER = Request.ServerVariables["server_name"].ToString(),
                PORT = Request.ServerVariables["server_port"].ToString(),
                SW = Request.ServerVariables["server_software"].ToString(),
                DNS = Request.ServerVariables["REMOTE_HOST"].ToString()
            };

            //set value
            var error = model.InsertLog(logtable);

            if (error != null) Response.Write(error);

    }
}
}

在LogModel.cs

public string InsertLog(LogTable logTable)
{
    try
    {
        MUSICEntities db = new MUSICEntities();
        db.LogTables.Add(logTable);
        db.SaveChanges();
        return null;
    }
    catch (Exception e)
    {
        return "Error:" + e;
    }
}

没有错误。但是没有任何内容添加到数据库中。谁能指导我?如果这是一个愚蠢的错误,请原谅我。

您可能没有看到错误,因为您没有检查它。

尝试输出您返回的错误。

var error = model.InsertLog(logtable);

if(error != null) Response.Write(error);

我还注意到您只是简单地连接了 Exception 实例...

你可能应该说 "Error:" + e.Message"Error:" + e.StackTrace

由于您似乎依赖异常 class 的 ToString() 方法。.不确定您将从中获得多少信息。