我不得不将一对多改为多对多关系。对于前者,用户在注册时被分配了一个companyId。他们可以从数据库返回的唯一文档是在我的Web Api中使用where语句控制的。既然可以为用户分配许多公司,我需要更改where语句来做同样的事情。到目前为止,我已经创建了联结表。我在访问它并正确返回时遇到问题。
公司类
public class Company
{
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public int UserCompanyId { get; set; }
public virtual UserCompany UserCompany { get; set; }
}
UserClass的
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 class UserCompany
{
[Key]
public int UCompanyId { get; set; }
public string Id { get; set; }
public int CompanyId { get; set; }
}
ApiController
public IEnumerable<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.UserCompanies
.Where(j => j.CompanyId == user.UserCompany.CompanyId)
.ToList();
}
}
错误发生在.ToList()
错误1无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.IEnumerable'。存在显式转换(您是否错过了演员?)
更新
// GET api/<controller>
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();
}
}
文件类
public class Document
{
public int DocumentId { get; set; }
public DateTime DocumentDate { get; set; }
public string DocumentUrl { get; set; }
public DateTime DocumentUploadDate { get; set; }
public string DocumentUploadedBy { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
}
我将IEnumberable更改为List。我仍然没有做对,我的ApiController错误
非静态方法需要目标。
我也发布了我的文档类。我迷失了如何使这项工作。第一次与多对多关系
这是stacktrace
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)
我认为这是因为您返回了一个List,并且您的函数返回的类型是IEnumerable,请尝试:
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.UserCompanies
.Where(j => j.CompanyId == user.UserCompany.CompanyId)
.ToList();
}
}