Create a table in an existing DB with LINQ to SQL - c#

I have a SQL Express 2008 DB that has gone live. I'd like to create a new table in that database without deleting all the existing data in the live DB.
I created an entity class for the new table and a separate data-context.
What is the best way for me to add the new table using LINQ to SQL
Thanks...

That's not Linq To Sql's role in your architecture. You need to create the tables prior to deploying your new version of your data model.
You can either manually update your db w/ direct SQL commands against it, or you can package it w/ the deployment (e.g. via continuous integration scripts or something like Migrator.NET)

In the new Linq to Entity which shipped with Visual Studio 2010 there is easy way to create Database table from Model you built.
here is the Example : Create a Database using Model-First

Why not just execute the SQL statement, like create table [mytable] ...?

Why you add unmapped-entities to diagram? I think, first you have to create tables after map entities.

Related

Entity Framework migration from MySQL to SQL Server and the Table names missing "dbo"

I am maintaining an existing (and working) C# project which uses Entity Framewwork 6 to do the ORM. Now I need to migrate the database from MySQL to SQL Server. So far I have been able to make the program compile by modifying the .edmx file (For example, I changed the Provider from MySql.Data.MySqlClient to System.Data.SqlClient and ProviderManifestToken from 5.6 to 2008. Also I needed to change some data types in the ssdl content, for example from double to float).
Now when I run a simple LINQ query against the ObjectContext-derived context like this:
var query = from data in context.user select data;
var users = query.ToArray();
It gives me the exception "Invalid object name 'MYDBNAME.user'."
Apparently a ".dbo" is missing, i.e., the translated SQL query should be
select * from MYDBNAME.dbo.user
instead of
select * from MYDBNAME.user
But I am not sure how I can instruct EF to add the ".dbo". Is there some configuration/option to do that?
It's very simple and easy approach You have to follow the below steps I hope it will work in your case.
You have to build all your schema in MSSQL Server from that MYSQL
Once you have completed your Database Schema in MSSQL Server
Then you have to follow the database first approach in Entity Framework and It will automatically build all you models and DB context from the database.
How to generate DB models and context (Database First Approach) Data Base First EF
Then You will be able to apply Linq on the database and queries. Thanks

Write data to two SQL databases from Entity Framework in c#

I am having an issue with my website(ASP.NET, c#, SQL, Code-first Entity Framework).
I have a project with an attached SQL database generated from code first entity framework.
Now I have imported another SQL database using model first database which looks almost same but the table and column names are different.
So now I would like to write data to two databases at the same time with just one click from my web application.
The newly attached database will be a backup and we should write data to both databases at the same time.
Any suggestions would be highly appreciated.
Thanks
As others have suggested, you need to do the mapping yourself, but one thing I would like to add, you may need to wrap your SaveChanges() into a transaction, you may find steps here: https://msdn.microsoft.com/en-us/data/dn456843.aspx

Generate model classes from Database for Linq to SQL

I have VS Database project with schema and existing published Database.
Now i have internal routines to arrange some data in this database and i'm going to use Linq to SQL for this. Is it possible to auto-generate model classes from Database project or existing database? I've created mapping XML, but to use it i need to generate classes. Is there any automation?
This tutorial should help you: http://www.asp.net/mvc/tutorials/older-versions/models-(data)/creating-model-classes-with-linq-to-sql-cs
You have to add add "linq to sql classes" which will generate you models.

Mapping and sql database creation

I started a project and use Entity Framework 5.
Now I created a database on my SQL Server Express with all tables.
Further I created the DbContext with a fluent mapping.
What is better, the fluent mappings or the .edmx mapping files?
The database is now on the SQL Server but I want support also SQL local db.
Is there a way to tell the EF that I want to use a SQL local db?
Should I ship the whole database within the setup or better to create the database on startup of my application? How can I use EF to create the database (SQL server or SQL local db)?
Every useful help would be appreciated.
If you are using EF5 I would stay away from edmx.
You could reverse engineer your model from database using Entity Framework Power Tools.
Than you can customize your model using either Data Annotations or fluent mappings.
If you use EF5 code first it can create database automatically for you if not present, however that would not work very well on subsequent upgrades (it can recreate database but then you will use or your existing data). The options you could use, is either EF migrations, where you can specify in fluent-like languagage the modifications that were made to your database or use Database Project in Visual Studio, where you can store all your schema in source control and then generate database upgrade scripts betweeen releases.

ADO.NET command to script database creation

Is there a SELECT command I can execute in ADO.NET to retrieve a script that will rebuild my database from scratch with empty tables? I'd like to be able to script the creation of the database itself, along with all the tables, views, and stored procedures and any relationships that exist between the tables. Also primary key and identity specifications.
I am using SQL Server 2000 and C# (.NET Framework 2.0).
There is no such SELECT command but you can use SQL Server Management Studio to generate such a script for you.
Context menu for a database in Object Explorer has Script Database as option that should do the trick for you. Then you could execute the resulting script from your program using standard SqlCommand objects.
Depending on what you are trying to do, you may consider to build your database structure into the model (System databases / model). Then, each time you'll create a new database, you'll get all the stuff you need already inserted into your new database.
But be warned that this solution will create all the stuff for ALL the new databases created.

Categories

Resources