Restrict SqlEntityConnection Type Provider to only certain tables - sql

I am writing a utility for myself that needs to be able to access a pair of tables in a SQL database. I used the SqlEntityConnection Type Provider and was rewarded with the data I needed from the table as easy to use entities.
One thing I noticed though was that startup and compiling of the project increased by quite a lot. I suspect this is because the database has over a hundred tables and it's compiling and getting data from all of them as opposed to just the two I need. Is there a way to restrict the EntityTypeProvider to only referencing the needed tables in the schema?
type private EntityConnection = SqlEntityConnection<ConnectionString="Server=Server;Initial Catalog=Database;Integrated Security=SSPI;MultipleActiveResultSets=true", Pluralize = true>
let private context = EntityConnection.GetDataContext()

I have not tried this myself, but I think you could add a new "ADO.NET Entity Data Model" (edmx) file to your project, let it generate from your existing database, and then delete from the model every table you don't want accessible to your code.
The EDMX designer will generate a *.csdl file that you can then reference from the LocalSchemaFile parameter of SqlEntityConnection. You'd use this parameter instead of ConnectionString.
The end result is that the entity provider would not automatically pick up changes to your database, but compilation times will go down, and only the tables you care about would be visible to your code.

Related

Entity Framework Core Error: dotnet.exe : System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Company' in the database

I am building a application with ASP.NET Core MVC6 and Entity Framework Core code first with build in DB context, the SQL database has already been populated with data records. I recently made some small changes to the data models and recreate the migration, with commands as ("dotnet ef migrations add Stage3", "dotnet ef database update") in VS 2015 Package Manager Console, but it ran into error as:
dotnet.exe : System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'Company' in the database.
Company table is on the top of the tables relationship, it seems that because the table Company is already there and the EF can not update the new table structure. If I change DB name in the connection string, it will create new database with new table structure without any issues. I am not sure how to address this issue? After the application go live in the near future I will properly make more changes to the Modes and will have same issue again and I cannot delete database with live data to recreate new table structure, Maybe I should configure it in the Startup.cs file, but I haven't found any useful resources yet. Please give me some advises.
I have attempted to change the DB Initializer as attached screenshot, but not sure how to do it.
I checked the project code again, the migration has not been applied to __MigrationHistory table, the migration code actually contained the code to create whole database structure as sample below:
migrationBuilder.CreateTable(
name: "Company",
columns: table => new
{
CompanyId = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
CompanyName = table.Column<string>(maxLength: 100, nullable: false),
IsAdmin = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Company", x => x.CompanyId);
});
And I haven't changed the project namespace. Recently I just made some changes on few table relationships such as site user permission table(company has many sites). I added a permission table, so now site user permission table can have multiple permissions type instead of single permission type.
Not sure how to set up automatic migrations in Entity framework core.
In Entity framework code first approch, there are four different database initialization strategies:
CreateDatabaseIfNotExists: This is default initializer. As the name
suggests, it will create the database if none exists as per the
configuration. However, if you change the model class and then run
the application with this initializer, then it will throw an
exception.
DropCreateDatabaseIfModelChanges: This initializer drops an existing database and creates a new database, if your model classes
(entity classes) have been changed. So you don't have to worry about
maintaining your database schema, when your model classes change.
DropCreateDatabaseAlways: As the name suggests, this initializer drops an existing database every time you run the application,
irrespective of whether your model classes have changed or not. This
will be useful, when you want fresh database, every time you run the
application, like while you are developing the application.
Custom DB Initializer: You can also create your own custom initializer, if any of the above doesn't satisfy your requirements or
you want to do some other process that initializes the database using
the above initializer.
So if you are using DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways then replace it with CreateDatabaseIfNotExists.
Please try this out.

NHibernate (and Fluent): Possible to prevent a specific table from being created via SchemaExport.Create?

