Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I created ASP.NET CORE 1.1 code first Project in Visual Studio 2017,

I made my model classes and AppContext class for Database, but my Database created only when i made Migration,

that error appear when try to add any data to database:
System.Data.SqlClient.SqlException: 'Cannot open database "Mydatabase" requested by the login. The login failed.

Login failed for user 'ServerName-3FT4ANB\MyName'.'

But if i make migration first this error not happen ?

What I have tried:

I search but cannot find what i need to understand this point
Posted
Updated 2-Jun-17 20:34pm
v2

1 solution

The following comments are what i've seen in my experience.

Quote:
I made my model classes and AppContext class for Database, but my Database created only when i made Migration,


Your database gets created when you take an action against the DB. For example, simply running your application won't create the databse, but if you go and register a user to the database (or insert a user) then the schema will be updated.

Quote:
But I thought that the Database is created when i run my application and every time i run application it created with any update i make in the model classes

and any change i make to class model will reflect on the database, as example when add new property to specific class that add new column in the table in the database


Your database doesn't just know to update itself based on code, you either have to run the command Update-Database (Entity Framework Code First Migrations[^]) or it looks like you can setup your web.config to automatically update the database (c# - Entity framework code first - how to run Update-Database for production database - Stack Overflow[^]) but I would think this is a terrible idea as you'd be committing DB changes that, without being tested, could break your app.

Quote:
But what about using application in Production level by end users ?


Not sure what you are asking here, but using code first is fine for production. Just create a deployment process that screens any production related migrations (this is where i would advise against auto updating DB via web.config) that could break your environment.
 
Share this answer
 
Comments
MrLonely_2 3-Jun-17 2:30am    
Thank you David for your reply, that exactly what i need:
Your database gets created when you take an action against the DB.
But, that error appear when try to add any data to database:
System.Data.SqlClient.SqlException: 'Cannot open database "Mydatabase" requested by the login. The login failed.

Login failed for user 'ServerName-3FT4ANB\MyName'.'
But if i make migration first this error not happen ?
David_Wimbley 5-Jun-17 13:16pm    
I think your error itself is pretty clear. The migration creates your database and all that is required where if you try and run your app pre-migration...your DB doesn't exist.

"System.Data.SqlClient.SqlException: 'Cannot open database "Mydatabase" requested by the login. The login failed.

Login failed for user 'ServerName-3FT4ANB\MyName'.'"

It seems as though Mydatabase doesn't exist at that point which is why you are getting this error.
MrLonely_2 6-Jun-17 19:02pm    
yes, but what is the solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900