asp.net mvc 5.1 mysql Entity Framework Controller Generation - c#

I have a asp.net mvc 5.1 solution with 3 projects in it shown by the image below:
I have Ninject, FakeitEasy, MySql nuget packages installed in my project and I am using the repository pattern in my project so I have an IRepository generic interface, IDbContext interface and a concrete repository generic class (all this is in the AccountManager.Domain).
With everything Ninject and mysql all setup, I built the solution and so far so good until I tried to add a controller using the scaffolding options given with Entity Framework and I get an error saying:
Unable to retrieve metadata for 'AccountManager.Domain.Entities.Bank' using the same DbCompiledModel to create contexts against different types of Database servers is not supported. Instead create a separate DbCompiledModel for each type of server being used.
I saw this when I was head banging with sqlite and EF code first. What does it mean again? Anyone see some perspective I don't?

I have been plugging away at this annoyance for a while and I found that its something that is just simple. Decorating my context class with this mysqlefconfig like so:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
helps the context to make the necessary adjustments to the migration table EF creates automatically so it works with MySql is the reason why my controllers didn't scaffold at all. I got the idea to try comment this out from https://stackoverflow.com/users/3671905/yurko and the scaffolding worked gorgeously.
Only thing to note is that after you finished your scaffolding you need to uncomment it so that all the MySQL peculiarities required by EF should be restored. I prefer attribute configuration than creating files and editing infrastructure files.

Well, for me the only thing that worked was delete everything in the config file(app.config or web.config) and paste the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description="Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MyContext" connectionString="server=127.0.0.1; User Id=root; Password=password; Persist Security Info=True; database=Test;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
</configuration>
Source
After that i've been able to generate the controllers, i hope that it helps.
(And of course, its necessary to configure correctly the connection strings)

Related

How to set SQLite database file to a binding source? (VS 2017/2019/2022) [duplicate]

I thought that sqlite was simple but it is giving me a hard time. I just want to create an application where I can connect to a sqlite database using the ado.net entity data classes.
I am having this problem when testing the application on a virtual computer running windows xp. the application works fine on my current computer and also on my laptop when I deploy them.
Here is what happens on the virtual computer :
The application is able to launch.
The application is able to interact with the database using System.Data.SQLite
The application is not able to connect to the database using The ADO.NET Entity data models
when I try to connect I get the following exception:
I know there are a lot of post that talk about this and most of them say that you need to download the .NET provider for Sqlite.
I have already installed the sqlite-netFx40-setup-bundle-x86-2010-1.0.79.0.exe and I get the same problem. What should I do?
Edit
I managed to establish a connection by adding:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
to my app.config file.
The problem is that now I cannot select data nor insert records to the database. The exception that I get when I try to insert a new record now is:
A null was returned after calling the 'GetService' method on a store provider instance of type 'System.Data.SQLite.SQLiteFactory'. The store provider might not be functioning correctly.
had to add:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
to my app config file. and it now looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
In the location where sqlite was installed I had to copy
to my output directory where my program exe is located
Install SQLite Toolbox through extensions
Download the latest setup file sqlite-netFx46-setup-bundle-x86-2015-1.0.xxx.0.exe
Install the correct x86 installation, installing VS designer components is a option in the MSI, there is bold text detailing this underneath the correct installation.
Restart and check SQLite in GAC was installed correctly by clicking the blue question mark above your connected databases underneath SQLite toolbox.
At this point you should see "SQLite EF6 DbProvider in GAC - Yes."
Elsewise, you most likely configured your machine.config manually in the past. Delete the changes you made underneath system.data > DbProviderFactories.
Restart VS and you should see "SQLite EF6 DbProvider in GAC - Yes."
Install System.Data.SQLite under the NuGet manager.
You can now connect to SQLite as a binding source and directly select your database file through "SQLite Database" vs "SQLite Provider (Simple for EF6 by ErikEJ)."
refer to this link, this solution was taken from a detailed github post, step by step

Get Sqlite from DbProviderFactories.GetFactory()

