Handling document relationships with T[] instead of T using RavenDB - ravendb

RavenDB docs show how to deal with document relationships in this sample using Includes.
public class Order
{
public Product[] Items { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public string[] Images { get; set; }
public double Price { get; set; }
}
public class Customer
{
public string Name { get; set; }
public string Address { get; set; }
public short Age { get; set; }
public string HashedPassword { get; set; }
}
How would I deal with Includes or Live Projections if I don't want to include the customer using Includes/Live Projections but a list of products instead:
public class Order
{
public string[] ItemIds { get; set; }
public string CustomerId { get; set; }
public double TotalPrice { get; set; }
}

If I understand what you're asking, this should help. I blogged about it here:
http://inaspiralarray.blogspot.com/2012/03/keeping-domain-model-pure-with-ravendb.html
Does that help?

Related

How to show and search a list of products in asp.net core?

I would like to build these functionalities to a project using Asp.Net Core MVC.
Could someone please guide me through, How I can approach these steps:
View a list of product types for a given product category or for all categories.
I have created an ASP.NET Core MVC project with Identity authentication, where the user could register and log in.
I also have these Models created.
namespace Company.Models
{
public class ProductType
{
public ProductType()
{
Products = new List<Product>();
}
public long ProductTypeId { get; set; }
public string ProductName { get; set; }
public string ProductInfo { get; set; }
public string Location { get; set; }
public ProductTypeStatus Status { get; set; }
public string ImageUrl { get; set; }
public string Manufacturer { get; set; }
public string AdminComment { get; set; }
public Category Categories { get; set; }
public ICollection<Product> Products { get; protected set; }
}
public enum ProductTypeStatus
{
Available,
ReservedAdmin
}
public enum ProductStatus
{
Available,
ReservedLoaner,
ReservedAdmin,
Loaned,
Defect,
Trashed,
Lost,
NeverReturned
}
namespace Company.Models
{
public class Product
{
public long ProductId { get; set; }
public long ProductTypeId { get; set; }
public int ProductNumber { get; set; }
public string SerialNo { get; set; }
public ProductStatus Status { get; set; }
public string AdminComment { get; set; }
public string UserComment { get; set; }
public long? CurrentLoanInformationId { get; set; }
}
}
namespace Company.Models
{
public class Category
{
public Category()
{
ProductTypes = new List<ProductType>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public ICollection<ProductType> ProductTypes
{
get; protected set;
}
}
I have recently turned to Asp.Net Core MVC. So this is a new envirnoment for me to get startd. Though, I did follow the msdn tutorials on asp.net mvc.
I APPRECIATE any help!
I saw your model design I think you missing 1 small thing that is relationship between Product and Category.
1 Product will be in 1 Category
So to add 1 to 1 relationship you need to adjust your model like this. You can view more here
namespace Company.Models
{
public class Product
{
public long ProductId { get; set; }
public long ProductTypeId { get; set; }
public int ProductNumber { get; set; }
public string SerialNo { get; set; }
public ProductStatus Status { get; set; }
public string AdminComment { get; set; }
public string UserComment { get; set; }
public long? CurrentLoanInformationId { get; set; }
public Category Category { get;set; }
}
}
namespace Company.Models
{
public class Category
{
public Category()
{
ProductTypes = new List<ProductType>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public ICollection<ProductType> ProductTypes
{
get; protected set;
}
}
}
So when you update your model you will need to run ef migration to apply change to db. Detail can be found here
And finally you need to write the code to query some thing like
var query = _db.Product.Where(x => x.Category == "Book");
You can read how to write ef query in c# here

Missing type map configuration or unsupported mapping.for Collection of DTO

I was making an API for saving a model where it has one to many relationship with another model. When I applying automapping in it. it is giving me following error:
CreateContestDto -> Contest
tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest
Type Map configuration:
CreateContestDto -> Contest
tritronAPI.DTOs.CreateContestDto -> tritronAPI.Model.Contest
Destination Member:
Problems
---> AutoMapper.AutoMapperMappingException: Missing type map
configuration or unsupported mapping.
Mapping types:
ProblemDto -> Problem
tritronAPI.DTOs.ProblemDto -> tritronAPI.Model.Problem
at lambda_method(Closure , ProblemDto , Problem , ResolutionContext )
My models are: Contest and Problem a contest contain many problems:
public class Contest
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public ICollection<Problem> Problems { get; set; }
public ICollection<ContestProgrammingLanguage>
ContestProgrammingLanguages { get; set; }
}
public class Problem
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(255)]
public string ProblemName { get; set; }
[ForeignKey("User")]
public string ProblemAuthorId { get; set; }
public virtual User ProblemAuthor { get; set; }
public string AuthorName { get; set; }
//public virtual List<Resources> Resourceses { get; set; }
public string ProblemDescription { get; set; }
public bool IsPublished { get; set; }
public virtual ICollection<Submission> Submissions { get; set; }
public string Tags { get; set; }
//public Guid Contest_Id { get; set; }
public virtual Contest Contest { get; set; }
[ForeignKey("Contest")]
public int? Contest_Id { get; set; }
public short Score { get; set; }
//Timelimit in miliseconds
public int TimeLimit { get; set; }
//MemoryLimit in bytes
public int MemoryLimit { get; set; }
//More than source code limit is not allowed
public int? SourceCodeLimit { get; set; }
public virtual ICollection<TestFile> TestFiles { get; set; } = new
List<TestFile>();
}
public class CreateContestDto
{
public CreateContestDto()
{
this.Problems = new HashSet<ProblemDto>();
}
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndDate { get; set; }
public DateTime EndTime { get; set; }
public string BackgroundImage { get; set; }
public string Description { get; set; }
public ICollection<ProblemDto> Problems { get; set; }
}
public class ProblemDto
{
public int Id { get; set; }
public string ProblemName { get; set; }
}
mapping profile:
CreateMap<CreateContestDto, Contest>().ForMember(
dest => dest.Problems , opt => opt.MapFrom(src =>
src.Problems));
controller code:
public async Task<IActionResult> AddContest([FromBody]
CreateContestDto contest)
{
var con = _mapper.Map<Contest>(contest);
this._uow.ContestRepository.Add(con);
return Ok();
}
I have already tried with reversemap selecting new id in mapping profile
You also need to add mappings for ProblemDTO to Problem:
CreateMap<CreateContestDto, Contest>();
CreateMap<ProblemDto, Problem>();

Asp Core Multiple Entity Relationships

I am working on modeling a Contact Info Structure and haven't quite figured out how the relationships should be coded with EF Core. I am fairly new to using EF for data access layer.
I want to have a contact model which can contain Website, Phonenumbers, Emails, or Social Info. Then the contact info will be added to several different models. Any suggestions would be helpful, I am not sure how code this One to many with many table relationship or if it is even possible using EF.
Models so far
public class Contact
{
public String Id { get; set; }
public Int32 ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
public String RecId { get; set; } //FK to multiple Models
public String RecType { get; set; }//Value for which model the RecID is for
public String Name { get; set; }
public String Value { get; set; }
}
public class ContactInfo
{
public virtual IList<Contact> Website { get; set; }
public virtual IList<Contact> PhoneNumbers { get; set; }
public virtual IList<Contact> Emails { get; set; }
public virtual IList<Contact> Socials { get; set; }
}
//Example of models to use the contact model
public class Company
{
....
pubic ContactInfo ContactInfo { get; set;}
}
public class Client
{
....
pubic ContactInfo ContactInfo { get; set;}
}
If I understand your question correctly, then you could use following code sample, but it is not exactly what you are trying to achieve. This may give you some understanding what you need to do with EF.
public class Contact
{
public String Id { get; set; }
public ContactType ContactType { get; set; } //Enum for Website, Phonenumbers, Emails, or Social
public String RecId { get; set; } //FK to multiple Models (This can't be the FK to multiple table as it should be FK for one table so that FK for Company would be CompanyId, FK for the Client should ClientId)
public String RecType { get; set; }//Value for which model the RecID is for (This need to rethink as it may not needed.)
public String Name { get; set; }
public String Value { get; set; }
// One to Many Relationship
public string CompanyId? { get; set; }
public string ClientId? { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class Company
{
public String Id { get; set; }
// Other properties
// One to Many Relationship
public ICollection<Contact> Contacts { get; set; }
}
public class Client
{
public String Id { get; set; }
// Other properties
// One to Many Relationship
public ICollection<Contact> Contacts { get; set; }
}
/* Db context */
public class YourDbContext : DbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options)
: base(options)
{
}
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<Company> Companies { get; set; }
public virtual DbSet<Client> Clients { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Contact>().HasKey(t => t.Id);
modelBuilder.Entity<Company>().HasKey(t => t.Id);
modelBuilder.Entity<Company>().HasMany(c => c.Contacts).WithOne(c => c.Company).HasForeignKey(k => k.CompanyId);
modelBuilder.Entity<Client>().HasKey(t => t.Id);
modelBuilder.Entity<Client>().HasMany(t => t.Contacts).WithOne(c =>c.Client).HasForeignKey(k => k.ClientId);
}
}
/* Db context - Endd */
public enum ContactType
{
Website,
PhoneNumbers,
Emails,
Social
}
Let me know if you need anymore information.
With the help from DSR, this is the solution I have (untested).
public class Company
{
public String Id { get; set; }
public String Name { get; set; }
public ICollection<ContactPhone> PhoneNumbers { get; set; }
public ICollection<ContactEmail> ContactEmail { get; set; }
public ICollection<ContactWebsite> ContactWebsite { get; set; }
public ICollection<ContactSocial> ContactSocial { get; set; }
}
public class Client
{
public String Id { get; set; }
public String Name { get; set; }
public ICollection<ContactPhone> PhoneNumbers { get; set; }
public ICollection<ContactEmail> ContactEmail { get; set; }
public ICollection<ContactWebsite> ContactWebsite { get; set; }
public ICollection<ContactSocial> ContactSocial { get; set; }
}
public class ContactWebsite
{
public String Id { get; set; }
public String Url { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactPhone
{
public String Id { get; set; }
public String Type { get; set; }
public String Number { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactEmail
{
public String Id { get; set; }
public String Category { get; set; }
public String Email { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}
public class ContactSocial
{
public String Id { get; set; }
public String Site { get; set; }
public String Handle { get; set; }
public Company Company { get; set; }
public Client Client { get; set; }
}

Adding checkboxes in MVC4

I have searched and was unable to find a way to display checkboxes in my view. I have 2 entities: Assignment and Chore which is a one-to-many relationship (one assignment, many chores)
public class Chore
{
public System.Guid ChoreId { get; set; }
public string ChoreName { get; set; }
public string Description { get; set; }
public int PointValue { get; set; }
}
public class Assignment
{
public int AssignmentId { get; set; }
public System.Guid RecipientId { get; set; }
public Nullable<bool> Completed { get; set; }
public System.DateTime AssignedDate { get; set; }
public Nullable<System.DateTime> CompletedDate { get; set; }
public virtual IEnumerable<Chore> Chores { get; set; }
}
How can I display a list of all possible chores That I can select whenever I want to create an assignment?

How do I construct my RavenDb static indexes for this document, given these requirements?

I have close to 3 million documents that are being stored in a RavenDb embedded instance. Everyone of the fields will be subjected to some type of filter/query/sorting, but in particular I wanted to do some type of intelligent textual search of the info and info2 columns. How might I go about constructing the RavenDb Indexes?
My first pass at the info2 column looks like this.
store.DatabaseCommands.PutIndex("ProdcustByInfo2", new IndexDefinitionBuilder<Product>
{
Map = products => from product in products
select new { product.INFO2 },
Indexes = { { x => x.INFO2, FieldIndexing.Analyzed } }
});
Thank you,
Stephen
[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; }
}
Spaten,
Create a single RavenDB index, which output all of the properties that you are interested in querying on.
Mark the Info2 and Info as full text search (Analyzed).
You are pretty much done.