什么都没有写入数据库

时间:2016-01-17 07:43:56

标签: c# asp.net entity-framework ef-database-first

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

在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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

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

尝试输出您要返回的错误。

var error = model.InsertLog(logtable);

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

我还注意到你只是连接了Exception实例......

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

因为您似乎依赖于Exception类的ToString()方法..不确定您将从中获取多少信息。