0


データアクセスを実行する非常に単純なクラスを作成しました。
その日の行がテーブルに存在するかどうかを確認し、それを更新するか、新しい行を作成します。

public class DataAccessClass
{
    public static DayWeather GetDayWeather(DateTime date)
    {
        try
        {
            using (var db = new Context())
            {
                var query =
                    (from day in db.DayWeather
                     where ((DateTime)day.DateOfDay).Date == date.Date
                     select new DayWeather((short)day.Temperature, (ushort)day.WindSpeed, (ushort)day.Pressure, (ushort)day.Humidity, day.Cloudiness, day.TypeRecip, (DateTime)day.DateOfDay)).First();
                return query;
            }
        }
        catch (Exception exp)
        {
            if (!EventLog.SourceExists("DataAccessSource"))
            {
                EventLog.CreateEventSource("DataAccessSource", "DataAccessErrorLog");
            }
            EventLog.WriteEntry("DataAccessSource", exp.Message);
            throw new Exception("Problem with data get.");
        }
    }

    public static void SaveDayWeather(DayWeather day)
    {
        try
        {
            using (var db = new Context())
            {
                var existingDay =
                  (from d in db.DayWeather
                   where ((DateTime)day.DateOfDay).Date == day.DateOfDay.Date
                   select d).SingleOrDefault<DayWeather>();
                if (existingDay != null)
                {
                    existingDay.Temperature = day.Temperature;
                    existingDay.WindSpeed = day.WindSpeed;
                    existingDay.Pressure = day.Pressure;
                    existingDay.Humidity = day.Humidity;
                    existingDay.Cloudiness = day.Cloudiness;
                    existingDay.TypeRecip = day.TypeRecip;
                    db.SaveChanges();
                }
                else
                {
                    DayWeather newDay = new DayWeather();
                    newDay.DateOfDay = day.DateOfDay;
                    newDay.Temperature = day.Temperature;
                    newDay.WindSpeed = day.WindSpeed;
                    newDay.Pressure = day.Pressure;
                    newDay.Humidity = day.Humidity;
                    newDay.Cloudiness = day.Cloudiness;
                    newDay.TypeRecip = day.TypeRecip;
                    db.DayWeather.Add(newDay);
                    db.SaveChanges();
                }
            }
        }

データベースの生成にはEFを使用します。Contex クラスと保存用のクラスは次のようになります。

public class DayWeather
{
    public short Temperature { get; set; }

    public ushort WindSpeed { get; set; }

    public ushort Pressure { get; set; }

    public ushort Humidity { get; set; }

    public string Cloudiness { get; set; }

    public string TypeRecip { get; set; }

    public DateTime DateOfDay { get; set; }

    public DayWeather(short Temperature, ushort WindSpeed, ushort Pressure, ushort Humidity, string Cloudiness, string TypeRecip, DateTime Date)
    {
        this.Temperature = Temperature;
        this.WindSpeed = WindSpeed;
        this.Pressure = Pressure;
        this.Humidity = Humidity;
        this.Cloudiness = Cloudiness;
        this.TypeRecip = TypeRecip;
        this.DateOfDay = Date;
    }

    public DayWeather()
    {

    }
}

internal class Context : DbContext
{
    public DbSet<DayWeather> DayWeather { get; set; }
}

このメソッドを次のコードで呼び出します。

DataAccessClass.SaveDayWeather(new DayWeather(12, 12, 12, 12, "Yes", "rain", DateTime.Now));
        DayWeather day = DataAccessClass.GetDayWeather(DateTime.Now);
        Console.WriteLine(day.ToString());
        Console.ReadKey();


新しいデータベースを生成する必要がありますが、エラーが発生します。メッセージにSQL Serverに接続できないと書いてあります。

誰かが何が悪いのか知っていますか?
PS 下手な英語で申し訳ありません。
PPS NuGet で EF を追加しました。

4

1 に答える 1

0

次のように接続文字列を手動で指定できます

using (var db = new Context("connectionString"))

既定のコンストラクターは、web.config で、派生したコンテキスト クラスと同じ名前の接続文字列を検索しますContext

見つからない場合は、デフォルトで

Data Source=.\SQLEXPRESS;

また

Data Source=(LocalDb)\v11.0;

使用している SQL Server のバージョンによって異なります。

于 2013-05-10T15:57:38.883 に答える