I need to have DbProviderFactories.GetFactory() return the provider for Sqlite. However, I have Sqlite added to my project via NuGet and want to avoid having to put it in the GAC and update machine.config. One of the advantages of Sqlite is it is there with no configuration.
However, I have a number of libraries that pull the connector from GetFactory().
Is there a way to do this?
thanks - dave
You just have to add an app.config file to your project, with the invariant that you want to add.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"></remove>
<add name="ADO.NET Provider for SQLite"
invariant="System.Data.SQLite"
description="ADO.NET Provider for SQLite "
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite">
</add>
</DbProviderFactories>
</system.data>
</configuration>
The type attribute contains the namespace of the provider factory and the namespace of the provider itself.
The GetFactory method first looks in your local app.config file, then in your machine.config. Just make sure that you do have a reference to the SQLite assembly, as you do.

WPF/EF/SQLite The server was not found or was not accessible

I have been pulling my hair for many hours on this. I'm new to C# et .NET so I apologize if this is something trivial.
I'm trying to use WPF with Entity framework + SQLite.
I have managed to connect to the SQLite database by manually doing:
var connection = ConfigurationManager.ConnectionStrings["AccountHelper.Properties.Settings.AccountHelperConnectionString"].ConnectionString;
m_dbConnection = new SQLiteConnection(connection);
executeReader();
I get the following error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 50 - Local Database Runtime error occurred.
The specified LocalDB instance does not exist.
I really have no clue on how to fix this. It is really frustrating!
EDIT: I answered this with a tutorial to configure everything properly.
So to answer my direct question above, the issue was that TextContext didn't know about the connection string. So if you give the same name to your Context as your string value, it will remove this error message.
That said, the configuration was completely wrong. I managed to make it working. Because I struggle a lot, I will share what I did to have it working.
Step to step guide to configure it
Configuration of a WPF project to work with Entity Framework (ADO.NET) and SQLite
Configuring this can be really tricky, even more if you have tried other methods before. So I advise to completely uninstall any SQLite software you might have installed.
1) Depending on your visual studio version, install sqlite-netFx45-setup-bundle-x86-2012-1.0.93.0.exe or equivalent. You can find it on sqlite.org. Few points:
I know for a fact that the 1.0.98.0 and 1.0.99.0 won't work with visual studio 2012. It works but you get an issue when creating your ADO.NET item in the project.
On this webpage, you can't find the old version, you will have to manually modify the URL to get the version you want.
On this webpage, make sure to select a version that has "This is the only setup package that is capable of installing the design-time components for Visual Studio 2013. " visual studio 2013 or not.
Install the exe and make sure to tick the designer related component during the install.
2) Create a new WPF project. Again, if you use a project you already modified, you might have some config issues. You better start a new project, do the config and once you know how to do it, port it to your project.
3) Install the Entity framework via the nugget manager.
4) Add manually (NOT WITH THE NUGGET MANAGER !!!) the following references that you can find in Assemblies -> Extension:
System.Data.SQLite Core
System.Data.SQLite Designer
System.Data.SQLite for Entity Framework
System.Data.SQLite for LINQ
5) Add a new item to this project -> Data -> ADO.NET blabla. Name it as you wish and create. You will need to create a connection for it. This is where if you don't have a compatible SQLite version, you won't be able to create a SQLite connection.
6) Then make sure your App.config look like the following, you might need to change the provider:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="AccountHelperEntities" connectionString="metadata=res://*/AccountHelperModel.csdl|res://*/AccountHelperModel.ssdl|res://*/AccountHelperModel.msl;provider=System.Data.SQLite;provider connection string='data source="W:\visual studio projects\AccountHelper\AccountHelper\Data\AccountHelper.sqlite3"'" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
7) You are good to go.
You can see the project source code on this link. Check the code from the First commit, it contains everything you need. The other commits are project specific.

C# Could not find the data provider. NET Framework requested

