Fluent NHibernate / NHibernate via NuGet for SQL2012 - asp.net-mvc-3

I've just installed the following latest packages from NuGet:
FluentNHibernate.1.3.0.733
NHibernate.3.3.2.4000 (was required for FNH)
I'm trying to get our ASP.NET MVC 3 app running using SQL 2012 (native).
The following code is taken from our method which builds our nhibernate Session:
var sqlConfig = MsSqlConfiguration.MsSql2005.ConnectionString(ConnectionString).AdoNetBatchSize(30);
We also use this approach for provisioning a new emtpy DB using the code first approach.
I need to use the SQL 2012 dialiect but neither the latest FNH or NHibernate have it from NuGet.
Does anyone kow the status of the code in Git for these? I can see a merge for FNH for this 2012 dialiect was done 7 months ago (a month after the latest NuGet was released).
What are others doing to connect to SQL 2012?
Does anyone know when the next NuGet packagaes for these will be available?

Just use the SQL Server 2008 dialect. I don't know of any changes in 2012 that would require an update to the dialect.

It's already supported by the NHibernate core, so it's just a matter of using MsSql2012Dialect:
var sqlConfig = MsSqlConfiguration.MsSql2005.Dialect<MsSql2012Dialect>();
MsSqlConfiguration.MsSql2012 was added to Fluent shortly after the latest release, so you can grab the source if you want, but the end result is the same.

My current setup is using NHibernate (3.3.2.4000) and FluentNHibernate (1.3.0.733) and the first part of my connection setup looks like this, feel free to adapt it to your needs. Note that even though I pull the connection string using the MsSql2008 configuration class, I have set my dialect to MsSql2012Dialect
var cfg = Fluent.NHibernate.Cfg.Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromConnectionStringWithKey("KeyName"))
#if DEBUG
.ShowSql()
.FormatSql()
#endif
.Dialect<NHibernate.Dialect.MsSql2012Dialect>()
.AdoNetBatchSize(50)
)
//Other chained methods (for setting up cache, mappings, etc)
//.Mappings(m =>
//{
//
//}
var sessionFactory = cfg.BuildSessionFactory();
from what I can see the 2012 Dialect just adds support for Sequences and registers iif as a function.

Related

Visual studio xsd set connection string to None

I want to set connection string for my DataSet to None, because i use string that i build in run time. So i do not want keep fake string in app.config, just tome make xsd compile. Is there any way to achieve this?
If i set None in designer its still turn it to existing connection string from config.
I use visual studio 2017.
The best way I've found to have the benefits of the XSD designer and be able to change the connection settings for staging and production is as follows: - NOTE: this works in VS 2012 ... think it should also in 2010 - Also, DataSets were created by dragging from the Server Explorer, which saves connection information in the class library app.config and Properties.Settings.settings which is used at design time.
1) For each DataTable, click on the TableAdapter header (below the attributes and above the methods) and look at the properties 2) Change the Connection Modifier to Public 3) Then, the code to access the table should be something like the following (where the class library is named DALib) ...
using DALib;
using System.Web.Configuration;
PriceData.averageSalePriceFDataTable priceTable;
DALib.PriceDataTableAdapters.averageSalePriceFTableAdapter priceAdapter
= new DALib.PriceDataTableAdapters.averageSalePriceFTableAdapter();
priceAdapter.Connection.ConnectionString
= WebConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
priceTable = priceAdapter.GetData( ... parameters ...);
This uses the connection string in the class library for design and picks it from Web.config when running.
Hope I understood your problem correctly, let me know.

Can Entity Framework Core be configured to generate migrations during project build

I am trying to prepare a new project for continuous integration.
My project is written in .Net Core and uses Entity Framework Core for Database access.
What I need to try and do is add a pre-build step in my project to add a new migration.
I have already setup the webapp to apply all migrations on startup, but did not really want to have to manually create migrations before committing my code. I believe that should be part of the CI setup as I may go through hundreds of database changes during development, but when I deploy my latest code I would like to have a migration script for each version released.
I am currently using v1.1.0 of all nuget dependancies for .Net Core and EF Core.
EDIT:
The link from J.Pichardo is similar to what I am asking. But from what I can tell it is about applying migrations (which I have already got working in my code).
Answer from bricelam may work as a code version of what I am trying to accomplish. But, I was hoping there was a way to configure the project.json file so that it would run the Add-Migration script using a generated name for the migration (ideally by using environment variables which can be set by my CI).
This would then allow me to manually create migrations during development, but then let my CI create complete migrations during the final stage of each release process.
You can programmatically generate migrations, but I wouldn't recommend it. To do it, reference the Microsoft.EntityFrameworkCore.Design package and use code similar to the following.
using (var context = new MyDbContext())
{
var services = ((IInfrastructure<IServiceProvider>)context).Instance;
var codeHelper = new CSharpHelper();
var scaffolder = ActivatorUtilities.CreateInstance<MigrationsScaffolder>(
services,
new CSharpMigrationsGenerator(
codeHelper,
new CSharpMigrationOperationGenerator(codeHelper),
new CSharpSnapshotGenerator(codeHelper)));
var migration = scaffolder.ScaffoldMigration(
"MyMigration",
"MyApp.Data");
File.WriteAllText(
migration.MigrationId + migration.FileExtension,
migration.MigrationCode);
File.WriteAllText(
migration.MigrationId + ".Designer" + migration.FileExtension,
migration.MetadataCode);
File.WriteAllText(migration.SnapshotName + migration.FileExtension,
migration.SnapshotCode);
}

