Can you map column name in SqlFu? - dapper

Everything else about this micro ORM looks great, except I can't figure out how to map column name, like
[Column("db_username")]
public string UserName {get;set;}
Did I miss something? Thanks

You can't do that, SqlFu is a data mapper, mapping a query result to a poco. The attributes used to decorate a Poco are for table creation only and not for querying. So, there's no mapping in an ORM sense.

Related

EntityFramework code-first ignoring derived columns when creating database

I would like some of tables in my database to have standard columns such as createdBy, ModifiedBy, CreatedDateTime, modifiedDateTime etc.
So, I created an interface with those properties and implemented the interface in an abstract base class. I derived my concrete classes from this base class.
This is a brand new application using Code-First approach. When I create the database, the derived properties are ignored. The tables are created with just the properties in the derived classes.
I'm not sure why.
Thank you.
I'm not sure exactly what was different, but tried it again with the following changes and it worked:
gave dbCreator rights in the database for the user
Changed the properties in my base class to be in the format (similar change for the other properties):
public string CreatedBy { get; set;}

Many-to-Many relationship in Zend 2 Framework

I use the Zend 2 Framework to build my web application. I implemented my database table models by this tutorial: http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
I have a many-to-many relationship between two models in my database. To get data from them I googled and found this link: http://mattmccormick.ca/2010/04/24/how-to-easily-create-models-and-table-relationships-in-zend-framework/
The problem is that all the table models extends from Zend_Db_Table_Abstract in the example. I don't know how to get data from the models.
I have a table containing votings, every voting has a unique hash id. Every voting also has tags. Therefore I defined a table tags with all the tags available and a voting_tag_map where all many-to-many relationships are mapped.
What I have tried so far is the following, that's code from my VotingTable class:
public function getTagsByVoting($votingHash){
$select = $this->tableGateway->getSql()->select();
$select->from(array('v' => 'voting'))
->join('voting_tag_map', 'v.voting_id=voting_tag_map.voting_id')
->join('tags', 'voting_tag_map.tag_id=tags.tag_id');
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
It says then:
Since this object was created with a table and/or schema in the constructor, it is read only.
Thats because of the from() method. If I delete the from() method, it says:
Statement could not be executed
Can anyone help me please?
Since this object was created with a table and/or schema in the constructor, it is read only.
This error is because you are trying to set the table name in the from clause, but it's already been set in the contructor of the TableGateway, and you can't change it once set.
If you really need to do this then you can extens AbstractTableGateway yourself then you won't have to add a string tablename to the contructor, but you don't really need to use an alias on your main table...
The SQL error you get when you comment out the from() method will be due to your referencing the votes table as it's alias 'v' in your join, when you are not using the alias v, try changing it to 'voting.XXX' from 'v.XXX'

Entity Framework Code First: How to map SQL Server's computed columns into CF model?

Is it possible to map computed columns into Code First model?
I'd like to define computed columns with SQL Server and then map them as properties inside my CF model.
Thanks in advance
Best Regards
Use the DatabaseGenerated attribute with DatabaseGeneratedOption.Computed as the value
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Foo { get; set; }
Not sure how you would do computed columns without using a view, and you cannot use views in EF Code First (you can in Model & DB first though). EF will execute some operations in SQL for you, instead of in code, but it doesn't sound like that is what you're looking for. Can you elaborate on what you are trying to achieve?
I think for EFCF beginners (like me!), there is no need for creating Database Initializers, this just work:
Model1 model = new Model1();//Model1 : DbContext
model.Database.CreateIfNotExists();
model.Database.ExecuteSqlCommand("alter table Results drop column Total; alter table Results add Total AS (Arabic + English + Math + Science)");
for the complete answer check here.

Object Relational Mapping between a Database and the Domain model

What rules should i follow in order map efficiently entities from my domain model to a database table?
Should database columns be aligned with domain class properties?
For example i have a Database table
Person {(varchar)FirstName, (varchar)LastName, int(age)}
And a Domain class
class Person(){
String firstName;
String lastName;
int age;
}
Should the class and the table
contain the same number of properties
Should they be of the same corresponding
type (e.g : varchar - string)
What are the best practices
What to avoid?
There are many strategies for object-relational mapping. Ultimately you have to represent every piece of persistent data in a field somewhere, so your strategy will work. Other questions you'll have to answer:
1) How are your records keyed? Are you going to have a unique ID field for each class?
2) How will you handle inheritance? If you have a PersonA(Person) class that introduces an additional field, will you create a whole new table for every field in Person + PersonA, or just have a PersonA table for the additional field?
3) How are you planning to construct queries against the database? Are you going to provide some form of object-query mapping?
There are many open source ORMs out there, so unless you're doing this for a research project, I'd advise that you use one of them.

Need advice on structure of my database, to create useful Entities

I need people's advice as to whether this the best way to achieve what I want. Apologies in advance if this is a little to subjective.
I want to use Entity Framework V.1 to create something similar to the following C# classes:
abstract class User
{
public int UserId;
public string TelephoneNumber;
}
class Teacher : User
{
public string FavorateNewspaper;
}
class Pupil : User
{
public string FavorateCartoon;
}
I need people's advice as to how to best to persist this information.
I plan to use SQL Server and the normal Membership Provider. It will create for me a table called aspnet_Users. There will be two roles: Teacher and Pupil.
I will add fields to the table aspnet_Users which are common to both roles. Then create tbl_Teachers and tbl_Pupils to hold information specific to one role.
So My database will look a bit like this:
aspnet_Users
int UserId
varchar TelephoneNumber
tbl_Teachers
int UserId
varchar FavorateNewspaper
tbl_Pupils
int UserId
varchar FavorateCartoon
The idea of course being that I can match up the data in aspnet_Users to that in either tbl_Teachers or tbl_Pupils by joining on UserId.
So to summarise, my questions are:
Is my database structure the best option to achieve these classes?
Should I try to wrap the Entities within my own POCO classes?
Should I change my database structure so that EF creates entities which are closer to the classes I want?
EDIT: I re-arranged my question it make it a bit clearer what I'm asking.
If you're using EF 1, then POCO can be a bit unpleasant. Unless there's a good reason not to, I'd just use normal EF entities. Your database model is fine, by the way, and is an example of TPT (Table Per Type) inheritance mapping. You could either use the wizard to create entites from the databaes, or create your entites and map them to the associated tables. If you do the former you'd initially end up with three unrelated entities. You'd then use the designer to tell EF that Pupil and Teacher inherit from User, and that User is abstract.
In general, one of the strengths of EF is that the entities don't have to match that closely to the tables that persist them. In this case though there's a natural mapping.

Resources