具有外键实体框架的复合键

时间:2022-07-18 11:30:15

I have the following composite key:

我有以下复合键:

Table RolesInProject

ProjectRole PK AND FK To ProjectRole

ProjectRole PK和FK到ProjectRole

Account PK AND FK To Account

账户PK和FK账户

ProjectID PK AND FK To Project

ProjectID PK和FK到项目

I want to check if a certain account has a certain role in a project without writing any SQL using the entity framework. How can i do that? I also want to be able to change/delete the role of the account using the entity framework.

我想检查某个帐户是否在项目中具有某个角色而不使用实体框架编写任何SQL。我怎样才能做到这一点?我还希望能够使用实体框架更改/删除帐户的角色。

1 个解决方案

#1


1  

You can check like:

您可以检查:

if(dbContext.RolesInProject.Any(c=>c.ProjectRole == certainRole && 
                                   c.Account == certainAccount && 
                                   c.ProjectID == certainProject))
{
    DoSomething();
}
else
{
    DoSomethingElse();
}

To delete:

var item = dbContext.RolesInProject.FirstOrDefault(c=>c.ProjectRole == certainRole && 
                                                      c.Account == certainAccount && 
                                                      c.ProjectID == certainProject);

if(item != null)
{
    dbContext.RolesInProject.DeleteObject(item);
    dbContext.SaveChanges();
}

To change:

var item = dbContext.RolesInProject.FirstOrDefault(c=>c.ProjectRole == certainRole && 
                                                      c.Account == certainAccount && 
                                                      c.ProjectID == certainProject);

if(item != null)
{
    item.SomeColumn = someValue;
    dbContext.RolesInProject.ApplyChanges(item);
    dbContext.SaveChanges();
}

#1


1  

You can check like:

您可以检查:

if(dbContext.RolesInProject.Any(c=>c.ProjectRole == certainRole && 
                                   c.Account == certainAccount && 
                                   c.ProjectID == certainProject))
{
    DoSomething();
}
else
{
    DoSomethingElse();
}

To delete:

var item = dbContext.RolesInProject.FirstOrDefault(c=>c.ProjectRole == certainRole && 
                                                      c.Account == certainAccount && 
                                                      c.ProjectID == certainProject);

if(item != null)
{
    dbContext.RolesInProject.DeleteObject(item);
    dbContext.SaveChanges();
}

To change:

var item = dbContext.RolesInProject.FirstOrDefault(c=>c.ProjectRole == certainRole && 
                                                      c.Account == certainAccount && 
                                                      c.ProjectID == certainProject);

if(item != null)
{
    item.SomeColumn = someValue;
    dbContext.RolesInProject.ApplyChanges(item);
    dbContext.SaveChanges();
}