脚手架时Web api Odata错误



我已经尝试为这个实体构建Odata控制器:

#region Attributes
public Guid TreningId { get; set; }
public DateTime DateTimeWhenTreningCreated { get; set; }
#endregion
#region Navigational properties
public string UserId { get; set; }
public User User { get; set; }
public int RoutineId { get; set; }
public Routine Routine { get; set; }

#endregion

脚手架做得很好,我也在使用ASP.net标识,但我的用户类和数据库上下文是在另一个项目中定义的:

public class User : IdentityUser
{
public String Something { get; set; }
}
And database context looks like this:
public class DatabaseContext : IdentityDbContext<User>
{
public MadBarzDatabaseContext()
: base("DefaultConnection")
{
Configuration.LazyLoadingEnabled = true;
}
...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<User>()
.ToTable("Users");
}
}

注意:我没有任何地方的线路:

public DbSet<User> Users{ get; set; }

因此,在搭建我的Webapi项目之后,将这一行添加到我的类DatabaseContext:中

public DbSet<User> IdentityUsers { get; set; }

但当我试图从数据库中提取任何东西时,我出现了错误:

Multiple object sets per type are not supported. The object sets 'IdentityUsers' and 'Users' can both contain instances of type 'Domain.Model.UserAggregate.User'.

如果我删除那一行,我会得到错误:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll but was not handled in user code
Additional information: Could not load file or assembly 'System.Web.Mvc, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

在线(在我的WebApiConfig中)

config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

如果删除行(从我的WebApiConfig):

builder.EntitySet<Trening>("Trening");
builder.EntitySet<User>("IdentityUsers"); 

一切又好起来了。所以问题可能是我的用户实体,所以我该如何解决这个问题?(当我脚手架任何与用户无关的东西时,一切都很好)

编辑

我还从我的trening控制器中删除了这些行:

// GET odata/Trening(5)/User
[Queryable]
public SingleResult<User> GetUser([FromODataUri] Guid key)
{
return SingleResult.Create(db.Trenings.Where(m => m.TreningId == key).Select(m => m.User));
}

并将这些行添加到WebApiConfig:

trenings.EntityType.Ignore(t => t.User);
trenings.EntityType.Ignore(t => t.UserId);

但这无济于事。

编辑2

所以我决定做一个新的测试项目,我遵循了这些教程(所有的教程基本上都是一样的):

教程1教程2教程3教程4

因此,我使用应用程序用户类扩展了Identity User,并添加了这样的测试模型:

public class TestModel
{
public int Id { get; set; }
public string Test { get; set; }

public string UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}

这是整个IdentityModels类:

namespace WebApplication2.Models
{
public class ApplicationUser : IdentityUser
{
public string Email { get; set; }
public string Data { get; set; }
public ICollection<TestModel> TestModels { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}

public DbSet<TestModel> TestModels { get; set; }
}
}

错误保持不变,

我尝试在测试模型中用IdentityUser替换ApplicationUser,并添加了以下行:

public DbSet ApplicationUsers{get;set;}

但还是同样的错误。

注意:这次我用TestModel搭建了Odata控制器。

以前从未见过此错误。根据你的错误信息,看起来这些线路正在给你带来麻烦:

modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<User>()
.ToTable("Users");

最新更新