Add column in database then update the dbml - c#

I have an application that have a dynamic table in database. The problem is, let say there's a change in table structure like addition of column, can I update the dbml file dynamically? so I don't have to fix the application everytime the column been added

Using the DBML and code generation approach, no there's no way to let your application take advantage of the changes automagically. The DBML results in new generated code which needs to be compiled, that requires the DSL Toolkit, Visual Studio and the .NET SDK to be present.

Related

C# Edmx - update a database

I have a question. I have a project that uses an edmx and all hooks up fine to the sql server.
I have installed it in several servers.
What I want to know is, having made changes in development so m database and my edmx are change,s say a new table and a modified one.
How do I make it so these updates are aplied automatically when I publish an app update?
I remember once a long time ago doing code first database work and making some kind of file or procedure that would automatically apply these changes when the app ran.
Can any one fill me in again.
1.- AFAIK, when you're using model first or database first, you can't use Migrations, you generate a DDL script that when executed, creates the database or drops and recreates tables losing any data you had.
See This link read point number 5.
"The script that is generated will drop all existing tables and then recreate the schema from scratch. This may work for local development but is not a viable for pushing changes to a database that has already been deployed. If you need to publish changes to a database that has already been deployed, you will need to edit the script or use a schema compare tool to calculate a migration script."
What I do here, is make the changes to the database and update the model (Right click edmx designer, click "update model from database")
2.- You can configure updates on a ClickOnce installer, any changes made to the edmx will be published on your app.

Update MVC database first to reflect db changes

i'm new to MVC and have to update my model.
I have an SQL Server database already connected to my project, today I add a new column. Now I need to update my MVC code to reflect the change.
Everything online tells me to update the .edmx and .tt files with the Update Model Wizard since they are auto generated, but I cannot find the wizard in VS 2012 so I am at a stand still.
Is this the right way I should be doing this? Any tips would be great...thanks
There are a couple options you can do:
If the database is empty or there isn't a lot of data that you care about, you can drop the database and recreate it.
Manually add the column in the database (after property is in model)
There is something called Code First Migrations. This will allow you to update your database. Here is a tutorial you can look at for this: http://msdn.microsoft.com/en-us/data/jj591621.aspx
Double click the edmx file. This opens the diagram of the database model.
Right click and select "Update Model from Database..."
That will start the wizard you're looking for.
Be aware that not all changes come through automatically.
Most obvious is that columns will not be removed from model just because they're gone from the db. If you're dealing with lots of modifications or deletes rather than additions, it may be best to just drop the model and recreate it (as suggested above by Andrew).
No matter which approach you take, I'd suggest making sure you have all other local source changes committed or shelved before starting because you may need to do an undo check out if EF gets freaked out.
I ended up downloading this update of VS 2012 and repaired. Now I am able to update my model.

Changing the schema of a table in SQLite in c#

I am using SQLite DB to store data in my c# project. I have an existing table which has some records. Now I want to insert two more columns in existing table. How can I insert the data with my C# code for those two columns. Do I need to create the table again or altering the table will do?
The project also has designer.cs corresponding to that table which is getting generated automatically. I want to know how this file is getting generated.
for changing to schema in sqlite u need to make appropriate changes in .XSD file in your project. This will change the designer.cs automatically and ur schema will be changed. You can search ".xsd" in ur project and can go ahead.

How to import existing mdf file into a lightswitch project?

I would like to copy table definitions from an existing database file into a new lightswitch project, does anyone know how to do that, it seems like you can only create a new database from scratch or attach an external database, but when i try to attach an external mdf file from another VS project I get an error which states: CREATE DATABASE Permission denied in database 'master'.
Any ideas about how to copy the table definitions from an existing database file into a lightswitch project?
If you have the external database attached on an instance of SqlServer, you shouldn't have no problem attaching to it from LightSwitch. From that point on, you can rename tables and columns, change datatypes etc, as well as adding more tables and structures to that DB.
Note that the DB is supposed to be served from a SqlServer instance, not a file on your file system.
As far as I know it can't be done. Lightswitch holds a representation of the database tables separate from the actual database definition. You can probably go the other way however because when the ApplicationDatabase.mdf file does not exist you can start Lightswitch (you will notice the table definitions are still in there) and then build the app which will recreate your ApplicationDatabase.mdf. It is empty of course but If you could find out where lightswitch stores those table definitions (an *.xaml file somewhere?) you might be able to get those copied into your project and then generate the ApplicationDatabase.mdf from there?

visual studio DataSet designer with stored-procedures ? what is this icon

What does this icon mean is Visual studio, visual Data Set designer.
I have several stored procedures.
can you help me understand,
why the one on top of the list has a
small "check mark"
why I can't delete it if I need to.
This is not the case with the rest.
Why is this "special" ?
Thanks
Because that item contains the method(s) that define the schema of the table the TableAdapter is part of.
The default names are Fill and GetData. Looks like you renamed them.
Each table has a default query (The one on top with the check on it). When you dragged your tables in to the dataset to create the query, it wrote a SQL statement which it uses to schema your table. Keep that query simple, you might not actually use it in code, and you can always edit that query to update the table schema. In the picture you provided, your default query is actually a stored procedure.
Every time you open the default query it connects to your datasource and allows you to select new columns that weren't in there before. If you want to update your existing columns, delete all the columns out of the table before you attempt to open the query. When you save the query, your updated columns get added back.
Make sure your connection string has permissions to view column information.

Categories

Resources