Entity Framework Code First Permission/DB Creation Issues - sql-server-2008

I am attempting to create a db using EF 5 Code First on SQL Server 2008. I can create the db using Integrated Security, but I cannot create the db using a custom user. The code runs the initializer on my home machine for both integrated security and custom user.
The difference in the connection strings is as follows. In addition to these I changed Data Source to Server and Initial Catalog to Database per connectionstrings.com.
<!--<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MVCTesting;Integrated Security=False;User Id=Green;Password=******" providerName="System.Data.SqlClient" />-->
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MVCTesting;Integrated Security=True;" providerName="System.Data.SqlClient" />
I compared the SQL users MachineName/LoginName and Green. They both have server roles of public and sysadmin, neither have any user mappings till a db is created then they are mapped to that db.
In global.asax.cs I have
protected void Application_Start()
{
Database.SetInitializer(new MVCTestingInitializer());
//Database.SetInitializer(new DropCreateDatabaseAlways<MVCTestingContext>());
ViewDb();
//Other setup
}
//Init the db by looking up a contact
private void ViewDb()
{
using (var context = new MVCTestingContext())
{
var user = context.Contacts.Where(u => u.FirstName.StartsWith("T"));
var asdf = "";
}
}
And my initializer is ...
public class MVCTestingInitializer : DropCreateDatabaseAlways<MVCTestingContext>
{
protected override sealed void Seed(MVCTestingContext context)
{
//Code to init the db
}
}
Using NLog I am able to verify if the seed is run or not. When I have integrated security it runs, when I do not the seed does not run.
I have also created an account on the machine, added that account to the AppPool and SQL Server, then retested. I still see a failure to connect.
System.Data.SqlClient.SqlException: Login failed for user 'Green'.
In a mad fit I decided to add all possible permissions to the user 'Green', I still see the above error. Does anyone see what I am missing? I am sure it is so simple and I will smack myself in the head afterwards, but I just do not see it.
If I am really far off, please provide the steps to create a SQL Account/Permissions that I can use in my web config and please include app pool setup.
Thanks,
TJ

I will post it as answer :)
If you ever have problem with connecting to db, open ssms and use the same credentials to login to db. This will confirm you have right credentials ;)

Related

How to have multiple web configs in single application

I have my login page, and 2 separate modules other then my login page. After login, it sends you to the login page with the links unhidden. The links only become available after your role has been confirmed. My question is how do I link my 2 separate webconfigs to my separate modules. I was told I could do this straight on the webconfig file without adding the Page_Load event in C#. If that’s true can I get an example? Thank you!
Maybe you should adds two connection string declare in web.config. Because Web.Config not unalterable in runtime. Choose connection string as need.
<connectionStrings>
<add name="dbConnectionString1" connectionString=""/>
<add name="dbConnectionString2" connectionString=""/>
</connectionStrings>
Or get the connection string from different database. You create function to get connection string method. Function get connection string as need. Or return static connection string. For example:
public string GetConnectionString()
{
if ()
return "connectionstring1";
else
return "connectionstring2";
}

ASP.NET Web Application and MySQL with custom Identity Storage

I want to implement a new WebApp in an existing MySQL database, the problem appears when I try to do the migration or the connection because I have already an 'Users' table in there, so I want to create my own tables (webapp_users, webapp_roles, ...).
I tried several ways to do it but no one worked, Can you help me and tell me the steps to follow to do this from the 'New > Project...' step?
Should I install the MySQL packages from the NuGet Package Manager, use external tools/addons, ...?
I will appreciate that, I'm completely blocked.
Create a db in your MySql server with the required tables and follow these steps :
Import MySql.Web, MySql.Data and MySql.Data.Entity.EF6 dlls into your project first.
Now create a Entity framework data model from your database by right clicking any folder -> new item -> Data -> ADO.net entity data
model
Click Generate from database -> Next
Click on New Connection -> Change ->
Select MySql Database as datasource
Give your MySql server credentials and select database to generate the data model from database
It should work fine.
Edit :
You can also pass the name of the connection string (stored in the web.config) in your context to the base constructor of the IdentityDbContext
public class MyDbContext : IdentityDbContext<MyUser>
{
public MyDbContext()
: base("TheNameOfTheConnectionString")
{
}
}
Check this for more info

MySql pooled datasource in standalone JAVA app (no J2EE container, no JNDI, no TOMCAT etc.)

