WCF Data Services Many-To-Many relationship - entity-framework-4.1

I have a database with tables for users and roles. The relationship between them is many-to-many and I have a junction table UserRole.
I use Entity Framework to map this to my object model using this configuration:
modelBuilder.Entity<User>()
.HasMany(u => u.Roles)
.WithMany(r => r.Users)
.Map(m => m.ToTable("UserRole")
.MapLeftKey("UserId")
.MapRightKey("RoleId"));
This works as expected but now I need to expose this to Excel using OData and Powerpivot. But since the relationships are lost when imported into Powerpivot I also need to expose the junction table so that I can recreate the relationship inside Powerpivot.
I understand that I need to create a new entity, UserRole but I am not sure how to configure the mapping.
Does anyone have any suggestions?

Related

Am I allowed to define both many-to-many and one-to-many relationships between User and Server models in Sequelize?

I have 2 models called User and Server and my verbal description of their relationships is something like this:
A User can create Servers and is therefore their owner. Following this logic, I need to implement a one-to-many relationship between these 2 models. That means the Server should have a userId column which will contain the id of the User/Owner.
Users can join many servers and a Server can be joined by many users, so I need to implement many-to-many relationship between these 2 models and have a junction table as well.
So my question is can I apply 2 relationships between the models like this:
let User = require('./models/User');
let ServerUser = require('./models/ServerUser');
let Server = require('./models/Server');
User.hasMany(Server)
Server.belongsTo(User)
User.belongsToMany(Server, { through: ServerUser });
Server.belongsToMany(User, { through: ServerUser });
What makes me think this won't work is this - once a relationship is created, Sequelize offers you some magic methods which you can use. For example in my case I have this:
let name = req.body.name;
let thumbnail = req.body.image;
let userId = req.body.userId
let user = await User.findByPk(userId)
let socketServer = await Server.create({
name: name,
thumbnail: thumbnail
});
user.addServer(socketServer)
Since I've defined a relationship between User and Server, I can use addServer() which does one of two things depending on whether the relationship between the models is one-to-many or many-to-many.
If I've defined many-to-many relationship, executing user.addServer(socketServer) will create a record in the junction table that will link the userId and serverId.
If I've defined one-to-many relationship, executing user.addServer(socketServer) will update the userId column of the server to the id of user
So now that I've applied both relationships between the models, when I execute user.addServer(socketServer), I only update the userId column of the server with the id of the user BUT I don't get a record in the junction table that creates the many-to-many relationship.
This leads me to believe I can only use one relationship at a time but how am I to achieve what I'm trying to achieve without the 2 relationships?
opinion
Your logic you addressed seems to me that User creates server and user within the server creates server on and on. Is the consequence correct?
shouldn't the one user create servers and other users go in to the servers
my suggetion is create one to many among user and server and have privillage table so the one user created the server have the authority to the what ever he or she wants.

relationship of mysql in laravel 5.5

