Home > Net >  EF6 how to realize the external key table associated query
EF6 how to realize the external key table associated query

Time:10-12

There are now three tables, respectively for the user, role, user_role,
User: id, name; Id primary key
Role: id, name; Id primary key
User_role: id, user_id, role_id; Id primary key, after two fields for foreign keys,
A user can have multiple roles,

Class has
The user
{
Public int Id {get; The set; }
Public string name {get; The set; }
Public List}

Role
{
Public int Id {get; The set; }
Public string name {get; The set; }
Public List}

User_role
{
Public int Id {get; The set; }
}

Dbcontext
{
Public DbSet {get; The set; }
Public DbSet {get; The set; }
Public DbSet}
I now want to link the user and role, should be how to implement,

CodePudding user response:

Navigation properties,,,,,

CodePudding user response:

reference 1st floor weixin_38925245 response:
navigation properties,,,,,,,
specific how to achieve this, most are directly online through the field matching link two tables to save, by mapping table query associated rarely,

CodePudding user response:

Do you want to set the main foreign key, ForeignKey
Public virtual ListAnd this is also key

CodePudding user response:

In fact you use ef, generate the db first, look at the approximate Model, you'll know,
In addition to the lack of foreignkey and key attribute tag,
Basic model is you want the generated code first,
As for how congestion model, after you according to your own business to encapsulate behavior to the viewmodel

CodePudding user response:

For example, under the relationship between employees and the company
 
//employee table
Public partial class WD_Employee
{
Public WD_Employee ()
{

}

[Key]
Public int id {get; The set; }
Public string account {get; The set; }
Public string name_CN {get; The set; }
Public string name_EN {get; The set; }
Public string PWD {get; The set; }
[ForeignKey (" WD_CompanyInfo ")]
Public int CompayInfoId {get; The set; }


Public virtual WD_CompanyInfo WD_CompanyInfo {get; The set; }

}



//table
Public partial class WD_CompanyInfo
{
Public WD_CompanyInfo ()
{

Enclosing WD_Employee=new HashSet
}

[Key]
Public int id {get; The set; }
Public string name_CN {get; The set; }
Public string name_EN {get; The set; }


Public virtual ICollection
}

CodePudding user response:

https://www.cnblogs.com/redfull/p/6593137.html

CodePudding user response:

Now if you want to under the user's role in a new data, how to do it, when I finished the role of foreign keys always null

CodePudding user response:

references 9 floor fan, fan response:
now if you want to under the user's role in a new data, how to do it, when I finished the role of foreign keys always null

Two methods
A new role, the foreign key value is assigned to a role
2 in the user directly. Role in this new role

CodePudding user response:

references to the tenth floor is nu month god reply:
Quote: references 9 floor fan, fan response:

Now if you want to under the user's role in a new data, how to do it, when I finished the role of foreign keys always null

Two methods
A new role, the foreign key value is assigned to a role
2 in the user directly. Role in this new role



At that time is in the foreign key assignment when the fault because of the role the Chinese and foreign key field is ef through public ListLater find method is to code the following
Var db=new Context ();
Var orderList=db. The Orders. The Include (x=& gt; X.o. rderTask)
SingleOrDefault (x=& gt; X.o. rderNo==orderNo);
If (orderList==null)
Return JsonError (" to add a task belongs to the order does not exist ");

Var orderTask=db. OrderTasks. SingleOrDefault (x=& gt; X.o. rderTaskNo==orderTaskNo);
If (orderTask!=null)
Return JsonError (" task number ");
OrderTask=new orderTask
{
OrderTaskNo=OrderTaskNo,
OrderTaskName=OrderTaskName,
OrderTaskStatus=OrderTaskStatus
};
OrderList. OrderTask. Add (OrderTask);
The SaveChanges ();
Return JsonOkays (" successful ");

The order here is equivalent to a user task is equivalent to role
  •  Tags:  
  • C #
  • Related