I have two tables called Users and Roles , and a bridge table to form many to many relation between users and roles.
My Question is that how can i create mapping for many to many relation in fluent nHibernate.
table User :
UserID
UserName
Password
FullName
Table Roles:
RoleID
RoleName
Description
Table Bridge:
UserID
RoleID
I have mapping tblUser like this
class tblUsersMap : ClassMap<tblUsers>
{
public tblUsersMap()
{
Id(user => user.UserID).GeneratedBy.Identity();
Map(user => user.UserName).Not.Nullable();
Map(user => user.Password).Not.Nullable();
Map(user => user.FullName).Not.Nullable();
}
}
and same way for mapping for Role table , but how can i define many to many mapping there?
Thanks
see here
Related
I am using laravel 5.2 to develop an application. Now what I need is I have two tables first one is users and second one is groups. Groups table is the parent table of the users table where groups_id is the column with foreign key with groups table (group_id). Now when login I am using the code as follows:
$users = User::leftJoin('groups', 'groups.group_id', '=', 'users.groups_id')->where('email', base64_decode(base64_decode(trim($request->input('user_login')))))->first();
where i used join to join the groups table. But It is executing perfectly but does not show the data in the session (Auth::user()).
So please suggest me how to add the second table in the session but only join the row with foreign key records.
Thanks.
You have define a relation in User Model like this
class User extends Authenticatable
{
......
/*
* Relation to Group Model
*
*/
public function group()
{
return $this->belongsTo('App\Models\Group','group_id','group_id');
}
......
}
Now you can access authored user group at anytime , anywhere by these line of code
auth()->user()->group()
or
$user = User::find($id);
$user->group();
and to retrieve users from a group you can define reverse of this relation in Group Model like this
class Group extends Mdoels
{
..........
public function Users()
{
return $this->hasMany('App\Models\Users','group_id','group_id');
}
..........
}
you can find more information on docs here
I'm using code first to create my DB but have to match a given schema.
I have an Org entity (simplified here) an Org can have many children of type org
public class Org
{
public int Id {get;set;}
public virtual IList<Org> Children{get;set;}
}
I want to generate an Org table and a table called OrgRelationship which has two columns ParentId and ChildId. The data is being provided to us in that format but I'd really like EF to expand the table into this model...
Is it possible to generate this join table just with an EntityTypeConfiguration on model builder? Do I have to have an OrgRelationship class?
This doesn't seem to cut it
public class OrgMap : EntityTypeConfiguration<Org>
{
public OrgMap()
{
HasKey(n => n.Id);
HasMany(n => n.Children);
}
}
You can configure the join table using the following calls.
HasMany(n => n.Children).WithMany().Map(
m => m.ToTable("OrgRelationship").MapLeftKey("ParentId").MapRightKey("ChildId"));
Given the example at http://notherdev.blogspot.com/2012/01/mapping-by-code-inheritance.html
I have a base class Party and two concrete classes (Company, Person)
I would like to use Table Per Hierachy (Single Table), but my ids are only unique per concrete type.
i.e. Company and Person may have matching Id's
Is there any way to include the discriminator value in the Id as a composite id?
But still be able to call Get<>(id)?
How about this (Fluent):
public class PartyMap : ClassMap<Party>
{
public PartyMap()
{
Table("Parties");
CompositeId<CompositeIdType>(x => x.Id)
.KeyProperty(x => x.IdCompositePart)
.KeyProperty(x => x.Discriminator);
DiscriminateSubClassesOnColumn("Discriminator");
}
}
I have 3 tables
task (Id,text,Contact_Id)
users(Id,name)
company(id,name)
and 2 junction tables
task_users (task_id,user_id)
task_companies (task_id,company_id)
note: contact_id may be refer to users table or company
How can I get task id, task text, contact name in one Criteria
This's example with QueryOver, but it almost like ICriteria.
Contact contact = null;
mappingSession.QueryOver<Task>()
.JoinAlias(() => task.Contact, () => contact)
.SelectList(list => list
.SelectGroup(task => task.Id)
.Select(task => task.Text)
.Select(() => contact.Name))
.TransformUsing(Transformers.DistinctRootEntity)
.List();
Other is a mapping. You can make base entity "Contact" and extend from it User and Company.
I have a database that has a one-to-one relationship modeled between a Person and a Address (that uses person id). However, I cannot find a way to make the map using NHibernate.
My table structure is the following:
PersonTable
PersonId
PersonName
PersonAge
AddressTable
PersonId
CountryName
StreetName
StateName
And I would like to have something like this as the final class:
PersonClass
int Id
string Name
int Age
Address HomeAddress
AddressClass
string Street
string Country
string State
Person Owner
I tried with HasOne relationship but I couldn´t reuse PersonId as Address Identifier.
Thanks!
Edit: I forgot to mention that I´m using FluentNHibernate so both fluent mapping and XML will be fine.
The problem is that your database schema does not represent a "Has One" relationship between Person and Address. It represents a "Has Many" relationship; You may be artificially limiting it to be one address per person, but that doesn't change the fact that the model is multiple addresses per person.
To get a "Has One" relationship, you would put the AddressID on the the PersonTable.
I did it using Id().GeneratedBy.Foreign() in Address class map referencing it to the Person´s ID and it worked.
Thanks!
I would map it as a component of Person. In the person ClassMap add the following:
Component(x => x.Address, m =>
{
m.Map(x => x.Street, "Street");
m.Map(x => x.State, "State");
// more here
});
Cheers