我不得不将一对多改为多对多关系。对于前者,用户在注册时被分配了一个companyId。他们可以从数据库返回的唯一文档是在我的Web Api中使用where语句控制的。既然可以为用户分配许多公司,我需要更改where语句来做同样的事情。
错误
Error 1 Operator '==' cannot be applied to operands of type 'int' and 'System.Collections.Generic.ICollection<TransparentEnergy.Models.Company>' C:\Development\TransparentEnergy\TransparentEnergy\ControllersAPI\apiDocumentUserController.cs 31 33 TransparentEnergy
ApiController
public IEnumerable<DocumentResult> Get()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
using (var context = new ApplicationDbContext())
{
return context.Documents
.Where(j => j.CompanyId == user.Companies)
.ToResults();
}
}
类
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public List<Document> Documents { get; set; }
public ICollection<ApplicationUser> Users { get; set; }
}
类
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string Name { get; set; }
public ICollection<Company> Companies { get; set; }
}
更新 结点表
public class UserCompany
{
[Key]
public int UCompanyId { get; set; }
public string Id { get; set; }
public int CompanyId { get; set; }
}
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public int UserCompanyId { get; set; }
public virtual UserCompany UserCompany { get; set; }
}
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager
.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public int UserCompanyId { get; set; }
public virtual UserCompany UserCompany { get; set; }
}
控制器
public List<Document> GetDocuments()
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
using (var context = new ApplicationDbContext())
{
return context.Documents
.Where(j => j.CompanyId == user.UserCompany.UCompanyId)
.ToList();
}
}
错误
非静态方法需要目标。
堆栈跟踪
at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) 在System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化) 在System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数,CultureInfo文化) 在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object [] index,CultureInfo culture) 在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object [] index) 在System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryGetFieldOrPropertyValue(MemberExpression me,Object instance,Object&amp; memberValue) 在System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.TryEvaluatePath(表达式表达式,ConstantExpression&amp; constantExpression) at System.Data.Entity.Core.Objects.ELinq.QueryParameterExpression.EvaluateParameter(Object [] arguments) 在System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1.&lt;&gt; c__DisplayClass7.b__6() 在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery
1.&lt;&gt; c__DisplayClass7.b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func1 operation) at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator&gt; b__0() 在System.Data.Entity.Internal.LazyEnumerator1.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable
1来源) 在C:\ Development \ TransparentEnergy \ TransparentEnergy \ ControllersAPI \ apiDocumentUserController.cs中的TransparentEnergy.ControllersAPI.apiDocumentUserController.GetDocuments():第29行 在lambda_method(Closure,Object,Object []) 在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor。&lt;&gt; c__DisplayClass10.b__9(Object instance,Object [] methodParameters) 在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance,Object [] arguments) 在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2 arguments,CancellationToken cancellationToken)
答案 0 :(得分:0)
我不知道有关实体框架语法的细节,但如果您在Users和Companies之间有多对多的关系,则需要创建一个名为“UserCompany”的关联表。此表将User和Company的主键作为外键,然后您可以拉取UserCompany主键以查看哪些用户与哪些公司相关。