I am currently developing some application in C# and I am using a n-tier architecture and embedded database SQL Server Compact. SQL Server Compact is pretty new to me, and I have some problem with connection string to this database. My n-tier application consists from DataTier, MiddleTier, PresentationTier. Every layer is separate project in solution.
DataTier contains the db file db.sdf and files for EF 5.0 including entities. Model was created with a database-first approach.
I had idea that I can build this embedded database directly into dataTier.dll and reference to it from other layers but I don't want to make connection string absolute, so I currently using connection string in this form:
Data Source = |DataDirectory|\db.sdf
in every layer, which is nonsense cause it creates separate database for each layer, but I can not find out how to make connection string relatively to database in dataTier. Is that even possible?
Could you point me to right direction or what is best approach to this problem?
Thanks
Related
I have developed a MVC web application using Entity Framework with SQL Server as backend. I have used a database-first approach.
Now my client wants to switch to an Oracle database. I am in the process of doing impact analysis of switching from SQL Server to Oracle database. My client believes that it is as simple as changing connection string.
My question is, considering the fact that the table name, table structure, relationships and attributes are all same with SQL Server database, what changes do I need to do in my project to switch from MS SQL to Oracle?
Do I need to create a new edmx file for Oracle database or do I only need to change the connection string and use the same context?
Also if in future this needs to be reversed, then is there a good practice or solution to support both the databases?
Thank you.
oracle has a 30 character limit for item (table, index, column) so if you are verbose in any of your naming you are going to have to rename a bunch of items.
i don't like using an edmx. it might be easier with code first models.
Generally projects are created using the below method:
Create a solution with 1 DAL class (this has a dbml file). Create a 2nd class project called BLL which is the business layer that creates the CRUD operations. Finally have a Asp .Net project.
First thing i do is in the DAL (Data Access Layer) i create a connection to the database and drag the required tables.
I create code to get,edit data etc, in the BLL project.
I then have to add a connection string in the Asp .Net project so it can connect to the database.
The issue i always seem to face is when i deploy the project to a test server i can change the Asp .Net projects web.config connection string easily, but at first run the application breaks (cant connect to the sql database) as in the DAL is still looking at the original connection string. So what i have to do is set the new connection string in the DAL project compile and copy that across which then allows everything to work.
I face the same issue when going from the test server to the live server. I've read about using config files but this is as far as i understand they can be used..... But surely there must be an easier way to,change the connection string in one place without having to recompile my DAL dll?
Are there tricks im missing or addons i could use to take advantage of?
If you dont want to use 2 connection string(one in web.config of your UI and the other one on your Data Access) and you dont want to depend of a specific database(in this case SQL Server), you can use just a single connection string on your web.config UI.
When you done that, on your DA, just use the DLL Microsoft.Practices.Enterprise.Data so you will not depend of SQL for connections.
Next time, when you want to migrate your application, for example, from SQL to Oracle, since you are not using anymore SQLConnection, you just change your connection string and its done!. Your migration to Oracle its done in 1 minute.
My application uses SQL Server Ce for the database and Entity Framework as the ORM.
Now I'm trying to switch to SQL Server Express but I'm having trouble doing so.
UPDATE
So after banging my head for 3 hours I finally figured out that you cannot use an Entity model generated from a SQL Server CE database against a SQL Server (in my case Express version).
I used a Diff program to view the generated files (Designer.cs) for both databases and I noticed these differences in the file:
SQL Server CE SQL Server
FK__Download__000000000000003F FK__Download__PlaneI__0519C6AF
So I have to create two entity models, but how can I interchange between the two in my program?
If I create two entity models, I'll have two classes with the same object names.
Thanks in advance
The EDMX file actually consists out of three parts.
CSDL, your conceptual model
SSDL, the storage model
MSL, the mapping between those two.
Normally these three parts are embedded in as a resource in your assembly and the connection string that you use tells the runtime to look for them in that assembly.
You can however use physical files and deploy them with your application. In such a way you can use the same CSDL but a different set of SSDL/MSL for the specific database. In that way you can reuse your entity model against both your SqlCE database and a SQL Server database.
Here you can find a blog post about swapping EF metadata
In my first c# project, I need to connect to a database server for multiple read only queries. Would anyone share experiences on how to organize the queries into the project? currently I just hardcoded query strings in the c# source files whenever needed. but it is hard to maintain and once something changes on the database server side I am in trouble. Or should I put all query strings in the .config file using appsettings? Are there better ways? I do not have rights to save stored procedures on the server. thanks.
There are different answers with varying levels of sophistication based on your needs. Except in the very smallest of projects, I create two class library projects for database access: one that contains the data model and queries and another test project that exercises the first project's queries. In simple solutions, you use this library in an ASP.NET or other project.
You should strongly consider learning an ORM like NHibernate or VS 2008/.NET 3.5's Linq-To-SQL or Entity Framework. Minimally, you MUST remember to use parameterized queries if you have a web-facing app.
In more sophisticated solutions you will completely encapsulate the database into it's own service, or tier. In my experience I had a data access tier that ran in it's own Windows Communication Foundation service, as a Windows Service, and it was the only service that could talk directly to the database or knew the database's data model. It would do all the interaction with the database, and then transform the data into different data models that are read by the other tiers. I typically create a project called "Contracts" that contains all the interfaces and data models that are communicated from the data tier to the rest of the system. The reason you do this is so that you avoid the pain you have mentioned: you can update the underlying database, ORM layer, and "common data models" and then not change the other tiers at all.
If this is your first project, try to keep thinks simple. If you add too much variables probably you'll end thinking more in technology than in solutions.
That said, if your queries don't expect to change it's parameters, you can use stored procedures. This approach also will help boost your queries as the execution plan will be kept in the database.
I just started to use LINQ-to-SQL, so my problem may be trivial.
We have a central database running on SQL Server 2005. There are distributed desktop .NET 4 applications which save measurement data into a local SQL Server Compact database. These local database (SDF) files are regularly transported to the server, where they are imported into the central database using SqlBulkCopy.
The distributed desktop applications use LINQ-to-SQL to handle data and to create their local SDF database. The ORM is currently done by a manually written DataContext subclass, compiled as a separate library, which contains a nested class for every table of the central database. I wrote the DataContext subclass by hand simply because I wanted to avoid code generators before I more-or-less understand how LINQ-to-SQL works.
The central database is simple at the moment, but it will structurally expand soon, by adding new tables and adding new versions of existing tables. The problem is, it would be nice to automate the generation of the DataContext subclass. In an ideal situation, this could be done as part of the daily build process. This way after the database team changes the database, the application developer team would get the new version of the ORM library. (Old code would not break, since every old table would stay in the database. Old versions of the tables will be deleted only when none of the distributed application versions use them.)
So my question is, what is the best way to generate a DataContext subclass for an existing database? I would prefer a command line tool or an API. Thank you for your help in advance!