简体   繁体   English

首先使用Entity Framework代码创建数据库

[英]Create database with Entity Framework code first

I've stumbled upon a problem where I want to create a database using code first with Entity Framework. 我偶然发现了一个问题,我想首先使用Entity Framework创建一个数据库代码。 In recent projects I have not used EF to connect to my database successfully but now something is wrong. 在最近的项目中,我没有使用EF成功连接到我的数据库,但现在出了问题。

I have created my app.config file with following content: 我创建了我的app.config文件,其中包含以下内容:

 <connectionStrings>
<add name="HomeCinema" 
     connectionString="Data Source= {MY SERVER NAME IN MSSQL};Initial
     Catalog=HomeCinema;Integrated Security=SSPI; MultipleActiveResultSets=true" 
     providerName="System.Data.SqlClient" />

And I assume Data Source shall contain what I log on with on my SQL Server. 我假设数据源应包含我在SQL Server上登录的内容。

When I try to create the database with my classes in the project, I use update-database -verbose to create it in the Package Manager Console and I target the correct project. 当我尝试使用项目中的类创建数据库时,我使用update-database -verbose在Package Manager控制台中创建它,并且我将目标指向正确的项目。

And here it says when I use the update-database -verbose: Target database is: 'HomeCinema' (DataSource: .\\SQLEXPRESS, Provider: System.Data.SqlClient, Origin: Convention) 在这里它说当我使用update-database -verbose:目标数据库是:'HomeCinema'(DataSource:。\\ SQLEXPRESS,Provider:System.Data.SqlClient,Origin:Convention)

The DataSource here is different than what I have specified in the App.config file. 这里的DataSource与我在App.config文件中指定的不同。 I assume there is something wrong there but just can't figure it out. 我认为那里有些不对劲但是无法弄明白。

I have not done this way before and its uncharted territory to me that I wish to fix, but can't seem to grasp why it happens or how to fix it. 我以前没有这样做过,而且我想要修复它的未知领域,但似乎无法理解它为什么会发生或如何解决它。

Hopefully I have described the matter clearly but please ask if there is something missing. 希望我已经清楚地描述了这个问题,但请问是否有遗漏的东西。

Would highly appreciate it if I could recieve some guidelines or some tips to how to solve this problem or if there is another way to go about to create the database. 如果我能收到一些指导或一些提示来解决这个问题,或者是否有其他方法可以创建数据库,我将非常感激。

My DbContext class: 我的DbContext类:

public class HomeCinemaContext : DbContext
{
    public HomeCinemaContext() 
        : base("HomeCinema")
    {
        Database.SetInitializer<HomeCinemaContext>(null);
    }
    #region Entity Sets 
    public IDbSet<User> UserSet { get; set; }
    public IDbSet<Role> RoleSet { get; set; }
    public IDbSet<UserRole> UserRoleSet { get; set; }
    public IDbSet<Customer> CustomerSet { get; set; }
    public IDbSet<Movie> MovieSet { get; set; }
    public IDbSet<Genre> GenreSet { get; set; }
    public IDbSet<Stock> StockSet { get; set; }
    public IDbSet<Rental> RentalSet { get; set; }
    public IDbSet<Error> ErrorSet { get; set; }
    #endregion

If you have two projects, let's say a Class Library and a Web Application referencing it. 如果你有两个项目,那么让我们说一个类库和一个引用它的Web应用程序。 You need to add the connection from app.config to the web.config in your web application. 您需要将app.config中的连接添加到Web应用程序中的web.config。

When you target the class library with the Package Manager Console you are telling VS to look for a DbContext defined in that assembly, but the connection string is from the main Web Application. 当您使用程序包管理器控制台定位类库时,您要告诉VS查找在该程序集中定义的DbContext,但连接字符串来自主Web应用程序。

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

相关问题 实体框架代码首先创建数据库 - Entity Framework Code First Create Database 实体框架代码创建数据库的第一期 - Entity Framework Code First issue to create the database 无法使用Entity Framework代码创建数据库 - 首先+没有数据库 - Can't create database with Entity Framework code-first + no database 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE 使用实体框架(从数据库中获取代码优先)创建其他类 - Create additional class using Entity Framework (Code First From Database) 无法先使用Entity Framework代码创建数据库 - Can't create database with Entity Framework code-first 实体框架代码优先迁移始终尝试创建新数据库 - Entity framework code first migration always tries to create new database 使用代码优先实体框架创建数据库结构 - Create database structure using code first entity framework 无法首先使用Npgsql和Entity Framework代码在PostreSQL中创建数据库 - Unable to create database in PostreSQL using Npgsql and Entity Framework code first 如何使用实体框架代码优先方法正确创建数据库 - How to correctly create a database using entity framework code first approach
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM