简体   繁体   English

首先使用Entity Framework代码时无法连接到数据库

[英]Cannot connect to database while using Entity Framework Code First


I wrote very simple class, that perfom data access. 我写了一个非常简单的类,即perfom数据访问。
It checks if line with that day exist in table and update her or create a new line. 它检查表中是否存在与该日对应的行,并对其进行更新或创建新行。

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();
                }
            }
        }

It use EF for generate database. 它使用EF生成数据库。 The Contex class and class for save look like this: 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; }
}

I call this methods by this code: 我通过以下代码调用此方法:

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


It should generate new database, but error occurs. 它应该生成新的数据库,但是会发生错误。 In message it write that can`t connect to the SQL Server. 在消息中写入无法连接到SQL Server的消息。

Is somebody know what is wrong? 有人知道怎么了吗?
PS Sorry for my bad English. PS对不起,我的英语不好。
PPS I added EF by NuGet. PPS我通过NuGet添加了EF。

You can manually specify the connection string as follows 您可以手动指定连接字符串,如下所示

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

The default constructor looks up a connection string in the web.config with the same name as the derived context class Context . 默认的构造函数在web.config中查找与派生的上下文类Context同名的连接字符串。

If it fails to find one it defaults to 如果找不到,则默认为

Data Source=.\SQLEXPRESS;

or 要么

Data Source=(LocalDb)\v11.0;

depending on the version of sql server you are using. 取决于您使用的sql server的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用实体框架代码优先使用凭据创建数据库? - Cannot Create Database with credentials using Entity Framework Code-First? 在实体框架中使用数据库优先实体和代码优先实体 - Using database first entity with code first entity in entity framework 代码优先实体框架无法连接到数据库 - code first entity framework does can't connect to database 如何使用 MVC 4 通过 Entity Framework 6 Code First 连接到多个数据库 - How to connect to more than one database with Entity Framework 6 Code First using MVC 4 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? 使用 Entity Framework Code First 连接到带有 devart dotconnect 的旧版 Oracle 数据库 - Using Entity Framework Code First to connect to Legacy Oracle Database with devart dotconnect 在代码优先实体框架中连接数据库时出错 - Error while connecting to database in code-first Entity Framework 无法使用 Blazor 和实体框架连接到 SQL 数据库 - Cannot connect to SQL database using Blazor and Entity Framework 在实体框架中首先创建数据库和代码 - Creating database first and code first in Entity Framework 使用实体框架(从数据库中获取代码优先)创建其他类 - Create additional class using Entity Framework (Code First From Database)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM