将IdentityDbContext与BreezeJS AspNetCore一起使用

时间:2018-05-17 14:12:01

标签: asp.net-core breeze identityserver4

当我使用IdentityDbContext

时,我无法弄清楚如何让Breeze返回MetaData

我的上下文如下

    public partial class MyContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
        {
    public MyContext(DbContextOptions options) : base(options)
        {
        }
 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {


    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<ApplicationUser>().HasMany(u => u.Claims).WithOne().HasForeignKey(c => c.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
    modelBuilder.Entity<ApplicationUser>().HasMany(u => u.Roles).WithOne().HasForeignKey(r => r.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);

    modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Claims).WithOne().HasForeignKey(c => c.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);
    modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Users).WithOne().HasForeignKey(r => r.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);

        }

        }

在我的ConfigureServices中:

// add identity
   services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<MyContext>()
            .AddDefaultTokenProviders();

我的默认控制器是:

 [Route("breeze/[action]")]
[BreezeQueryFilter]
public class DefaultController : Controller
{
    private readonly NyContext _context;
    private readonly UnitOfWork _unitOfWork;
    private readonly MyPersistenceManager _persistenceManager;

    // called via DI 
    public DefaultController(MyContext context)
    {
        _context = context;
        _unitOfWork = new UnitOfWork(context);
        _persistenceManager = new MyPersistenceManager(context);
    }

    [HttpGet]
    public IActionResult Metadata()
    {
        return Ok(_persistenceManager.Metadata());
    }

    [HttpPost]
    public SaveResult SaveChanges([FromBody] JObject saveBundle)
    {
        return _unitOfWork.Commit(saveBundle);
    }
}

当我像这样执行和测试时:

http://localhost:3333/Breeze/MetaData

我收到以下错误:

  

{“$ id”:“1”,“$ type”:“Breeze.AspNetCore.ErrorDto,Breeze.AspNetCore.NetCore”,“Code”:0,“Message”:“对象引用未设置为实例一个对象。“,”StackTrace“:”在C:\ git \ Breeze \ breeze.server.net \ AspNetCore \ Breeze.Persistence.EFCore \ MetadataBuilder.cs中的Breeze.Persistence.EFCore.MetadataBuilder.BuildAssocName(INavigation prop) :第138行\ r \ n在Breeze.Persistence.EFCore.MetadataBuilder.CreateNavProperty(INAVigation p)在C:\ git \ Breeze \ breeze.server.net \ AspNetCore \ Breeze.Persistence.EFCore \ MetadataBuilder.cs:第128行\ r \ n at System.Linq.Enumerable.WhereSelectEnumerableIterator 2.ToList()\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 source)\ r \ n at Breeze.Persistence.EFCore.MetadataBuilder.CreateMetaType(IEntityType et,Dictionary 2 dbSetMap) in C:\\git\\Breeze\\breeze.server.net\\AspNetCore\\Breeze.Persistence.EFCore\\MetadataBuilder.cs:line 89\r\n at System.Linq.Enumerable.WhereSelectEnumerableIterator 2.ToList()\ r \ n \ n在System.Linq.Enumerable.ToList [TSource](IEnumerable 1 source)\r\n at Breeze.Persistence.EFCore.MetadataBuilder.GetMetadataFromContext(DbContext dbContext) in C:\\git\\Breeze\\breeze.server.net\\AspNetCore\\Breeze.Persistence.EFCore\\MetadataBuilder.cs:line 21\r\n at Breeze.Persistence.EFCore.EFPersistenceManager 1.BuildJsonMetadata()在C:\ git \ Breeze \ breeze.server.net \ AspNetCore \ Breeze.Persistence.EFCore \ EFPersistenceManager.cs :第109行\ r \ n在Breeze.Persistence.PersistenceManager.Metadata()在C:\ git \ Breeze \ breeze.server.net \ AspNetCore \ Breeze.Persistence \ PersistenceManager.cs:第34行\ r \ n在C:\ Users \ MrTou \ Documents \ Visual Studio 2017 \ Projects \ Dictionary Project \ DP Server \ DictionaryPortalServer \中的DictionaryPortalServer.Controllers.DefaultController.Metadata() Controllers \ DefaultController.cs:第32行\ r \ n在lambda_method(Closure,Object,Object [])\ r \ n在Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target,Object [] parameters)\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()\ r \ n ---从抛出异常的上一个位置的堆栈跟踪结束--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo。抛出()\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext()\ r \ n ---结束从抛出异常的先前位置堆栈跟踪--- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispat Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp;)中的Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)\ r \ n的chInfo.Throw()\ r \ n接下来,范围&amp;范围,对象&amp; state,Boolean&amp; isCompleted)\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext()\ r \ n ---从抛出异常的上一个位置的堆栈跟踪结束--- \ r \ n在System。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__23.MoveNext()“,”中的Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n EntityErrors“:空}

我知道我的上下文正在运行,因为如果我使用另一个控制器来获取这样的数据:

http://localhost:3333/breeze/Summary/CartOrders

我收回了数据。只有当我尝试获取MetaData时才会出现错误。

似乎Breeze无法生成MetaData因为

ApplicationUser和ApplicationRole

有人能指出我正确的方向吗?

谢谢!

0 个答案:

没有答案