gpt4 book ai didi

entity-framework - 脚手架时Web api Odata错误

转载 作者:行者123 更新时间:2023-12-03 17:50:50 27 4
gpt4 key购买 nike

我试图为这个实体搭建 Odata Controller :

        #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 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");
}
}

注意:我没有任何线路:
public DbSet<User> Users{ get; set; }

因此,在搭建我的 Web api 项目之后,将这一行添加到我的类 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");

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

编辑

我还从我的趋势 Controller 中删除了这些行:
// 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

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

Tutorial 1
Tutorial 2
Tutorial 3
Tutorial 4

因此,我使用 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; }
}

这是整个 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 { 获取;放; }

但仍然是同样的错误。

注意:这次我用 TestModel 搭建了 Odata Controller 。

最佳答案

以前没见过这个错误。根据您的错误消息,这些行似乎引起了您的麻烦:

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

关于entity-framework - 脚手架时Web api Odata错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22021221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com