How to use Enterprise librrary in MVC as a DAL - asp.net-mvc-3

Is there any fine tutoruial for MVC using Enterprise Library for data access?

Here is a very simple article from c-sharpcorner. The code from there
First of all you need to add a reference for "Microsoft.Practices.EnterpriseLibrary.Data.dll".
Then add a config section in the configuration file.
<configSections>
<sectionname="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
Here is the connection string:
<connectionStrings>
<addname="NorthWNDConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|NORTHWND.MDF;User Instance=true"
providerName="System.Data.SqlClient" />
<addname="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>

As requested by #Chris my comment on the question posted as an answer:
I don't know of any tutorial but you might want to search for 'the data access application block' which is part of entlib.

Related

Membership Roles in Azure and MVC

I'm developing an ASP.NET MVC3 website and I'm using the Microsoft ASP.NET Universal Providers.
Web.config :
<connectionStrings>
<add name="MyContext" connectionString="data source=tcp:***.database.windows.net,1433;Initial Catalog=***;User Id=***;Password=***;Trusted_Connection=False;Encrypt=True;Connection Timeout=30" providerName="System.Data.SqlClient" />
</connectionStrings>
...
<roleManager defaultProvider="DefaultRoleProvider" enabled="true">
<providers>
<clear />
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider" connectionStringName="MyContext" applicationName="/" />
</providers>
</roleManager>
The connectionStrings is ok in Azure.
Aspnet tables and storedprocedures I added by using the query script. Data in tables added by migrations. I checked it in management portal.
I can get profile membership and profile data, but the the code below returns empty list.
var userName = User.Identity.Name;
var userRoles = Roles.GetRolesForUser(userName).ToList();
userName is correct and table aspnet_UsersInRoles contains correct data too.
There is no exceptions on page.
Why the userRoles is empty?
I fix this problem by creating the custom provider, which is inherited from SqlRoleProvider.

ADO.NET EF - can't establish DB connection using VS2010

I'm following this tutorial : http://msdn.microsoft.com/en-us/data/jj193542 for Code First basics using ADO.NET EF. The problem is that when executing the code the DataBase is not created automatically and I think that this is because the configurations in my app.config but it's my first day with real code and I can't figure out how to adjust my config file so I can connect to my server and use a created DataBase if neccessary or let the program from the tutorial create new database as expected from what is written.
This is my App.config file:
<?xml version="1.0" encoding="utf-8"?>
<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=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<!-- <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory> -->
<connectionStrings>
<add name="BloggingContext"
providerName="System.Data.SqlClient"
connectionString="server=milka-pc\sqlserver2012;Database=Panorama;user id=MyID;password=MyPass;port=3333;Integrated Security=True;"/>
</connectionStrings>
<!--<contexts>
<context type=" Blogging.BloggingContext, MyAssembly">
<databaseInitializer type="Blogging.MyCustomBlogInitializer, MyAssembly" />
</context>
</contexts>-->
</entityFramework>
</configuration>
As you can see some parts are commented as I tried a various things to make it work.
This is how y server explorer looks like while I'm trying to connect:
Also since I have zero experience with ViasualStudioXXXX and connecting to DataBases this is what I see and how I select my server:
There in milka-pc\sqlserver2012 I have a DB named Panorama which I can use or better follow the tutorial step by step and leave the program to create my new DB. For now I can't neither of these two. And to connect to milka-pc\sqlserver2012 I need to provied UserName and Password which I did in App.config. I say this just to know that these fields are not blank
If you are sure that milka-pc\sqlserver2012 is in fact working on port 3333 as you suggested
change your connectionstring to this:
<add name="BloggingContext"
providerName="System.Data.SqlClient"
connectionString="server=milka-pc\sqlserver2012,3333;Database=Panorama;user id=MyID;password=MyPass;Integrated Security=True;"/>
Just to make sure try that connection string in you SQL management studio with provided credentials.
If that doesnt work consult your sql server configuration manager, specifically under SQL server Network configuration make sure that your instance has enabled TCP/IP pipe with appropriate port enabled.
Cheers.

Newbie in MVC3 connection setup

I'm a real newbie in MVC3 but excited to learn. Guide me how to change connection from App_Data (DB.mdf) to MS SQL Server 2008.
MDF connection
<add name="MvcMusicStoreEntities" connectionString="metadata=res://*/Models.MusicStore.csdl|res://*/Models.MusicStore.ssdl|res://*/Models.MusicStore.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
This is a working one from my running project:
<add name="CONNECTION_NAME" connectionString="data source=SQL_SERVER_IP\INSTANCE_NAME; User ID=SQL_USER_NAME; Password=YOUR_PASSWORDd;Initial catalog=DB_NAME" providerName="System.Data.SqlClient" />
for your posted one, it may look like this:
as I can see, you are not following the MVC3 version of music store, or that's how it look for me as I've checked the PDF file of the MVC3 vesion and found that Jon Galloway is using the connecion string just like the one I'm using.
check out the PDF from here: http://mvcmusicstore.codeplex.com/releases/view/59112#DownloadId=197609
That's it, no other parts are required.
this is also a very very nice resource for you to find the different ways that you can do to configure your app connection string.
http://www.connectionstrings.com/sql-server-2008
From SQL Server 2008 connection strings
Data Source=myServerAddress;
Initial Catalog=myDataBase;
User Id=myUsername;
Password=myPassword;
Example:
<add name="NAME" connectionString="Data Source=.\SQLExpress; User ID=username; Password=password;Initial catalog=database" providerName="System.Data.SqlClient" />
If you are going to use Entity Framework you need to change the provider to
providerName="System.Data.EntityClient"
And modify the add tag:
<add name="Entities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLExpress;Initial Catalog=Test;Persist Security Info=True;User ID=test;Password=test;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>

Where did aspnetdb.mdf come from in my ASP.NET MVC Application?

I'm new to ASP.NET and have just starting to learn ASP.NET MVC 3. I've started a new ASP.NET MVC 3 project using the default template, which already has most of the membership stuff already configured. Now I'm trying to understand what is really going on behind the scenes.
I've found that default template defines an IMembershipService interface which is implemented by the AccountMembershipService class which basically just wraps a System.Web.Security.MembershipProvider. From the comments in the code is sounds like this abstraction is done to facilitate unit testing.
The default MembershipProvider is set in the Web.config file. My project, which was created from the default template, it is set to the SqlMembershipProvider.
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
This references a connection string in the Web.config file, which references a SQL Express database "aspnetdb.mdf".
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
When I first created my project there was no aspnet.mdf file. But after running my app and registering a new user this file was automatically generated tables and all. What's going on here? What creates this file and specified the tables that should be created?
These are created by the SqlMembershipProvider class. If you'd like, you can customize your config file to use a different membership provider or to tell it what existing database to use.
Use this tool to add the appropriate tables, etc to your existing SQL Server installation: http://msdn.microsoft.com/en-us/library/ms229862(v=VS.100).aspx

How do I set minumum password requirements in Umbraco for the membership provider?

I am trying to set minimum password requirements for my membership provider in Umbraco. Currently my web.config membership section looks like this:
<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" />
<add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" />
</providers>
</membership>
However, when I login in through the website and change my password, it will let me use a password of any length and makeup. Is there something I'm missing?
Thanks!
Typically custom membership provider settings are passed as custom xml attributes on the tag itself. You can see one there already: "userIsOnlineTimeWindow." Fire up Reflector and have a dig around the Assembly containing that provider, or praise-jeebus, RTFM. :D
-Oisin

Resources