Im using Visual Studio 2010 Ultimate, C# WPF, MVVM, Sqlite.
I have this project that is running without problems (On Windows 8 x64, .NET Framework 4 Client Proile), but I get all this exceptions when run an installed application (On Windows 7 x32, .NET Framework 4 Client Proile):
Exception: System.Windows.Markup.XamlParseException: 'The invocation of the constructor of type' GestorDocument.UI.DeterminanteView 'that matches the specified binding constraints threw an exception.' (Line number: '3 ', line position '9'). ---> System.ArgumentException: The specified store provider can not be found in the configuration or is not valid. ---> System.ArgumentException: Could not find the data provider. NET Framework requested. It may not be installed.
  
This is my connection string:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="GestorDocumentEntities" connectionString="metadata=res://*/GestorDocument.csdl|res://*/GestorDocument.ssdl|res://*/GestorDocument.msl;provider=System.Data.SQLite;provider connection string="data source=C:\SQLITE\BD\GestorDocument.s3db"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup></configuration>
Please, Any idea?
Now it works!
MY SOLUTION WAS:
App.config in your project:
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.85.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
</configuration>
Include a reference of: (version for the machine on which the application will be installed.)
SQLite.Designer.dll
System.Data.SQLite.dll
System.Data.SQLite.Linq.dll
Set "Copy local" property to True.
Add in machine.config this line between system.data and DbProviderFactories
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.85.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
Ensure that the version of. NET Framework, which was developed the application, is installed on the test machine.
The System.Data.SQLite provider isn't installed on the machine. The correct installation not only drops the binaries on the machine (probably the GAC), but also drops an entry into machine.config that looks something like this (this is NOT REAL):
<system.data>
<DbProviderFactories>
<add name="System.Data.SQLite"
invariant="System.Data.SQLite"
description="SQLite Framework Data Provider for SQLite databases lol"
type="SomeProviderFactory, System.Data.SQLite, Etcetera=etcetera"/>
<!--snip-->
</DbProviderFactories>
</system.data>
Configuration settings (like this database provider configuration) in machine.config are inherited by your application's derp.exe.config file. In your .config file, you are configuring EF to use the SQLite provider (from the middle of your EF connection string):
provider=System.Data.SQLite;
If you look up at the hypothetical entry, it has a provider name that's the same as in your connection string. That's how EF knows to use the SQLite factory to create a connection to the database. See, it's not really magic at all. Its just hidden.
So what's the solution? Hell if I know. I mean, I use a REAL compact database, not this SQLite stuff. You will either have to install SQLite on the target machine, add the factory definition to your .config file, or do something else, like construct a plane out of bamboo.

SQLite basic example

I am trying to create a database with SQLite with c# then create a table insert data then close the connection. I have just downloaded System.Data.SQLite.dll lybrary and I am not sure how to use it. There are a lot of examples in the internet but all of them seem to have a database already. Or maybe I am doing something wrong.
It will be nice If I can have a short example to just create a database, table and basic query.
EDIT
I have tried the examples provided by the comments but I don't understand why I do get errors. Maybe I downloaded the wrong library?
The error was because I was using .NET Framework 4.0 . I downgraded to 2.0 and it worked. Sorry for the question. It will be nice to use it with .NET Framework 4.0 though.
Edit:
It actually works with .NET Framework 4.0 I had to add this lines of code to my app.config file:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
also if you plan to use ado.net in your solution I experience a lot of problems when deploying. Everything worked great under development. If you use ado.net and you plan on deploying your app then include also:
<!--Sqlite configuration so that it works with ado.net-->
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
if you include that last part in your app.config file then you will have to make sure that:
those dll's have to be in your output directory.
if you deploy make sure that you copy those files to the working directory
shouldnt it be {"FailIfMissing", "False"} instead of {"FailIfMissing=False", "False"} ?
Please change the Platform Target for your C# project to Project Settings > Build > Platform Target: Any CPU.
I had a similar error occurred when i tried to run in a x64 machine with SQLite x64 binaries. But worked fine after these settings were changed

Categories