I've been reading dozens of topics here with no real enlightment: I'm running a standalone java app, on a Synology NAS with Java 8. I also installed MariaDB on the same NAS. So everything is local.
I am able to setup a datasource and get a connection, but I would like to be able to access it in any instance of any of my classes / threads for connection pooling. Everything seem to show that I would need JNDI. But I don't have a J2EE container and this seems overkill for my need. Idem for developping my own implementation of JNDI.
I've seen replies to similar questions where people suggest C3PO. But this is just another datasource implementation. I don't think it solves the standalone app issue with no way to access datasource from anywhere in the code :
How to retrieve DB connection using DataSource without JNDI?
Is there another way to share a datasource across java threads ?
Can I pass the Datasource instance as a parameter to each thread, so
they can get a connection when they need ?
Should I assign a given connection to each thread - also passed as a
parameter ? and in this case, never really close it properly ?
Or should I really install something like tomcat, jboss, jetty ? are
they equivalent ? Is there a super light J2EE container that could
provide JNDI ?
Thanks
Vincent
You could use the singleton pattern, like this for example:
public class DataSourceFactory {
private static DataSource instance = null;
private DataSourceFactory() { }
public static synchronized DataSource getDataSource(){
if(instance == null){
instance = // initialize your datasource
}
return instance;
}
}
Then any from any thread you can call DataSourceFactory.getDataSource() to get a reference to your DataSource object.

Login failed for user 'IIS APPPOOL\myApp' when using Entity Framework with MySQL

I'm an Entity Framework newbie who is trying to develop some simple add-on to Sitefinity CMS on top of MySql database.
The database is working perfectly with Sitefinity itself; however, when my own EF code trying to connect with it, I always got the error:
Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'IIS APPPOOL\myApp'.
The connection string I'm using is:
<add connectionString="Server=localhost;Uid=MyUserId;Pwd=MyPassword;Database=dev_MyDatabase;CharacterSet=utf8" providerName="System.Data.SqlClient" dbType="MySQL" name="MyDatabase" />
The user (id and password) is created in MySQL, thus, when trying putting it in IIS (Application Pools > Advanced Settings...), it says "The specified user name does not exist".
Any idea what I'm missing here?
Thanks,
Harry
I see that you are using Entity Framework.
What does your Context look like? If you have a constructor that inherits the base constructor of DbContext, you need to make sure that you pass along the correct name in the parameter. In your case, it should look like this:
public class MyContext : DbContext
{
public MyContext():
base("MyDatabase")
{
}
}

Mvc-Mini-Profiler v1.7 on EF 4.1 Code-First project doesn't profile SQL

I setup MiniProfiler.MVC3 - 1.7 package in my project yesterday. The Controller and view profiling is working fine, but the peice I'm really interested in is the SQL Profiling. I have not been able to get this to work. I'm using EF Code First with a SQL 2008 database.
I have followed all the suggestions in this post ..
mvcminiprofiler-on-ef-4-1-code-first-project-doesnt-profile-sql
In the miniprofiler.cs i have my SQL connection setup as...
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["CMDBMVC3"].ConnectionString);
My Web.config db connection is...
<add name="CMDBMVC3" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|CMDB_MVC3.mdf;Initial Catalog=CMDB_MVC3;Trusted_Connection=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
If I put a breakpoint on the mini-profiler line it points to the correct db connection. I'm not sure what else to do at this point. I would appreciate any direction on how to get the SQL profiling working.
I use EF Code first and the mini profiler within my Context constructor I create a new connection factory and pass this to the ProfiledDbConnectionFactory method this returns a profiled connection that you can then set as the DefaultConnectionFactory of the context.
public MyConext()
{
var factory = new ConnectionFactory();
var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
Database.DefaultConnectionFactory = profiled;
}
The connection Facotry just returns a new sql connection
public class ConnectionFactory :IDbConnectionFactory
{
public DbConnection CreateConnection()
{
var cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["SomeConnection"].ToString());
return cnn;
}
You also need to add the ProfiledDBProvider to the web config file. Make sure the version number is correct for you.
<system.data>
<DbProviderFactories>
<remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
<add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider"
description="MvcMiniProfiler.Data.ProfiledDbProvider"
type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
This works fine for me in both MVC and asp.net webforms using the Miniprofiler nuget package. I'd also check out the new MVC version of the nuget package that auto configs profiling as part of a global action filter.