I am trying to model with Laravel a database for purchases, in my mysql the relations are [ (for each table in the mysql i have one model)
so I was modeling in Laravel, for the associative table "fornecedor_item_detalhe" I used relation "belongstoMany", it worked out using tinker.
Now comes the problem:
together with the detalhes("fornecedor_detalhe") and the "items" I would like to be able to access the other tables related to "fornecedor_detalhe" which are "fornecedor" and "formpagto"."Has Many Through" will work For this?
And also I would like to link this associative table "fornecedor_detalhes-Item" in relation n: m with another table to create another associative table "fornecedor_detalhe_item_rci".
I've created a model for the pivot table "fornecedor_detalhe_item", but I'm not exactly sure how to do that, as long pivot tables don't have a primary key, how can i reference to them in the other pivot table
I did not codify anything, I'm just trying to model the same thing in laravel.
If you have any suggestions about MER I also accept it.
Thank you
just use Eloquent: Relationships
look in this link realtion types
and you should have model for fornecedor and formpagto
by build relation in model hasMany or belongsTo as your database structure you can call the anywhere just by type relation name
for example
in User Model
public function post()
{
return $this->hasMany(Post::class);
}
when you call in blade $user->post you get all posts related to this user and so on

Grails not generating all SQL tables in many-to-many

I'm a new to Grails and have started a project, but I'm having troubles finding out what is wrong:
The project is already connected to my database (SQL) and it has a few many-to-many to relationship with more than 1 "parameter", like this:
static hasMany = [rules:AvaliateRules,professors:Professor,candidates:Candidate];
I run the application with no problems, but when I used show tables the transaction tables weren't all created. It just created the last parameter's table (candidate).
Any idea about the reason and how to fix it? everywhere I checked, people did the same as me and had no problems with the tables. I'm using grails 2.4.4 version.
I don't think that your current domain has many-to-many relationship with all entities that you mentioned. It might be a one-to-many relationship. Let's say your current domain is "DomainA". If only other domain like AvaliateRules,Professor has "DomainA" in hasMany then it makes it a many-to-many association.
In one-to-many association, only a column is added at the child level(many side) which contains a parentId to denote who the parent is.

JPA Entities - MySQL tables Mapping

Database tables:
Tutor( {PK}tutorId, name )
Module( {PK}moduleId, name, {FK}tutorId )
Relationship Tutor -> Module (OneToMany)
Questions:
If you create the domain model
classes with JPA annotations, the
corresponding database tables are
auto-created with the same columns as the annotated fields of the class?
Do you create the database first
and then the JPA classes with the
same fields as the database table
columns?
How do you model foreign key constraints with JPA
(1) and (2) are depended on your situation. you can create domain model class first and it will generate table and columns which are the similar to the fields. In addition, you can establish a database first (it is easy to design and have better view of the whole database.), then map the tables to your domain class.
about (3) you can try this link

Possible to Fix a Foreign Key in Yii without having set it up in the Database?

I'm using phpMyAdmin for my database GUI and it's connecting to Yii Framework on my website.
I wish for my products table for instance, to have a foreign key department_id which is a reference to the id field of my departments table. Unfortunately, I don't currently have the facility to set the foreign key up properly in phpMyAdmin, so department_id is just an indexed field. I cannot change the configuration of phpMyAdmin at the moment so it's stuck like it is without a foreign key and relationship facility.
Is there a way to modify the Models of these tables in Yii so they link? I know there is a relations function inside the Model Class file that holds this information:
return array('department_id' => array(self::BELONGS_TO, 'Departments', 'id'),
Could I not just add something similar to the above? Is there more legwork? Is it now fixed (as in static, not corrected) because of phpMyAdmin?
Cheers
If I'm not mistaken, you don't need to have mySql enforcing foreign key relationships for them to still work in Yii. Setting up FK constraints in mySql ensures proper database integrity, but I don't think Yii actually uses that at runtime.
When initially running yiic (of Gii) to build the project I think it looks at the DB to build the right relations in the Model, but it doesn't use them after that.
Yii then uses this knowledge (from yiic) of the table relationships to make your life easier by providing shortcut methods to access relational data, and to ensure you don't violate mySql constraints and get ugly SQL errors, etc. But you can still use Yii relation logic without the underlying SQL constraints. The only problem will be that if Yii messes up and assigns a non-existing FK or something, your database will not catch this error (your data integrity will be more error prone).
To link your products to departments, just make sure you have a department_id field in the Product (which it sounds like you do). Then add a relation rule like so to Product:
'department' => array(self::BELONGS_TO, 'Department', 'department_id'),
And in your Department model:
'products' => array(self::HAS_MANY, 'Product', 'department_id'),
Now you should be able to use the relation like normal:
$myProductModel->department; // returns the model of the Department referenced
$myDepartmentModel->products; // returns the models of all Products in the department
Good luck, and let me know if I am way off base and it doesn't work for you!