I'm looking for a solution to do a column mapping. The database is not mine, and I am just making some kind of additional software for an existing database where column naming is terrible. I want to handle full CRUD, but now I'm using DapperExtensions.Mapper, which is mapping only for update/insert, but data selection with QueryAsync is not mapped. I am getting zeros and nulls. I need to add [MyPropertyName] in the select query.
There are a lot of tables that contain a lot of columns:
Documents table has 262 columns
Payments have two tables, first with 90 columns, second with 87
Customers - 171 columns
Is there any way to map those faster? A solution that works both ways add/edit and select with one configuration.
I think you can try to use EF Core Db First to map column and handle CRUD.
Reverse Engineering:
Reverse engineering is the process of scaffolding entity type classes
and a DbContext class based on a database schema. It can be performed
using the Scaffold-DbContext command of the EF Core Package Manager
Console (PMC) tools or the dotnet ef dbcontext scaffold command of
the .NET Command-line Interface (CLI) tools.
More details your can refer to link1,link2
I have over 100 basic types in my project. Assume that all of them just contains an Id and a Title , so which of these approach is better to use :
Approach 1 : create a separate table for each of them
Approach 2 : Create a table for all of them and use another field as discriminator
I am using Using MSSQL server with Entity Framework Code-First Approach . Actually I can not decide which approach I should choose to use.
I think the question is self-briefed , but Let me know if you need more details.
UPDATE1 : Please do not refer me to this question . I have checked this one , wasnt that much helpful
UPDATE2 : Many of these tables have many relations to the other tables. but some of them wont use that much
100 types that inherits from Id/Title base type and EF TPH (so the DB will have 1 table with discriminator and programmers will have 100 types).
Approach1 will keep relation integrity and clean navigation properties form models.
Also your IDE will helps you completing rigth model names.
The tip: create and interface for all tables in order to reuse UI controls.
Edited
If you found a business name for this table, like customer_data then do a single table. If name is related with technology master_tables split into full semantical classes.
I am designing a WCF service that has several functions:
1. ProductDTO GetProduct ( Guid productId )
2. void SetProduct ( ProductDTO product )
3. List<ProductDTO> GetAllProducts()
4. void SetAllProductValues ( int newValue )
These are the operations the Service will support.
I will have a Business Layer (BL) and a Data-Access Layer (DAL).
I will be using LINQ-TO-EF as my ORM to connect to the SQL server tables.
My question is - what exactly should the DAL contain ?
I am asking this question because I have read 2 different books saying different things:
First Approach: the DAL contains only classes needed for data-access layer (if at all). It includes special entities or functions for that.
The EDMX and model file for the LINQ-TO-EF is in a seperate assembly (that is referenced by the BL and the Service Layer).
In this method - the BL contains the actual functions that perform the LINQ queries.
For example : 'GetProduct' will perform the LINQ query to extract data from the DB. So - in this approach - what exactly is supposed to be in the DAL ? Is it empty ?
Second Approach: the DAL contains functions for performing CRUD operations on the DB, meaning - any LINQ-TO-EF queries will be done in the DAL.
So - in this approach - what exactly does the BL do except for maybe some validations ?
Which approach is the correct one and how does that answer my concern of that approach ?
From MSDN:
"All code that is specific to the underlying data source – such as creating a connection to the database, issuing SELECT, INSERT, UPDATE, and DELETE commands, and so on – should be located in the DAL. The presentation layer should not contain any references to such data access code, but should instead make calls into the DAL for any and all data requests. Data Access Layers typically contain methods for accessing the underlying database data. The Northwind database, for example, has Products and Categories tables that record the products for sale and the categories to which they belong. In our DAL we will have methods like:
GetCategories(), which will return information about all of the categories
GetProducts(), which will return information about all of the products
GetProductsByCategoryID(categoryID), which will return all products that belong to a specified category
GetProductByProductID(productID), which will return information about a particular product"
Of course you are always free to choose your own design, but I like to stick with generally accepted practices so my code will likely be acceptable in any shop and understandable to anyone that has to maintain it later...
I'm beginning to wonder if I am in fact too dumb to use NHibernate. I'm currently using FluentNHibernate to create simple db mappings and that's worked well for us. When dealing w/ our various classes in isolation, we're able to read and write, perform updates, etc. to all of those classes. My problem however is in trying to build up a query that involves anything more complex than filtering upon fields of the entity type. A perfect example is illustrated below--
The pertinent portions of the mappings:
public class UserMap : ClassMap<User> {
Id(u => u.Id).Column("UserID").GeneratedBy.Identity();
//other non-pertinent fields
}
public class RoleMap : ClassMap<Role> {
Id(r => r.Id).Column("RoleId").GeneratedByIdentity();
//snip
}
public class RoleMapMap : ClassMap<RoleMap> {
Id(rm => rm.Id).Column("RoleMapId").GeneratedByIdentity();
Map(rm => rm.UserId);
Map(rm => rm.RoleId);
//snip
}
The intent is to generate a query w/ the Criteria API to retrieve all users of a specific role--at a high level, filter rolemap based on a specific role ID, then join to Users, and return only those users.
Attempted with following, but my usage of CreateAlias is obviously flawed, as the runtime exception basically tells me that it has no idea what "RoleMap" in the below is as it relates to the User object.
var criteria = session.CreateCriteria<User>().
CreateAlias("RoleMap", "rm").
Add(Expression.Eq("rm.UserId", "UserId")).
Add(Expression.Eq("rm.RoleId", 99)).
SetResultTransformer(new
DistinctRootEntityResultTransformer());
var users = criteria.List<User>();
Can someone point me in the right direction? I'd prefer not to edit the underlying objects to expose collections--(e.g. a User.Roles[] collection) as there's cases where we specifically have tables used solely for joins but we don't want floating to the middle tier. So learning how to join isolated classes is going to matter to us.
Your mapping contains no way to navigate from User to RoleMap, yet that is what you are trying to do in your Criteria API call. You have multiple options. Here are a couple:
1) Allow User to navigate to RoleMap in your mapping. This is the easiest and how it's normally done.
2) Use two queries, one to get a list of UserIds based on the RoleMap to Role relationship and then a second query to get all the Users for those UserIds.
You say you don't want a User.Roles collection in your middle tier, but NHibernate should exist in your data layer, not necessarily your business layer. You can allow NHibernate to know about User.Roles while effectively hiding it from your business layer.
Joining isolated classes isn't really what ORMs are built for. ORMs are built for joining related classes. Related classes are generally mapped to related tables at the database level. To join isolated classes, you are going to need to do things like option 2 above where you run multiple queries and/or work around the lack of a relationship in custom code.
This question already has answers here:
What is an ORM, how does it work, and how should I use one? [closed]
(5 answers)
Closed 5 years ago.
As the title says; what is a ORM framework and what is it useful for?
A simple answer is that you wrap your tables or stored procedures in classes in your programming language, so that instead of writing SQL statements to interact with your database, you use methods and properties of objects.
In other words, instead of something like this:
String sql = "SELECT ... FROM persons WHERE id = 10"
DbCommand cmd = new DbCommand(connection, sql);
Result res = cmd.Execute();
String name = res[0]["FIRST_NAME"];
you do something like this:
Person p = repository.GetPerson(10);
String name = p.FirstName;
or similar code (lots of variations here.) Some frameworks also put a lot of the code in as static methods on the classes themselves, which means you could do something like this instead:
Person p = Person.Get(10);
Some also implement complex query systems, so you could do this:
Person p = Person.Get(Person.Properties.Id == 10);
The framework is what makes this code possible.
Now, benefits. First of all, you hide the SQL away from your logic code. This has the benefit of allowing you to more easily support more database engines. For instance, MS SQL Server and Oracle has different names on typical functions, and different ways to do calculations with dates, so a query to "get me all persons edited the last 24 hours" might entail different SQL syntax just for those two database engines. This difference can be put away from your logic code.
Additionally, you can focus on writing the logic, instead of getting all the SQL right. The code will typically be more readable as well, since it doesn't contain all the "plumbing" necessary to talk to the database.
From wikipedia:
Object-relational mapping (ORM, O/RM,
and O/R mapping) in computer software
is a programming technique for
converting data between incompatible
type systems in relational databases
and object-oriented programming
languages. This creates, in effect, a
"virtual object database" that can be
used from within the programming
language. There are both free and
commercial packages available that
perform object-relational mapping,
although some programmers opt to
create their own ORM tools.
It's good for abstracting the datastore (flat file / SQL / whatever) out in order to provide an interface that can be used in your code. For example, (in rails) instead of constructing SQL to find the first user in a users table, we could do this:
User.first
Which would return us an instance of our user model, with the attributes of the first user in the users table.
Databases usually work on relational model: you have tables (simplifying: like a spreadsheet), and relations between them - one-to-one, one-to-many, many-to-many, etc, meaning for instance that one record in table A has many related records in table B. You can retrieve data from them as rows (collection of values representing rows from table/tables) More in wikipedia.
Modern programming languages use object model. Objects have methods, attributes (simple or complex) etc.
ORM software does a transition between those models. For instance, it puts all related records from table B into an attribute of object A. This kind of software makes it easier to use relational databases (most popular kind) with object programming languages.
Whenever you go with ORM (Object Relational Mapper) you will find DBAL (Database Abstraction Layer) side by side. So its necessary to know what these are, so as to get good insights of what you are using and whats the advantages that you will get.
DBAL (Database Abstraction Layer)
It acts as a layer between your code and database. Irrespective of whatever your database, the code written will work fine with minor tweaking.
Assume that for the current project your using MySQL once its a fully matured and gets huge traffic your team plan to switch the database to Oracle for some reason then the code you have written in MySQL must be rewritten to Oracle based queries. And rewriting the queries for the whole project is a tedious task.
Instead if you use any DBAL libraries then you can switch the configuration of the database and make sure your project will be up and running within a day (May be with some minor tweaking).
ORM (Object Relational Mapper)
Object Relational Mapping (ORM) is a technique (Design Pattern) of accessing a relational database from an object-oriented language.
If you have used any kind of frameworks like Symfony (if you come from PHP background)/Hibernate (Java), then your familiar with these. Its nothing but Entities.
In order to access the database in Object Oriented context and interface translating the object logic is necessary, this interface is called as ORM. Its make up of the object that give access to data and keep the business rules with themselves.
Eg.
class User{
private $email;
private $password;
public function setEmail($email){
$this->email = $email;
return $this;
}
public function getEmail(){
return $this->email;
}
public function setPassword($password){
$this->password = $password;
return $this;
}
public function getPassword(){
return $this->password;
}
}
/* To save User details you would do something like this */
$userObj = new User();
$userObj->setEmail('sanitizedEmail');
$userObj->setPassword('sanitizedPassword');
$userObj->save();
/* To fetch user details you would do something like this */
$userObj = new User();
$userDetails = $userObj->find($id);
Eg. Doctrine, Propel, RedBean
ORM is:
An abstraction and like any abstraction it makes life easier for you.
From Wikipedia:
http://en.wikipedia.org/wiki/Object-relational_mapping
Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language. There are both free and commercial packages available that perform object-relational mapping, although some programmers opt to create their own ORM tools.
Pros and cons
ORM often reduces the amount of code needed to be written, making the software more robust (the fewer the lines of code in a program, the fewer the errors contained within them).[1].
There are costs as well as benefits for using O/R mapping. For instance, some O/R mapping tools do not perform well during bulk deletions of data. Stored procedures may have better performance but are not portable.
It allows you to do things like this (this is Doctrine code):
$activeUsers = Doctrine::getTable('User')->createQuery('u')->addWhere('u.active = false');
foreach($activeUsers as $user)
{
$user->active = true;
$user->save();
}
Object-relational mapping (ORM) libraries provide this mapping of database tables to domain object classes.