I'm using Fluent NHibernate (and I'm a newbie). I have mapped a read-only table that already exists in the database (it's actually a view in the db). In addition, I have mapped new classes for which I want to create tables using SchemaExport.Create().
In my fluent mapping, I have specified "ReadOnly()" to mark the view as immutable. However, when I execute SchemaExport.Create(), it still tries to create the table so I get the error "There is already an object named 'vw_Existing'".
Is there a way to prevent NHibernate from trying to create that specific table?
I supposed I could export and modify the sql (SetOutputFile), but it would be nice to use SchemaExport.Create().
Thanks.
You're looking for
SchemaAction.None();

Doctrine schema changes while keeping data?

We're developing a Doctrine backed website using YAML to define our schema. Our schema changes regularly (including fk relations) so we need to do a lot of:
Doctrine::generateModelsFromYaml(APPPATH . 'models/yaml', APPPATH . 'models', array('generateTableClasses' => true));
Doctrine::dropDatabases();
Doctrine::createDatabases();
Doctrine::createTablesFromModels();
We would like to keep existing data and store it back in the re-created database. So I copy the data into a temporary database before the main db is dropped.
How do I get the data from the "old-scheme DB copy" to the "new-scheme DB"? (the new scheme only contains NEW columns, NO COLUMNS ARE REMOVED)
NOTE:
This obviously doesn't work because the column count doesn't match.
SELECT * FROM copy.Table INTO newscheme.Table
This obviously does work, however this is consuming too much time to write for every table:
SELECT old.col, old.col2, old.col3,'somenewdefaultvalue' FROM copy.Table as old INTO newscheme.Table
Have you looked into Migrations? They allow you to alter your database schema in programmatical way. WIthout losing data (unless you remove colums, of course)
How about writing a script (using the Doctrine classes for example) which parses the yaml schema files (both the previous version and the "next" version) and generates the sql scripts to run? It would be a one-time job and not require that much work. The benefit of generating manual migration scripts is that you can easily store them in the version control system and replay version steps later on. If that's not something you need, you can just gather up changes in the code and do it directly through the database driver.
Of course, the more fancy your schema changes becomes, the harder the maintenance will get i.e. column name changes, null to not null etc.

Get column names when you load from an access file

I load 10 tables from an ACCESS 2007 file database. IS their a way I can get the column names into the dataset or do I have to rename each column? I am using Visual Studio 2008 in VB.NET.
I would like to reference the columns in the code by the name and not have to use an index
I just went ahead and added the following code for each of the tables:
Table1.column(0).name = "NAME"
Table1.column(1).name = "ADDRESS"
Table1.column(2).name = "CITY"
Table1.column(3).name = "STATE"
But I do this for 10 tables so it can add up fast on the lines of code. But if their is a way for me to read it from the file I would like to replace all the lines of code with one line of code.
When you create the dataset, if you provide a connection string locally, when you generate the xsd file it should automatically type out any tables you add to the set. As long as you're using the tableadapters built into .Net, you can change the connection string of any tableadapter you use for your local config when you deploy.
When you right-click in the blank space of the new dataset you create (in the designer), you can add->tableadapter. It'll ask you for the connetion string to the database (provide it), and then you can submit a query. Start with the a general query for the basic table you're looking for. You can then add subsequent queries as you need them to the same table or create custom tables (and names) based on joins/other queries.
EDIT: I'm assuming some version of .Net is being used here.

Few questions from a Java programmer regarding porting preexisting database which is stored in .txt file to mySQL?

I've been writing a Library management Java app lately, and, up until now, the main Library database is stored in a .txt file which was later converted to ArrayList in Java for creating and editing the database and saving the alterations back to the .txt file again. A very primitive method indeed. Hence, having heard on SQL later on, I'm considering to port my preexisting .txt database to mySQL. Since I've absolutely no idea how SQL and specifically mySQL works, except for the fact that it can interact with Java code. Can you suggest me any books/websites to visit/buy? Will the book Head First with SQL ever help? especially when using Java code to interact with the SQL database? It should be mentioned that I'm already comfortable with using 3rd Party APIs.
View from 30,000 feet:
First, you'll need to figure out how to represent the text file data using the appropriate SQL tables and fields. Here is a good overview of the different SQL data types. If your data represents a single Library record, then you'll only need to create 1 table. This is definitely the simplest way to do it, as conversion will be able to work line-by-line. If the records contain a LOT of data duplication, the most appropriate approach is to create multiple tables so that your database doesn't duplicate data. You would then link these tables together using IDs.
When you've decided how to split up the data, you create a MySQL database, and within that database, you create the tables (a database is just something that holds multiple tables). Connecting to your MySQL server with the console and creating a database and tables is described in this MySQL tutorial.
Once you've got the database created, you'll need to write the code to access the database. The link from OMG Ponies shows how to use JDBC in the simplest way to connect to your database. You then use that connection to create Statement object, execute a query to insert, update, select or delete data. If you're selecting data, you get a ResultSet back and can view the data. Here's a tutorial for using JDBC to select and use data from a ResultSet.
Your first code should probably be a Java utility that reads the text file and inserts all the data into the database. Once you have the data in place, you'll be able to update the main program to read from the database instead of the file.
Know that the connection between a program and a SQL database is through a 'connection program'. You write an instruction in an SQL statement, say
Select * from Customer order by name;
and then set up to retrieve data one record at a time. Or in the other direction, you write
Insert into Customer (name, addr, ...) values (x, y, ...);
and either replace x, y, ... with actual values or bind them to the connection according to the interface.
With this understanding you should be able to read pretty much any book or JDBC API description and get started.