MiniProfiler and SqlMembershipProvider

I am trying to get any DB queries that happen when using the SqlMembershipProvider to show in MiniProfiler but I can't think of any way to swap out the SqlConnection is uses with a ProfiledDbConnection as it seems to do everything internally. Anyone have any ingenious ideas?
You could download the source (freely avaliable) for the ASP.Net SQL Providers here, read more about it here, Scott Gu published tons of whitepapers in this article about the provider model.
internal to the assembly is a SQLConnectionHelper that you can update to use the ProfiledDBConnection instead. This would give profiling to all the providers using that class, all thats left is to change your .config file to use the new provider instead.

InvalidCastException using ODP 9 with Fluent NHibernate

We're getting started with plugging in (Fluent) NHibernate 2.1.2 onto a legacy ASP.NET application.
Previously we've been connecting to an Oracle 9 database using System.Data.OracleClient, but because of some requirements (namely with using Oracle SPs with HBM maps), it seems that at least for the NH component, we'll have to adapt the Oracle.DataAccess.dll instead.
For the most part, we've managed to get that bit working by doing the following:
Adding and referencing the 9.2.0.700 Oracle.DataAccess.dll into the data layer.
Qualifying the Oracle.DataAccess assembly via the web.config. [via]
Adding OraOps9.dll into the application, and setting it to Embedded Resource.
The last step, we've done because our version of the ODP references it, but we have limited capacity to make sure that it will be present on the production server eventually.
This eventually led to getting an InvalidCastException:
Unable to cast object of type
'Oracle.DataAccess.Client.OracleConnection'
to type
'System.Data.Common.DbConnection'
Sifting around for a solution, the status quo seemed to be to ensure that the hbm2dll.keywords property in the NH configuration was set to none. I've done that in both a dynamically created config, and the fluent call to create a SessionFactory.
NHibernate.Cfg.Configuration cfg =
new NHibernate.Cfg.Configuration()
.SetProperty("dialect", "NHibernate.Dialect.Oracle9iDialect")
.SetProperty("connection.driver_class", "NHibernate.Driver.OracleDataClientDriver")
.SetProperty("connection.connection_string",conn)
.SetProperty("connection.provider","NHibernate.Connection.DriverConnectionProvider")
.SetProperty("hbm2dll.keywords","none")
;
_sessionFactory = Fluently.Configure(cfg)
.Database ( OracleDataClientConfiguration.Oracle9.ConnectionString(conn) )
.Mappings ( x => x.FluentMappings.AddFromAssemblyOf<MyDL>() )
.Mappings ( x => x.HbmMappings.AddFromAssemblyOf<MyEntity>() )
.ExposeConfiguration(x => x.Properties.Add("hbm2dll.keywords", "none"))
.BuildSessionFactory()
;
But even so, I still get the same InvalidCastException.
Is there anything else that could be causing this? Are any of my calls above accidentally tripping the hbm2dll.keywords property value back to something else? Any ideas?
ODP 9 is WAY too old and IIRC it's compiled against .NET 1.1.
You need to use ODP 10 at least.

Oracle Stored Procedures in SubSonic 2.2

I'm still not getting my SP's generated in SubSonic 2.2 against an Oracle 10g DB. The Tables and Views generate up perfectly. Also this product is multiplatform so we're gen'ing up SubSonic libraries against SQL 2K5 and that works great for Tables/Views and SP's.
I recall on the old forums there was a bug in earlier versions of the Provider, so I'm not sure if this is still the same issue, or if I'm doing something wrong on my end? Also SubStage (the UI) chokes when invoking the provider on Oracle (no issues with SQL). So I'm figuring there's something in the SPs that are choking it [?]
Thanks! Real close to migrating to SS from our current home grown DAL.
I was able to fix a lot of the issues with the Oracle Provider in SubSonic in version 2.1 and most all of my fixes made it into 2.2. I didn't work on fixing the SP generation portion of the Oracle Provider as I only had one or two SPs. Even if the SP generation isn't working you can still use SPs with the Oracle Provider. I simply added the SPs by hand (see below) as a partial class in the altered folder I use to add functionality to the classes SubSonic generates.
example
public partial class SPs
{
public static decimal CreateSp(string username, string labelNote)
{
Decimal returnId = 0;
SubSonic.StoredProcedure sp = new StoredProcedure("User.MySP");
sp.Command.Parameters.Add("username", username, DbType.String);
sp.Command.Parameters.Add("labelnote", labelNote, DbType.String);
sp.Command.Parameters.Add("returnId", returnId, DbType.Decimal, ParameterDirection.Output);
sp.Execute();
return Convert.ToDecimal(sp.Command.Parameters.Find(delegate(QueryParameter qp) { return qp.ParameterName == "returnId"; }).ParameterValue);
}
}

Resources