网站存储单个数据类型的位置

|| 我正在使用asp.net mvc。如何或在何处存储单个数据?例如。
SubscriptionFee
IsSiteOffline
。 我在这里问了一个有关用户设置的问题。我应该对网站设置做这样的事情,还是数据库之外还有别的方法?我希望我的用户可以从网站本身更改这些设置。 我将首先使用EntityFramework代码,如果我可以做类似的事情,我会很高兴::2ѭ。
已邀请:
通常,您会将这些设置放入Web.config文件的“ 3”部分。 一个标准的ASP .NET MVC 3应用程序已经带有一些设置(在
<configuration>
元素内部):
  <appSettings>
    <add key=\"ClientValidationEnabled\" value=\"true\" />
    <add key=\"UnobtrusiveJavaScriptEnabled\" value=\"true\" />
    <add key=\"MyCustomSetting\" value=\"abcd1234\" />
  </appSettings>
要在您的应用程序中引用它们,请使用ConfigurationManager类:
using System.Configuration;
string value = ConfigurationManager.AppSettings[\"MyCustomSetting\"];
如前所述,最好在数据后端(即SQL Server或您使用的任何数据库)中创建一个配置表,然后从那里获取它们。 在我的一个(非MVC)应用程序中,我创建了一个静态SysProperties类,该类将使用应用程序的缓存使它们至少缓存5分钟。此示例未使用实体框架,但可以很容易地对其进行修改:
public static class SysProperties
{
    public static string SMTPServer
    {
        get
        {
            return GetProperty(\"SMTPServer\").ToString();
        }
    }

    public static decimal TicketFee
    {
        get
        {
            return Convert.ToDecimal(GetProperty(\"TicketFee\"));
        }
    }

    private static object GetProperty(string PropertyName)
    {
        object PropertyValue = null;
        if (HttpContext.Current != null)
            PropertyValue = HttpContext.Current.Cache[PropertyName];

        if (PropertyValue == null)
        {
            SqlCommand cmSQL = new SqlCommand(\"SELECT Value FROM tblProperty WHERE Name = @PropertyName\");
            cmSQL.Parameters.AddWithValue(\"@PropertyName\", PropertyName);

            DataTable dt = Functions.RunSqlCommand(cmSQL);

            PropertyValue = dt.Rows[0][0];

            if (HttpContext.Current != null)
                HttpContext.Current.Cache.Insert(PropertyName, PropertyValue, null, DateTime.UtcNow.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);

            return PropertyValue;
        }
        else
        {
            return PropertyValue;
        }
    }
}
注意:您可以将相同的技术与ConfigurationManager一起使用,以从Web.config文件而不是从数据库中检索这些值。 只是另一个免费的东西,这是我用来利用SQL Server的SqlCacheDependency的一些代码。您必须启用SQL Server Broker,但这允许您将值保留在内存中,直到SQL Server中的值更改为止。这样,您就不会有任意的5分钟到期时间。 此功能旨在检索包含两部分标识符的内容(例如用户的全名),因此它需要三个参数: -如果未缓存值,则运行SQL查询 -您要检索的商品的唯一ID(即用户ID) -标识数据类型的任意字符串(即\“ UserFullName \”,\“ UserEmail \”) 您可以很容易地对此进行修改,以检索包含一部分标识符的内容:
// Static constructor ensures that SqlDependency.Start is called before
// we try to use any SqlCacheDependencies   
static Functions()
{
    SqlDependency.Start(ConnectionString);
}

public static string GetUserFullName(string UserName)
{
    return GetSqlCachedValue(\"SELECT FirstName + \' \' + LastName FROM dbo.tblUser WHERE UserName = @Id\", UserName, \"UserFullName\").ToString();
}

public static string GetEventNameFromId(int Id)
{
    return GetSqlCachedValue(\"SELECT EventName FROM dbo.tblEvents WHERE EventID = @Id\", Id, \"EventName\").ToString();
}

private static object GetSqlCachedValue(string Query, object Id, string CacheName)
{
    // Get the cache
    System.Web.Caching.Cache currentCache = HttpContext.Current.Cache;

    object Value = null;

    // We use a standard naming convention for storing items in the application\'s cache
    string cacheKey = string.Format(\"{0}_{1}\", CacheName, Id.ToString());

    // Attempt to retrieve the value
    if (currentCache != null && currentCache[cacheKey] != null)
        Value = currentCache[cacheKey];

    // If the value was not stored in cache, then query the database to get the value
    if (Value == null)
    {
        // Run the query provided to retrieve the value. We always expect the query to have the @Id parameter specified, that we can use
        // to plug-in the Id parameter given to this function.
        SqlCommand Command = new SqlCommand(Query);
        Command.Parameters.AddWithValue(\"@Id\", Id);

        // Generate a cache dependency for this query
        System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency(Command);

        // Run the query
        DataTable dt = RunSqlCommand(Command);

        if (dt.Rows.Count == 1)
        {
            // Grab the value returned
            Value = dt.Rows[0][0];

            // Save the value in the cache, so next time we don\'t have to query SQL Server
            if (currentCache != null)
            {
                currentCache.Insert(cacheKey, Value, dependency);
            }
        }
    }

    // return the final value
    return Value;
}
最好,尤其是由于您希望允许用户更改这些设置,请将它们存储在您具有后端的任何数据存储中,例如SQL或其他。 要使用这些设置,您可以将它们带入应用程序缓存并创建对数据存储的依赖关系,以便任何更新都会使您的缓存过期。您甚至可以为此使用静态类,但是您必须自己实施管理。

要回复问题请先登录注册