使用Entity Framework Code First时无法连接到数据库

时间:2013-05-10 15:46:30

标签: c# .net entity-framework


我写了非常简单的类,即perfom数据访问。
它检查表中是否存在当天的行,并更新她或创建一个新行。

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。

有人知道出了什么问题吗?
附:对不起,我的英语不好。
P.P.S.我添加了Nu by NuGet。

1 个答案:

答案 0 :(得分:0)

您可以手动指定连接字符串,如下所示

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

默认构造函数在web.config中查找与派生上下文类Context同名的连接字符串。

如果找不到,则默认为

Data Source=.\SQLEXPRESS;

Data Source=(LocalDb)\v11.0;

取决于您使用的sql server的版本。