I'd like to display the 'ApplicationRole' Name with the 'ApplicationUser' data together using Eager loading.
here is my ApplicationUser
public class ApplicationUser : IdentityUser
{
public string? Name { get; set; }
public string? Department { get; set; }
public bool IsDeleted { get; set; }
public string RoleId { get; set; }
public virtual ApplicationRole Role { get; set; } = new ApplicationRole();
}
public class ApplicationUserDTO
{
public string Id { get; set; }
public int UserId { get; set; }
public string Name { get; set; }
public string UserName { get; set; }
// Foreign Key
public string RoleId { get; set; }
public virtual ApplicationRoleDTO Role { get; set; } = new ApplicationRoleDTO();
}
here is ApplicationRole
[NotMapped]
public class ApplicationRole : IdentityRole
{
public string Id { get; set; }
public string Name { get; set; }
//[InverseProperty(nameof(ApplicationUser.Role))]
public virtual ICollection<ApplicationUser> Users { get; set; } = new List<ApplicationUser>();
}
public class ApplicationRoleDTO
{
public string Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ApplicationUserDTO> Users { get; set; } = new List<ApplicationUserDTO>();
}
and here is the UserRepository
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync()
{
//return await _dbContext.User.Where(u => u.IsDeleted == false).ToListAsync();
return await _dbContext.User.Include(u => u.Role).Where(u => u.IsDeleted == false).ToListAsync();
}
public async Task<ApplicationUser> GetUserAsync(string id)
{
return await _dbContext.User.Where(u => u.Id == id).Where(u => u.IsDeleted == false).FirstOrDefaultAsync();
}
But when I use the 'Include' keyword it show this error
When I try to use the string "Role", it also show error
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync()
{
return await _dbContext.User.Include("Role").Where(u => u.IsDeleted == false).ToListAsync();
}
Here is the error show
I have the following 2 Parent/Child objects:-
public Submission()
{
SubmissionQuestionSubmission = new HashSet<SubmissionQuestionSubmission>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Npi { get; set; }
public bool Independent { get; set; }
public string Comment { get; set; }
public virtual ICollection<SubmissionQuestionSubmission> SubmissionQuestionSubmission { get; set; }
}
public partial class SubmissionQuestionSubmission
{
public int SubmissionQuestionId { get; set; }
public int SubmissionId { get; set; }
public string Answer { get; set; }
public virtual Submission Submission { get; set; }
}
and i created the following view model:-
public class SubmissionCreate
{
public Submission Submission {set; get;}
public IList<SubmissionQuestion> SubmissionQuestion { set; get; }
public IList<SubmissionQuestionSubmission> SubmissionQuestionSubmission { set; get; }
}
where i have the following action method to add a parent record (submission) and a child record (SubmissionQuestionSubmission ), but to do so, i have to issue 2 save requests to the database, one to save the parent and get its ID, while the other to save the child record and assign it the parent ID, as follow:-
public async Task<IActionResult> Create(SubmissionCreate sc)//Bind("Id,FirstName,LastName,Npi,Independent,Comment")]
{
if (ModelState.IsValid)
{
var newsubmission = _context.Submission.Add(sc.Submission);
await _context.SaveChangesAsync();
foreach (var v in sc.SubmissionQuestionSubmission)
{
v.SubmissionId = sc.Submission.Id;
_context.SubmissionQuestionSubmission.Add(v);
}
await _context.SaveChangesAsync();
TempData["message"] = "Thank You.. Your request has been submitted...";
return View("Confirmation");
}
return View(sc);
}
so my question is if i can do the above job, using one save statement instead of 2?
You don't need use two SaveChanges. You can assign newsubmission into Submission property instead of v.SubmissionId = sc.Submission.Id;.
In this case Id and ForeignKey created automatically by EF Core
var newsubmission = _context.Submission.Add(sc.Submission);
foreach (var v in sc.SubmissionQuestionSubmission)
{
v.Submission = newsubmission;
_context.SubmissionQuestionSubmission.Add(v);
}
await _context.SaveChangesAsync();
Another way
sc.Submission.SubmissionQuestionSubmission = new List<SubmissionQuestionSubmission>();
foreach (var v in sc.SubmissionQuestionSubmission)
{
sc.Submission.SubmissionQuestionSubmission.Add(v)
}
_context.Submission.Add(sc.Submission);
await _context.SaveChangesAsync();
This question builds on the following question (s)
Indexing : How do I construct my RavenDb static indexes for this document, given these requirements?
Simple Where clause with paging : How to construct a proper WHERE clause with RavenDb
The essence of the question is how do I dynamically add or remove fields to participate in a where clause?
Document:
[Serializable]
public class Product
{
public string AveWeight { get; set; }
public string BrandName { get; set; }
public string CasePack { get; set; }
public string Catalog { get; set; }
public decimal CatalogId { get; set; }
public decimal CategoryId { get; set; }
public string Info { get; set; }
public bool IsOfflineSupplierItem { get; set; }
public bool IsRebateItem { get; set; }
public bool IsSpecialOrderItem { get; set; }
public bool IsSpecialPriceItem { get; set; }
public bool IsTieredPricingItem { get; set; }
public string ItemNum { get; set; }
public string ManufactureName { get; set; }
public string ManufactureNum { get; set; }
public decimal OffineSupplierId { get; set; }
public string PackageRemarks { get; set; }
public decimal Price { get; set; }
public decimal PriceGroupId { get; set; }
public decimal ProductId { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public string SupplierName { get; set; }
public string UOM { get; set; }
public string Upc { get; set; }
public string Url { get; set; }
}
Index:
if (store.DatabaseCommands.GetIndex("Products_Index") == null)
{
store.DatabaseCommands.PutIndex("Products_Index", new IndexDefinitionBuilder<Product>
{
Map = products => from p in products
select new { p.CatalogId,
p.HasPicture,
p.INFO2,
p.IsOfflineSupplierItem,
p.IsRebateItem,
p.IsSpecialOrderItem,
p.IsSpecialPriceItem,
p.IsTieredPricingItem,
p.Price },
Indexes =
{
{ x => x.INFO2, FieldIndexing.Analyzed },
{ x => x.CatalogId, FieldIndexing.Default},
{ x => x.HasPicture, FieldIndexing.Default},
{ x => x.IsOfflineSupplierItem, FieldIndexing.Default},
{ x => x.IsRebateItem, FieldIndexing.Default},
{ x => x.IsSpecialOrderItem, FieldIndexing.Default},
{ x => x.IsSpecialPriceItem, FieldIndexing.Default},
{ x => x.IsTieredPricingItem, FieldIndexing.Default},
{ x => x.Price, FieldIndexing.Default}
}
});
}
Naive Where clause
string t1 = "foo";
bool t2 = true;
decimal t3 = 100m;
products = DocumentSession.Query<Product>()
.Statistics(out stats)
.Where(p => p.INFO2.StartsWith(t1) && p.IsRebateItem == t2 && p.CatalogId = t3)
.OrderByField(columnToSortBy, columnToSortByAsc)
.Skip(pageIndex * pageSize)
.Take(pageSize)
.ToList()
;
First Pass at Advanced Query
var products = s.Advanced.LuceneQuery<Product>("Products")
.WhereEquals("Catalog", "National Catalog")
.ToList()
;
which throws an exception
A first chance exception of type 'Lucene.Net.QueryParsers.QueryParser.LookaheadSuccess' occurred in Lucene.Net.dll
A first chance exception of type 'System.IO.IOException' occurred in Lucene.Net.dll
Second pass (works)
result = s.Advanced.LuceneQuery<Product>("Products_Index")
.Where("CatalogId:(736275001) AND HasPicture:(true) AND IsOfflineSupplierItem:(false)")
.ToArray();
Third Pass (and fastest yet)
result = s.Advanced.LuceneQuery<Product>("Products/Index")
.Statistics(out stats)
.WhereStartsWith("INFO2", "ink")
.AndAlso()
.WhereStartsWith("INFO2", "pen")
.AndAlso()
.WhereEquals("CatalogId", 736275001)
.AndAlso()
.WhereEquals("HasPicture", true)
.AndAlso()
.WhereEquals("IsOfflineSupplierItem", false)
.AndAlso()
.WhereEquals("IsRebateItem", false)
.AndAlso()
.WhereEquals("IsSpecialOrderItem", false)
.AndAlso()
.WhereEquals("IsSpecialPriceItem", false)
.ToArray()
;
If you want to do this dynamically, you can use the DocumentSession.Advanced.LuceneQuery, which allows you to pass strings as the property names for the index.
That way, you don't have to deal with the strongly typed issues.
There is o possibility to create a convention for Column naming:
I have this piece of code:
public AutoPersistenceModel Generate()
{
var result = AutoPersistenceModel.MapEntitiesFromAssemblyOf<User>()
.Where(GetAutoMappingFilter)
.WithConvention(GetConventions);
return result;
}
private bool GetAutoMappingFilter(Type t)
{
return
t.GetInterfaces().Any(
x => x.IsGenericType && (x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>)));
}
private static void GetConventions(Conventions conventions)
{
conventions.GetPrimaryKeyNameFromType = type => type.Name.ToLower() + "_id";
conventions.FindIdentity = type => type.Name.ToLower() == "id";
conventions.GetTableName = type =>
{
if (!type.Name.Contains("Lookup"))
{
return Inflector.Net.Inflector.Pluralize(type.Name).ToLower();
}
return Inflector.Net.Inflector.Underscore(type.Name)
.Replace("lookup", "lu").ToLower();
};
conventions.IsBaseType = DontMapAsJoinedSubclassTypesInheritedFrom;
conventions.GetForeignKeyNameOfParent = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
conventions.GetForeignKeyName = type => Inflector.Net.Inflector.Underscore(type.Name) + "_id";
conventions.OneToManyConvention = m => m.Cascade.All();
}
private static bool DontMapAsJoinedSubclassTypesInheritedFrom(Type arg)
{
var derivesFromEntity = arg == typeof(Entity);
var derivesFromEntityWithTypedId = arg.IsGenericType &&
(arg.GetGenericTypeDefinition() == typeof(EntityWithTypedId<>));
return derivesFromEntity || derivesFromEntityWithTypedId;
}
and an Entity class
public class Organisation : EntityWithTypedId<int>
{
public virtual Organisation Parent { get; set; }
public virtual LookOrganisationType OrganisationType { get; set; }
[DomainSignature]
public virtual string OrganisationName { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string City { get; set; }
public virtual string Postcode { get; set; }
public virtual LookupCountry Country { get; set; }
public virtual string TelNo { get; set; }
public virtual string FaxNo { get; set; }
public virtual string Email { get; set; }
public virtual User Contact { get; set; }
public virtual DateTime? DateCreated { get; set; }
public virtual DateTime? DateAmended { get; set; }
public virtual bool Active { get; set; }
}
Finally I want to have a column for, let's say TelNo like tel_no.
So the Convention is if Property contains capital letter in the middle ti should be underscored. Inflector.Net.Inflector.Underscore works fine. But I do not know how to write the convention.
Somethig like:
(conventions.GetPropertyName = type => Inflector.Net.Inflector.Underscore(type.ColumnName))
Thanks
I'm trying to use NHibernate for data access, and I have 2 simple entities that look like this:
public class User : IEntity
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Logon { get; set; }
public string Password { get; set; }
public ICollection<Role> Roles { get; set; }
public bool IsNew
{
get { return (ID == 0) ? true : false; }
}
public User()
{
Roles = new List<Role>();
}
}
public class Role : IEntity
{
public int ID { get; set; }
public string RoleName { get; set; }
public string RoleDescription { get; set; }
public bool IsNew
{
get { return (ID == 0) ? true : false; }
}
}
My question......how do I construct a Criteria if I want to find any user that contains a Role with an ID of 1 in it's Roles collection?
Never mind, this ended up being relatively straight forward to do:
// role was the role passed in to my Find method.
var criteria = DetachedCriteria.For(typeof (User))
.CreateCriteria("Roles")
.Add(Restrictions.Eq("ID", role.ID));