hyp*_*erN 5 entity-framework odata asp.net-web-api
我试图为这个实体搭建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
Run Code Online (Sandbox Code Playgroud)
脚手架很好,我也使用ASP.net Identity,但我的用户类和数据库上下文在另一个项目中定义如下:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我没有任何地方:
public DbSet<User> Users{ get; set; }
Run Code Online (Sandbox Code Playgroud)
所以在脚手架之后,我的Web api项目将这一行添加到我的类DatabaseContext中:
public DbSet<User> IdentityUsers { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是当我试图从数据库中获取任何东西时,我得到了错误:
Multiple object sets per type are not supported. The object sets 'IdentityUsers' and 'Users' can both contain instances of type 'Domain.Model.UserAggregate.User'.
Run Code Online (Sandbox Code Playgroud)
如果我删除该行,我会收到错误:
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)
Run Code Online (Sandbox Code Playgroud)
在线(在我的WebApiConfig中)
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
Run Code Online (Sandbox Code Playgroud)
如果删除行(来自我的WebApiConfig):
builder.EntitySet<Trening>("Trening");
builder.EntitySet<User>("IdentityUsers");
Run Code Online (Sandbox Code Playgroud)
一切都再好了.因此问题可能是我的用户实体,所以我该如何解决这个问题呢?(当我脚手架任何与用户无关的东西时,一切都很好)
编辑
我也从我的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));
}
Run Code Online (Sandbox Code Playgroud)
并将这些行添加到WebApiConfig:
trenings.EntityType.Ignore(t => t.User);
trenings.EntityType.Ignore(t => t.UserId);
Run Code Online (Sandbox Code Playgroud)
但它没有帮助.
编辑2
所以我决定制作新的测试项目并且我已经按照这些教程(所有这些都基本相同):
所以我使用Application用户类扩展了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; }
}
Run Code Online (Sandbox Code Playgroud)
这是整个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; }
}
}
Run Code Online (Sandbox Code Playgroud)
并且错误保持不变,
我试图在测试模型中用IdentityUser替换ApplicationUser,我添加了这一行:
public DbSet ApplicationUsers {get; 组; }
但仍然是同样的错误.
注意:这次我用TestModel搭建了Odata控制器.
以前没见过这个错误。根据您的错误消息,这些行似乎给您带来了麻烦:
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<User>()
.ToTable("Users");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1082 次 |
| 最近记录: |