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 回答