S#arp Architecture / NHibernate with multiple databases - nhibernate

I'm using S#arp Architecture (which uses NHibernate). I have some entities mapped to tables in one database and others mapped to a different database. Disclosure: Databases already exist so i can't do model first.
How do I configure this to work?
EDIT: Would the SchemaIs method in Fluent NHibernate be the recommended approach to map an entity to a table in a different database? I believe this is possible via NHib's xmp mapping files too.

You should use NHibernateSession.AddConfiguration instead for additional database. The call to NHibernateSession.AddConfiguration goes immediately under NHibernateSession.Init(). An explicit session factory key will have to be defined for the second initialization.
The whole process is explained here in detail.
https://github.com/sharparchitecture/sharp-architecture/wiki?Page=FAQ

The way I have done this is to initialise multiple NHibernateSessions in InitializeNHibernateSession within global.asax.cs using multiple nhibernate config files. I then used [Transaction("nhibernate.dbname")] (dbname being names assigned to WebSessionStorages) in the controllers against each appropriate action method.

Related

How should I modify database model in Entity Framework?

EF beginner here.
How am I supposed to make changes in database model using Entity Framework?
I mean in DB model like changing datatypes of columns, adding attributes etc.?
E.g. I have string Password property in User table and I want to add [DataType(DataType.Password)] Attribute or [Required] or anything.
How am I supposed to do that? Of course along with applying changes to my DB? I created DB model from mdf local file (detached from mssql studio) using 'EF Designer from database' so I have my emdx model inside Models (asp.net mvc5) with classes for each table and DB MDF in App_Data.
Am I suppose to modify these classes?
Because I can add attributes right there but Diagram doesn't change and DB doesn't change. I guess I have to commit changes somehow.
I'll add that I can't enable migrations:
Creating a DbModelBuilder or writing the EDMX from a DbContext created using Database First or Model First is not supported.
EDMX can only be obtained from a Code First DbContext created without using an existing DbCompiledModel.
I think you are mixing allot of things here.
If you have an EDMX file, then your models are generated at compile time (or you can generate them from right click on the Model.tt file -> Run Custom Tool). So adding attributes to properties in a class representing a model entity will indeed be overwritten the next time you compile. The solution is:
Create another partial class to the generated classes
In the partial class, decorate the class with the [MetadataType] attribute and give it a type of a metadata class. The metadata class is a simple class, with the same properties as the generated class, but a different name, to prevent naming conflicts. From a design point of view, it should be abstract, because you're not supposed to create instances of it, but this is not required.
In the metadata class, decorate the matching properties with the validation and DataType attributes.
To the best of my knowledge, using model-first or database-first doesn't support migrations as in code-first. If you want to make changes to your schema (semi) automatically, I believe your best option is:
Make changes to your model in the EDMX designer
Right-click on the EDMX design surface -> Generate Database from Model.
After selecting the connection to your database, this will generate the SQL to generate your schema. This is a bit clunky, because it will erase your data each time, so you should have a script in place to re-populate your database after each time.

jhipster entity - what exactly is that?

I am wondering what jhipster entities are exactly?
What if I just want to create a new page/view without any database entry and relationship? For example: the about page, do I have to use the entity generator in order to create it? What are the benefits of it?
jhipster entity tutorial
Regards
redyar
Entities are typically objects that are backed by a database table. With JHipster, you can generate entities (and their associated screens) using Yeoman.
For an about page, or any other page, you can author them by hand. You don't need to use a generator. JHipster merely provides its entity sub-generator as a convenience for you.
You can also use the Yeoman generator for AngularJS to generate basic scaffolding.
http://yeoman.io
Immagine to build a website of e-commerce. That website contains users, sensible data informations, details about sellers and other things. You can save a lot of time creating it with Jhipster because it automatically create Objects class that represents database's tables, RESTful web services to manage them and the frontend view with Angular, CSS and HTML (all generated automatically). The objects that represent database's tables are entities.
With entities you can do CRUD queries on database using Java objects and Java methods with Spring framework. You can do queries by calling java methods. Thats all! After some practice it's really easy to use.
With that generator you don't have to configure relationships, constraints or things like that. Jhipster does that for you!
If you need a web application that does not use a database you don't need to use a generator like this, in my opinion.
I took the right Matt Raible's answer trying to give you more informations about entities. I hope to be helpful!
Have a nice day!

Multiple database with NHibernate 3.x

I found a couple of articles how to use NHibernate with multiple database, for example this one
http://codebetter.com/karlseguin/2009/03/30/using-nhibernate-with-multiple-databases/
But all articles are very old, and may be there is some new approach with NH 3.x? I looked in documentation but did not found anything, but maybe i missed somthing?
Does anybody knows some better way (native NH3.x way) to use NH 3.x with multiple database than described in this article?
http://codebetter.com/karlseguin/2009/03/30/using-nhibernate-with-multiple-databases/
Thanks,
Alexander.
AFAIK, there is nothing new in NH 3. But there are still more options to use several databases than in the blog post you linked.
You can open your own connection and pass it to NH when opening a session.
You can open a session and switch to another database on the same server (eg. by executing a use database statement on sql server).
You can provide a schema (database) name on each table you map in the mapping file. It is not useful to have it hard coded, but you can still replace it after loading the mapping files or use mapping by code.
The articles you linked are still the way to go. Each SessionFactory is responsible for a single connection (connectionstring) and schema.
There is one special case where ou split the database into multiple with the same schema to load balance. This is called sharding and there is the contrib NHibernate.Shards to deal with it.

Custom NHibernate entity persister for modifying generated SQL statements

What I need is to populate entity from DB view (non-insertable) and make all entity updates to updatable DB table.
Mapping entity to table and writing custom load SQL from view is not an option since in some cases NHibernate still tries to select from table name (when joining this entity, for example).
Mapping entity to view and writing custom data modification queries is not an option since I can not write cross-database sql-insert statement (because of the last inserted identity value selection part).
The only idea I came up with for now is to modify generated SQL statements on-the-fly. I managed to do it with custom interceptor but I don't think that its a good idea (since I intercept every single query, even for other entities). However, I think that it should be possible to change only needed queries using custom IEntityPersister. I created one based on SingleTableEntityPersister, specified it in <class persister="…">, but NHibernate doesn't even want to instantiate it.
Are there any examples of writing custom entity persisters for NHibernate?

Entity Framework and consuming a WCF service

I'm getting data where the database is hidden behind a WCF service.
Is it possible to use Entity Framework in a scenario where I have custom objects coming from a web service?
(No access to the external database, and no current plans for insert/update/delete logic)
Starting with an empty EF model and adding an entity I get this error on compile:
No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer ..
Is it possible to make an entity this way, and fill it with data received from an object?
(In this case a WCF, but could also be a predefined model class/xml data)
If the web service retured a Customer object I could do something like this with a dataset:
Make an unbound table and do a loop through the customer properties adding them to a temp row, add it with tbl_Customer.Addtbl_CustomerRow(customerRow) to get my view filled.
thanks, nakori
Entities are object representation of your DB entries (see Object-Relationnal Mapping; ORMs). Given Employee and SalesOrder, two hypothetical tables in a DB :
Entity: entities are instances of Entity Types (e.g. Employee, SalesOrder), which are richly structured records with a key. Entities are grouped in Entity-Sets.
Taken from the Modeling Data at the Conceptual Level of Abstraction: The Entity Data Model section of The ADO.NET Entity Framework Overview. Perhaps it is also a good read to start using the EF.
As for comm through WCF, it is kindof supported, such that entities are fully serializable/deserializable. You may also want to know that you can generate entities from an existing DB, theres a wizard and everything.