简体   繁体   English

实体框架app.config配置不会创建数据库

[英]Entity framework app.config configuration does not create a database

I'm trying to use entity framework with code first method, but for some reason it does not create a database for me. 我正在尝试将实体框架与代码优先方法结合使用,但是由于某种原因,它不会为我创建数据库。 I suppose the problem is in my app.config configuration file. 我想问题出在我的app.config配置文件中。 I never used entity framework before. 我以前从未使用过实体框架。 I have read similar posts on stackoverflow, but however I cannot solve my problem. 我已经阅读过关于stackoverflow的类似文章,但是我无法解决我的问题。 I do not get any errors during compilation, but when I open microsoft sql server I don't see my database created. 编译期间没有出现任何错误,但是当我打开microsoft sql server时,看不到创建的数据库。

This is my app.config file. 这是我的app.config文件。 I found this code online, but I don't know what should I write inside Database attribute. 我在网上找到了此代码,但是我不知道应该在Database属性中写什么。

<configuration>
  <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="ipadress" value="127.0.0.1" />
    <add key="port" value="14000" />
  </appSettings>

  <connectionStrings>
    <add name="AVLdataContext" connectionString="Server=.\SQLEXPRESS;Database=Blogging;Integrated Security=True;"  providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Also here is my model class: 这也是我的模型课:

namespace TCPserver
{
    public class DataBaseModel
    {
        public static string LoadToDatabase(byte[] arr, string imei)
        {
            return ReversedBinaryReader.LoadToDatabase(arr, imei);
        }
    }

    public class AVLdataContext : DbContext
    {
        public DbSet<Datas> Data { get; set; }
        public DbSet<GPSelement> GPSelement { get; set; }
        public DbSet<IOelement> IOelement { get; set; }
    }
}

Here is my controller: 这是我的控制器:

using (var db = new AVLdataContext())
{
    db.Data.Add(data);
    db.GPSelement.Add(data.Gps);
    db.IOelement.Add(data.Io);
    db.SaveChanges();
}

What am I doing wrong? 我究竟做错了什么? I'm completely green at this. 我完全是绿色的。

You need to update the AVLdataContext to include a constructor calls base() with the connection string 您需要更新AVLdataContext以包括带有连接字符串的构造函数调用base()

public class AVLdataContext : DbContext
{
    public AVLdataContext ()
        : base("AVLdataContext")
    {

    }

    public DbSet<Datas> Data { get; set; }
    public DbSet<GPSelement> GPSelement { get; set; }
    public DbSet<IOelement> IOelement { get; set; }
}

Without this, EF has no way of knowing where the database is. 没有这个,EF无法知道数据库在哪里。

Have a look at this as well. 也看看这个

Try to open your package manager Console and write folowing commands: 尝试打开软件包管理器控制台并编写以下命令:

Enable-Migrations
Add-Migration initial
Update-Database

This will enable migrations for your db, add first migration and create db. 这将为您的数据库启用迁移,添加第一个迁移并创建数据库。

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

相关问题 在 app.config 之外的实体框架中为 PostgreSQL 数据库设置 connectionString - Set a connectionString for a PostgreSQL database in Entity Framework outside the app.config 未创建实体框架 6 App.config 文件 - Entity Framework 6 App.config file is not created App.Config中的实体框架连接字符串 - Entity Framework Connection String in App.Config app.config和用于远程连接的实体框架 - app.config and Entity Framework for remote connection 没有App.config的SQLite实体框架 - SQLite Entity Framework without App.config 实体框架(代码优先到现有数据库)-在运行时覆盖app.config中的连接字符串 - Entity Framework(Code First to an existing database) - overwrite a connection string in app.config at runtime 不带App.config的实体框架,数据库优先,存储库模式,N层解决方案体系结构 - Entity Framework without App.config, Database first, Repository Pattern, N-tier Solution Architecture App.Config PostgreSQL实体框架6的Npgsql C#问题 - Npgsql C# problem with App.Config PostgreSQL Entity Framework 6 实体框架未在app.config中使用连接字符串 - Entity Framework not using connection string in app.config WCF:使用实体框架-App.config - WCF: Work with Entity Framework- App.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM