How do I get second level public properties in ServiceStack - servicestack-autoquery

I have the following POCOs in my service model:
public class Personnel {
...
[Ignore]
[Reference]
public List<Posting> _Postings { get; set; }
}
public class Posting {
...
[Reference]
[Ignore]
public Personnel Personnel { get; set; }
[Reference]
[Ignore]
public Position _Position { get; set; }
}
public class Position {
...
[Reference]
[Ignore]
public List<Posting> _Postings { get; set; }
}
When I use AutoQuery RDBMS to get Personnel I want to be able to get e.g. Personnel._Postings[1]._Position.Title.
How do I get the Position object which is a second level public property to Personnel using ServiceStack AutoQuery RDBMS? Please help.

Don't use [Ignore] that tells OrmLite and AutoQuery to ignore the property.
AutoQuery automatically returns nested results but your Data Models need to follow OrmLite's Reference Conventions.

Related

Can not get list in a model which I get it AsQuerable in ef?

public class Product
{
public int Id{get;set;}
public int UserId{get;set;}
public Users User{get;set;}
}
I have set the Users to Product's relative:
b.HasOne("User").WithMany().HasForeignKey("UserID");
When I use entityframework to get the list of products.
The User is returned null, why?
There is a value in User table and the UserId is right in Product Table.
var list = _context.Products.AsQueryable();
the items in list has the User=null.
You need to Include the entity you're looking for. For example, let's suppose I have the following context.
AppDbContext.cs
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Notification> Notifications { get; set; }
public DbSet<Offer> Offers { get; set; }
}
Notification.cs
public class Notification
{
public int Id { get; set; }
public string Title { get; set; }
public int? OfferId { get; set; }
public virtual Offer Offer { get; set; }
}
If you want to use the Offer entity from Notification, you need to use the following statement:
context.Notifications.Include(n=> n.Offers).ToList();
// Your code goes here
In your situation:
var list = _context.Products.Include(p=> p.User).AsQueryable();
You have to explicitly ask to include the users in the returned list.
_context.Products.Include(p => p.Users).AsQueryable();

Filter expression cannot be specified for an entity type because it may only be applied to the root entity type in a hierarchy

I am trying to change the way asp.net generates its identity tables, trying to base the generation on an Id of int instead of a Guid(string), also adding a different schema(instead of dbo -> Security) and a QueryFilter for all my entities, in that case I created for each class a Mapping but will illustrate the idea with just one that is giving me the error.
public class AspNetRole : IdentityRole<int>, IEntityBase
{
public bool IsDeleted { get; set; }
public string CreatedBy { get; set; }
public string UpdatedBy { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
}
public interface IEntityBase
{
int Id { get; set; }
bool IsDeleted { get; set; }
string CreatedBy { get; set; }
string UpdatedBy { get; set; }
DateTime? CreatedOn { get; set; }
DateTime? UpdatedOn { get; set; }
}
The mapping class with the QueryFilter:
public class AspNetRoleMap : IEntityTypeConfiguration<AspNetRole>
{
public void Configure(EntityTypeBuilder<AspNetRole> builder)
{
builder.ToTable(name: "AspNetRole", schema: "Security");
builder.HasQueryFilter(app => !app.IsDeleted);
}
}
The DbContext:
public class AspNetSecurityDbContext : IdentityDbContext<AspNetUser, IdentityRole<int>, int>
{
public AspNetSecurityDbContext(DbContextOptions<AspNetSecurityDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new AspNetRoleMap());
}
}
Once I run the migration I am getting the following error:
The filter expression 'app => Not(app.IsDeleted)' cannot be specified
for entity type 'AspNetRole'. A filter may only be applied to the root
entity type in a hierarchy.
I tried this approach https://github.com/aspnet/EntityFrameworkCore/issues/10259 but still getting more errors
builder.HasQueryFilter(app => !((IEntityBase)app).IsDeleted);
The problem has nothing in common with EF Core query filter, but the incorrect base generic IdentityDbContext argument. Here
: IdentityDbContext<AspNetUser, IdentityRole<int>, int>
you are passing IdentityRole<int>, which in the base OnModelCreating will be configured as entity, hence EF Core will map your AspNetRole entity using TPH inheritance strategy, which along with the additional discriminator column introduces additional constraints, like the query filter exception you are getting.
To fix that, pass the correct generic type argument which in this case is the custom AspNetRole class:
: IdentityDbContext<AspNetUser, AspNetRole, int>
In case you create other custom entities inheriting the generic IndentityXyz<> classes, take a look at the other base IdentityDbContext classes having more generic type arguments, and select the one that allows you the pass all your custom identity derived types.

Parameter xxx of domain operation entry xxx must be one of the predefined serializable types

