How to Connect to Azure Synapse Database using Entity Framework? - c#

We have Entity Framework DB First architecture in our project and where we were required to connect to Azure synapse Database from relational Sql server Database for migration purpose.
As Azure Synpase is PAAS and not relational Database , we are not able to map Data in Database to those into our models using EF edmx.
As we know EF is a ORM i.e object relational mapper which mapps relations from DB to objects.
Error :
Unable to generate the model because of the following exception:
'System.Data.Entity.Core.EntityCommandExecutionException: An error
occurred while executing the command definition. See the inner
exception for details. ---> System.Data.SqlClient.SqlException:
'columnproperty' is not a recognized built-in function name. at
System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection, Action`1 wrapCloseInAction)
while creating edmx from Azure synapse Database we are getting below error
Error :
Unable to generate the model because of the following exception:
'System.Data.Entity.Core.EntityCommandExecutionException: An error
occurred while executing the command definition. See the inner
exception for details. ---> System.Data.SqlClient.SqlException:
'columnproperty' is not a recognized built-in function name. at
System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection, Action`1 wrapCloseInAction)
We needed to connect to Azure synapse Database using Entity framework using any means not only Database first approach it can be any.

For this problem, we tried multiple options for EF Database first approach but here EF Code First approach solved our problem for connection to Azure synapse.
Using existing edmx you cannot connect to Azure synapse.
You need to follow below steps for solving you problem.
Create new Model classes that are present in your edmx.
Create new DBContext class in Models folder.
Create new controller
In controller get method you can use C# lambda expression to query the data from new DBset like below.
IQueryable<test_customer> result = db.customerDbset
.Where(n => n.job.Equals(job))
.ToList().AsQueryable();`
Add new controller:
Select new model and new Dbcontext class in below fields:
Make below changes in DbContext class for new mapping as per azure db table:
In OnModelCreating method you need to mapp new table from Azure synapse to our new model class like in above image.
and add Dbset like below in Dbcontext class
For example:
public DbSet<test_customer> customerDbset { get; set; }
After all this is done remove your all reference from existing edmx file and unload that edmx from your project.

Related

Migration exception when trying to update database using Entity Framework 6.2

In a Winforms application in C# that is connected to a SQL Server Compact Edition (CE) database, I get an exception when I try to use a method to add and save new object into database.
The error is
System.Data.SqlServerCe.SqlCeException (0x80004005): A duplicate value cannot be inserted into a unique index. [ Table name = __MigrationHistory,Constraint name = PK_dbo.__MigrationHistory ]
I try to create migration with tag -IgnoreChanges but it throws the same exception too. How I can solve this problem ?

How do I add columns to database without users losing data when a new version of the application is published?

I have a c# .NET application with a SQLLocalDB database. I have used database first to create the EF6 model. I have added columns to one of the tables using SQL Server Management Studio and then used 'Update model from database' to propagate the changes into my model. When doing this, data on the development PC is unaffected and incorporates the new columns. However, when a different user runs the new version of the application, their existing datafile (.mdf) won't recognise the new columns and crashes with the exception:
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'VAT_long_description'
making the user's existing data in that table unreachable. I appreciate that Code First gives the ability to migrate data, but this facility appears not to be available when building the EF model using database first.
My connectionString is:
<add name="PMMEntities" connectionString="metadata=res://*/PMMData.csdl|res://*/PMMData.ssdl|res://*/PMMData.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0; AttachDBFilename=|DataDirectory|\PMM.mdf; initial catalog=PMM;Integrated security=True;MultipleActiveResultSets=True; App=EntityFramework""providerName="System.Data.EntityClient" />
I want to ensure that when a user installs a new version, their .mdf datafile will adapt to the new database schema without loss of data. It seems the only way to do this is through running a conversion or migration method on startup if the app throws the “Invalid column name” SQL exception. Where can I find the code (or a NuGet package) that will do this?
if you are using MVC , you have two methods to make an update in your models class database. the most easy way to resolve that issue is that you delete the model class related to the database and you import it again into your model folder. don't worry about data, they will still stored in your database. unless you have wrotten some extra object in the model class database, in this case you will waste those extra objects.. it will not affect your storage.

C# switch from local db to external (Azure) db causes errors

I have used the built in Database that comes with Visual studio by using Code First with Entity Framework. Now I wanted to move to an external database so I created one and saved the connections string. So I connected to my azure database by supplying the connection string in the db context constructor. Now though, the problem is that Entity Framework isn't able to create the necessary tables. When I run my application and try to access something, I get System.Data.SqlClient.SqlException: 'Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.'
And I assume this is beacuse my azure db is empty. Why doesn't Entity Framework create the tables?
The error was that I had forgotten to run migration on the new database:
*Add-Migration newDb
*Update-Database

Entity Framework with IBM DB2

We have a DB2 database which we are accessing via EF. We are able to connect to the database and do read & write operations as part of this.
Now the plan is to initialize the DB using
Database.SetInitializer(new CreateDatabaseIfNotExists<CustomContext>())
This throws out an error saying
HResult=-2146232032
Message=CreateDatabase is not supported by the provider.
Source=EntityFramework InnerException:
System.Data.Entity.Core.ProviderIncompatibleException
Previously we were connecting with
Database.SetInitializer(new NullDatabaseInitializer<CustomContext>());
and this was working fine.
The question is has any one tried creating a new DB2 database from within EF?
You cannot do that.That is Known limitation of the provider.
General limitations:
Only database-first scenarios are supported: any database object that
you reference in Entity Framework must first exist in the database.
Invocation of store-specific functions is not supported.
Trusted context connection properties that you set in the Server Explorer Add
Connection dialog are not passed to Entity Framework connections.
You can read it here : Limitations to Microsoft Entity Framework support
Migration is not supported by IBM EF provider implementation.
If you need DB2 migration support you can use this package that implements only migration (so you can use it in addition to IBM DB2 EF Provider)
https://www.nuget.org/packages/System.Data.DB2.EntityFramework.Migrations/
You can find more info here
https://db2ef6migrations.codeplex.com/

How to make entityFramework asking me before create db;

I want to make Entity Framework showing me message : 'There is no database' (if database not exist) and asking me for create db or not;
i use CodeFirst.
In your 'DbMigrationsConfiguration' class, you should turn off any automatic migrations:
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
Now on bootup, it won't do anything other than throw an DBMigrationException when you first access the database. So, before you access the database, check to see if it exists by using the following:
dbContext.Database.Exists();
You can then ask your questions about server location, etc, followed by creating the database with:
var migrator = new DbMigrator(new Configuration());
migrator.Update();
This isn't something built into Entity Framework, nor does it make sense to build it into Entity Framework, because it is so specific to your particular application that whatever you come up with won't be directly usable in any other application.
You can simply configure EF to not automatically create any database. If connecting to the database fails, check if you can connect to the server. If you can connect to the server, check if the database exists. If the database does not exist, offer to create it.

Categories

Resources