Create SQL Server database tables from C# classes - c#

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.

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

Convert MS SQL Server Table to a C# Class

I have a project that is using Entity Framework Code First but with an Existing Database. So far I have been creating the class for each existing table accordingly and it's all worked out great. What would be brilliant is if there was some way to quickly generate a table to a class to save time on manually coding each class, any ideas? I want to stay with Code First so please don't suggest alternatives, thanks.
You can use EF database first - that will generate .cs classes you can use as a base. Just copy the CS code and then delete the EF database first stuff.

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.

Entity Framework Multiple Stored Procedures

I am quite new to the Entity Framework, and only have recently started looking into it. I have been using Linq to SQL for sometime now in a C# enviroement and found it really wonderful to use.
Currently I use sqlmetal to generate a DataContext File (Linq to SQL).
Now after some time I thought it would be nice to use to the Entity Framework, (Linq to Entities), I can see that in some respect there are syntatical similarities between the two, i.e. accessing and creating new instances providing the connection string.
However what Im interested in is when the mapping is generated, is there a way to automatically import all the stored procedures, similar to how sqlmetal does it. So that I dont have to import each one individually.
Thank you in advance.
In the Model Designer (inside Visual Studio, default view option for *.edmx files)
right click --> Update Model from Database
in the "Add" tab of the resulting dialog you can select any or all Stored Procedures.
Edit: Here's a screenshot of the dialog I'm talking about, found at a tutorial at robbagby.com

Working with Database using LINQ to XML

I am Working on a project (C#) in the university and they said that we can't use a DBMS like SQL Server so we decide to use Linq and XML...we learned some basics in Linq to Xml But really we don't know how we can create tables and fields and work with them in Xml.any suggestions ?
The simplest option would be a Typed Dataset saved to an XML file. (With or without LINQ)
You would use it just like an RDBMS, but it isn't an RDMS, so it should be allowed.
If it's a single user application you can just create a serializable class and use that to store your data. Then when your app is closed the data class is serialized (binarily) to a file, and when the app starts it reads the file and all your data is still there.
Here is a simple example from the docs.
If you really want to use tables, you can create a Typed DataSet and Save/Load it as (proprietary) XML.
A DataSet can hold multiple tables + relations between them.

Categories

Resources