I get this webservice error sometimes on a SL5 + EF + WCF app.
"Parameter 'role' of domain operation entry 'AddUserPresentationModelToRole' must be one of the predefined serializable types."
here is a similar error, however his solution doesn't work for me.
I have the codegenned DomainService which surfaces the database entities to my client:
[EnableClientAccess()]
public partial class ClientAppDomainService : LinqToEntitiesDomainService<ClientAppUserEntitlementReviewEntities>
{
public IQueryable<Account> GetAccounts()
{
return this.ObjectContext.Accounts;
}
//..etc...
and my custom service which is surfacing a Presentation model, and db entities.
[EnableClientAccess]
[LinqToEntitiesDomainServiceDescriptionProvider(typeof(ClientAppUserEntitlementReviewEntities))]
public class UserColourService : DomainService
{
[Update(UsingCustomMethod = true)]
public void AddUserPresentationModelToRole(UserPresentationModel userPM, Role role, Reviewer reviewer)
{
...
}
public IDictionary<long, byte> GetColourStatesOfUsers(IEnumerable<RBSUser> listOfUsers, string adLogin)
{
//....
}
}
and the PresentationModel:
public class UserPresentationModel
{
[Key]
public long UserID { get; set; }
public byte UserStatusColour { get; set; }
public string MessageText { get; set; }
[Include]
[Association("asdf", "UserID", "UserID")]
public EntityCollection<Account> Accounts { get; set; }
public DateTime AddedDate { get; set; }
public Nullable<long> CostCentreID { get; set; }
public DateTime? DeletedDate { get; set; }
public string EmailAddress { get; set; }
public long EmployeeID { get; set; }
public string FirstName { get; set; }
public Nullable<bool> IsLeaver { get; set; }
public string LastName { get; set; }
public DateTime LastSeenDate { get; set; }
public string LoginDomain { get; set; }
public string LoginName { get; set; }
public byte WorldBuilderStatusID { get; set; }
}
Also cannot get the solution to reliably fail. It seems whenever I change the service slightly ie make it recompile, everything works.
RIAServices unsupported types on hand-built DomainService - seems to be saying the same thing, that decorating the hand built services with the LinqToEntitiesDomainServiceDescriptionProvider should work.
Possible answer here will post back here too with results.
From Colin Blair:
I am a bit surprised it ever works, I don't think I have seen anyone trying to pass additional entiities into a named update before. It might be a bug in RIA Services that it is working at all. What are you trying to accomplish?
Side note, you have a memory leak with your ObjectContext since it is not getting disposed of correctly. Is there a reason you aren't using the LinqToEntitiesDomainSerivce? It would take care of managing the ObjectContext's lifetime for you.
Results:
1) This makes sense. Have refactored out to more sensible parameters now (ints / strings), and all working.
2) Have brought together my 3 separate services into 1 service, which is using the LinqToEntitiesDomainSerivce. The reason I'd split it out before was the assumption that having a CustomUpdate with a PresentationModel didn't work.. and I had to inherit off DomainService instead. I got around this by making a method:
// need this to avoid compile errors for AddUserPresentationModelToRole.. should never be called
public IQueryable<UserPresentationModel> GetUserPresentationModel()
{
return null;
}

Entity Framework and using Fluent API for mapping two entities to another one

Scenario seems to be trivial and I'm really confused on what I'm doing wrong.
So, I have a Client class
public class Client
{
[Key]
public int ClientID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Account Account { get; set; }
}
Employee class
public class Employee
{
[Key]
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Account Account { get; set; }
}
and an Account class
public class Account
{
[Key]
public int AccountID { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public virtual Employee Employee { get; set; }
public virtual Client Client { get; set; }
}
Both Client and Employee may have an Account or not ( online access is optional ). As database is not compatible with EF namingconvention I have to come up with Fluent API explicit mappings.
Both Client and Employee tables have "AccountID" column that I'm trying to use to build a relation.
modelBuilder.Entity<Client>()
.HasOptional(e => e.Account)
.WithRequired(a => a.Client)
.Map(m => m.MapKey("AccountID"));
modelBuilder.Entity<Employee>()
.HasOptional(e => e.Account)
.WithRequired(a => a.Employee)
.Map(m => m.MapKey("AccountID"));
but I get
Schema specified is not valid. Errors:
(15,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined.
(16,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined.
so, is there a way to fix this other than modification of the table/entity structure?
Turns out you don't need Fluent API in this case, what you need is to DataAnnotate your properties in Entities with InverseProperty attribute
[InverseProperty("AccountID")]
There is a great answer by Ladislav Mrnka in Entity Framework 4.1 InverseProperty Attribute question
However if anyone knows how to do that correctly with Fluent answers are highly appreciated

MVC3 - Extending a Class and Updating the SQL Table

I am using MVC3 and Entity Framework. I have a class called User with 20 different properties. I have already created a database and filled it with some data. I want to break out the Addresses property and make it it's own class.
namespace NameSpace.Domain.Entities
{
public class User
{
public int UserId { get; set; }
...
...
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
}
}
I want to break out both Addresses like so
namespace NameSpace.Domain.Entities
{
public class User
{
public int UserId { get; set; }
...
...
public Addresses Addresses { get; set; }
}
public class Addresses
{
public string AddressOne { get; set; }
public string AddressTwo { get; set; }
}
}
HERE'S MY QUESTION:
Since I already have the data table filled with data, how can I update this in the Server Explorer?
Thanks ( if you need more info please let me know )
If you are using EF code first 4.3 you can use the concept of migrations to achive what you want.
You will need to do a code based manual migration since you change is a bit to advanced for the framework to figure it out itselfe.
Further reading: http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx