Generate model classes from Database for Linq to SQL - c#

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.

Related

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

Entity data inheritance

I'm currently involved in a project where we will present data from an external data source to visitors, but we will also provide meta data for the entities/rewrite some of the original data.
The external data source is a SQL Server database which I've created an .edmx file from and I've created an additional, controllable, SQL Server database with it's own .edmx file. But I'm not comfortable with using two entities for what, in my eyes, is one type of data.
Somehow I would like to merge the two data sources into one, and use only one entity class which I could query. Inheritance in LINQ to Entities would be perfect, but I would prefer no to change the .edmx files manually.
As it is now I have to create wrapper classes and populate them manually with the entity classes, or use multiple database queries to fetch the required data which is a big turn off performance wise.
It feels like it have to exist some sort of work around for these problems I'm facing?
You have two options here.
First you can extend the entity framework class by using partial
classes. It will help you avoiding changes to the generated classes.
Second you can use Entity Framework code first, Which i will
recommend as you will have more control on your entities.

Create SQL Server database tables from C# classes

Is it possible to automatically convert a lot of C# classes to SQL Server database tables ?
I need to import some xml files into SQL Server but using SSIS the runtime stops working as soon as I select the xsd file. However I easily converted the xsd to class files so that's why I am asking if its possible to have a workaround...
You can use Entity Framework Code First functionality, with a little extra work to get the tables done in your database.
There you have a good post on it by ScottGu
Yes, it's possible to automatically do this. Entity Framework has a model called Code First, which essentially does this: takes your C# classes and creates the database and tables automatically for you.
Take a look at this post by Scott Guthrie.
Other option you might test is DataSet.ReadXml() function. Drawback is the Dataset can't handle complexType="mixed", but it deals well with large files (my files had about 50M each). All tables and columns are named by XML tags and relations are autogenerated by DataSet itself.

Create a table in an existing DB with LINQ to SQL

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.

Mapping existing tables to Domain classes in C#

I don't even know if I'm using the correct terms but here goes:
Is there a way to map the tables and their relations in a SQL Server to domain (C# code) automatically, by means of a tool or something?
I've used the nhibernate plugin, but it creates a file in .cs and another in xml, that has the mapping, but I want that mapping to be present as "property" in the .cs file.
Sorry if this is a bit confusing.
You could always use the Entity Framework or maybe Linq2SQL, but I'm not familiar with how that works.
Tho, out of EF and NHibernate, I prefer NHibernate.
Use LLBLgen of Linq 2 Sql (available in VS 2008). Or use Entity Framework (in VS 2008 SP1).
Linq 2 sql allows you to drag and drop a table from sql server to the canvas and it creates a domain class for you with properties mapped to columns.
Take a look at Fluent NHibernate and see if that is what you are looking for:
http://blog.jagregory.com/2008/08/08/introducing-fluent-nhibernate/
You can use Castle ActiveRecord which uses attributes.
I think you should read a bit about Linq to Sql
http://searchwindevelopment.techtarget.com/tip/0,289483,sid8_gci1269859,00.html#
Check out SubSonic. It will generate your DAL and can also generates code off of custom templates.

Categories

Resources