I've tried to scaffold Odata controller for this entity:
#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
And scaffolding went fine, I'm also using ASP.net Identity, but my user class and database context are defined in another project like this:
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");
}
}
Note: I don't have anywhere line:
public DbSet<User> Users{ get; set; }
So after scaffolding my Web api project added this line to my class DatabaseContext:
public DbSet<User> IdentityUsers { get; set; }
But then when I tried to fetch anything from database I got error:
Multiple object sets per type are not supported. The object sets 'IdentityUsers' and 'Users' can both contain instances of type 'Domain.Model.UserAggregate.User'.
If I delete that line I get error:
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)
At line (in my WebApiConfig)
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
If remove lines (from my WebApiConfig):
builder.EntitySet<Trening>("Trening");
builder.EntitySet<User>("IdentityUsers");
Everything forks fine again. So probably problem is whit my User entity, so how can I solve this ? (when I scaffold anything that has nothing to do with User, everything is fine)
EDIT
I've also deleted these lines from my trening 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));
}
And added these lines to WebApiConfig:
trenings.EntityType.Ignore(t => t.User);
trenings.EntityType.Ignore(t => t.UserId);
But it didn't help.
EDIT 2
So I've decided to make new test project and I've followed these tutorials (all of them are basically same):
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4
So I've extended Identity User using Application user class and added test model like this:
public class TestModel
{
public int Id { get; set; }
public string Test { get; set; }
public string UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
}
And here is whole IdentityModels class:
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; }
}
}
And the error remains the same,
I've tried to replace ApplicationUser with IdentityUser in test model and I've added this line:
public DbSet ApplicationUsers { get; set; }
But still same error.
Note: this time I scaffolded Odata controller with TestModel.