[英]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无法知道数据库在哪里。
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.