How to fetch one column from another table using Fluent NHibernate - c#

I have a Products table:
ProductId
ProductDescription
CategoryId
And a Categories table:
CategoryId
CategoryDescription
***For every product, I would like to display a line like so:
Product Id | Product Description | Category Description
I have not been successful in forming the necessary mapping that is required for the above task.
Products Mapping I am using:
public ProductsMap()
{
Table("Products");
Id(x => x.ProductId);
Map(x => x.ProductDescription);
Map(x => x.CategoryId);
References(x => x.Categories)
.Column("CategoryId")
.Not.Nullable();
// Need Join() statement here?
...
My Products class:
public class Products
{
public virtual int ProductId { get; set; }
public virtual string ProductDescription { get; set; }
public virtual int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual int? CategoryDescription { get; set; } // Not in the db table.
}
My goal is to have the CategoryDescription field in the above class to be populated automatically by Fluent-NHibernate through the mapping specified.
I used the join statement suggested by this answer but I got various exceptions for the following statement:
List<Products> products = session.Query<Products>().ToList();
Note: I can pull in all products from the database without the corresponding column in the Categories table, so I know that my database connectivity is good, and that the basic functionality of the application is sound.
I am new to Fluent-NHibernate, have invested quite a bit of time on this, but feel I am not getting anywhere. I would appreciate some directed guidance.

I'm a little confused because you seem to mixing singular and plural, but I would create separate domain mappings for the product and category
public class Product
{
public virtual int ProductId { get; set; }
public virtual string ProductDescription { get; set; }
public virtual Category Category { get; set; }
}
public class Category
{
public virtual int CategoryId { get; set; }
public virtual string CategoryDescription { get; set; }
}
map them the way you are mapping in the question, then create a view model
public class ProductViewModel
{
public virtual int ProductId { get; set; }
public virtual string ProductDescription { get; set; }
public virtual string CategoryDescription { get; set; }
}
that gets populated with this query
var products = session.Query<Products>().Select(p => new ProductViewModel()
{
ProductId = p.ProductId,
ProductDescription = p.ProductDescription,
CategoryDescription = p.Category.CategoryDescription
});
This will produce a query that only returns the columns you need. If you return full entities, you are going to return information you don't need.

Related

How to save collection of foreign keys in code first

Update: At this moment I can assign only one product to Recipe. What I want to do is add access to all products from db in recipe (controller create) - here Im using public int ProductId but it allow to save only one product. I want to choose a few products from this list and save foreign keys in database. photo
I also tried add public List < int > in ProductId but I got error from entity framework.
I will be grateful for any help.
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int ProductId { get; set; }
public List<Product> Products { get; set; }
public Recipe()
{
this.Products = new List<Product>();
}
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Recipe? Recipes { get; set; }
}
If you want to create a one-to-many relationship you are almost in the correct direction, but you should remove the public int ProductId { get; set; } and re-arrange as like as below example.
Say you have the following classes:
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public List<Product> Products { get; set; } = new();
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public Recipe Recipe { get; set; }
}
You can instantiate and use as per below:
public static void Main()
{
var recipe = new Recipe
{
Name = "My Recipe",
Products = new List<Product>
{
new Product { Name = "Product 1" },
new Product { Name = "Product 2" },
new Product { Name = "Product 3" }
}
};
recipe.Products.ForEach(product =>
{
Console.WriteLine(product.Name);
});
}
It sounds like you are looking for a many-to-many relationship rather than a one-to-many.
If you are using Code-First and EF6 or EF Core 5+ then you can use:
public class Recipe
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; } = new List<Product>();
}
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; } = new List<Recipe>();
}
To understand what is happening behind the scenes, EF should create a joining table called ProductRecipe or RecipeProduct which contains two FKs. ProductId and RecipeId. These will also form a composite PK for this table. Using this table, EF can associate one product to several recipes while also associating one recipe to the various products. In the object model you get the collection of products for each recipe, and the collection of recipes for each product.
With earlier versions of EF Core you were limited to having to declare this linking entity so you would have something more like:
public class Recipe
{
// ...
public virtual ICollection<ProductRecipe> ProductRecipes { get; set; } = new List<ProductRecipe>();
}
public class Product
{
// ...
public virtual ICollection<ProductRecipe> ProductRecipes { get; set; } = new List<ProductRecipe>();
}
public class ProductRecipe
{
[Key, Column(Order = 0)]
public int ProductId { get; set; }
[Key, Column(Order = 1)]
public int RecipeId { get; set; }
public virtual Product Product { get; set; }
public virtual Recipe Recipe { get; set; }
}
This approach is still an option in the other versions of EF, and is required if you want to support adding any additional fields to the joining table. For instance if you want to track things like CreatedDate/ModifiedDate etc. to record when a product was associated to a recipe etc. To expose that information to an application through EF, EF needs to know about the ProductRecipe as an entity. The trade off is that this approach is "messier" when usually you care about the Products for a Recipe etc. To get a list of products for a recipe you would need:
var products = context.Products
.Where(p => p.ProductRecipes.Any(pr => pr.RecipeId == recipeId)
.ToList();
or
var products = context.Recipies
.Where(r => r.RecipeId == recipeId)
.SelectMany(r => r.ProductRecipies.Select(pr => pr.Product).ToList())
.ToList();
vs. the implied joining table in the first approach:
var produts = context.Recipes
.Where(r => r.RecipeId == recipeId)
.SelectMany(r => r.Products)
.ToList();
... which is arguably easier to read.

Sharing a table using Entity Framework Core

I have multiple entities that I would like to share a single "Images" table. For example, products can have a list of images and categories can have a list of images. I would like to use the enum "EntityType" to distinguish what type of entity it is. My solution below doesn't work because there is a foreign key error when I try to insert an image with a EntityId that might exist in Category but not in Product. This makes sense because the solution below isn't taking into account the "EntityType". Are there any recommendations for how I can accomplish this? I know I can use "ProductId", "CategoryId", etc instead of "EntityId" but I will have a lot of entities so I would prefer to not to do it that way.
public class Product
{
public int Id { get; set; }
public List<Image> ProductImages { get; set; }
}
public class Category
{
public int Id { get; set; }
public List<Image> CategoryImages { get; set; }
}
public class Image
{
public int EntityId { get; set; }
public EntityType EntityType { get; set; }
public string ImageUrl { get; set; }
public Product Product { get; set; }
public Category Category { get; set; }
}
modelBuilder.Entity<Product>().ToTable("Product");
modelBuilder.Entity<Category>().ToTable("Category");
modelBuilder.Entity<Image>().ToTable("Image");
modelBuilder.Entity<Image>().HasOne(p => p.Product).WithMany(p => p.ProductImages).HasForeignKey(p => p.EntityId);
modelBuilder.Entity<Image>().HasOne(p => p.Category).WithMany(p => p.CategoryImages).HasForeignKey(p => p.EntityId);
What you're describing is a many-to-many relationship. For that, you'll need an entity to track said relationship:
public class ProductImage
{
[ForeignKey(nameof(Product))]
public int ProductId { get; set; }
public Product Product { get; set; }
[ForeignKey(nameof(Image))]
public int ImageId { get; set; }
public Image Image { get; set; }
}
On your Product/Category classes:
public ICollection<ProductImage> ProductImages { get; set; }
Then, for your fluent config:
modelBuilder.Entity<ProductImage>().HasOne(p => p.Product).WithMany(p => p.ProductImages);
modelBuilder.Entity<ProductImage>().HasOne(p => p.Image).WithMany();
Do the same with your categories.

How to insert record with many to many relational tables

work on asp.net mvc5 ef-6.Face problem with many to many relational table data insertion.My classes models are
public class Product
{
public long ProductID { get; set; }
public string ProductName { get; set; }
//navigation property to Supplier
public virtual ICollection<ProductSupplier> ProductSupplier { get; set; }
}
public class Supplier
{
public long SupplierID { get; set; }
public string SupplierName { get; set; }
// navigation property to Product
public virtual ICollection<ProductSupplier> ProductSupplier { get; set; }
}
public class ProductSupplier
{
public long ProductID { get; set; }
public long SupplierID { get; set; }
public virtual Product Product { get; set; }
public virtual Supplier Supplier { get; set; }
public bool IsActive { get; set; }
}
How to insert records on above classes.Will first i need to insert on Product then Supplier then ProductSupplier.
You essentially just have a M2M with a payload here. With that, you'll need to set the Product/Supplier on ProductSupplier before saving that relationship. So something along these lines:
var product = new Product();
var supplier = new Supplier();
var productSupplier = new ProductSupplier
{
Product = product,
Supplier = supplier
};
db.ProductSuppliers.Add(productSupplier);
db.SaveChanges();
For simplicity, I only dealt with the relationships here. Obviously you'd want to intialize/add the other data on the entities. Also, note that it's only necessary to explicitly add the ProductSupplier instance to its DbSet. By virtue of being attached to that instance, the Product and Supplier instances will also be added and saved. You can of course still do that explicitly, instead, if you like.

asp.net mvc - Inserting multiple records in the third table having foreign keys to parent using entity framework 6

I am trying to learn how to use Entity Framework 6 with an already created database, without creating an .edmx file, i.e, using the DbContext and POCO classes.
These are my model classes:
[Table("Category")]
public class Category
{
[Key]
public long CategoryID { get; set; }
public string CategoryName { get; set; }
}
[Table("RegistrationForm")]
public class RegistrationForm
{
[Key]
public int RegistrationID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Country { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}
[Table("RegistrationCategory")]
public class RegistrationCategory
{
[Key]
public long RegistrationCategory { get; set; }
public long RegistrationID { get; set; }//Foreign key to RegistrationID in RegistrationForm table in database
public long CategoryID { get; set; }//Foreign key to CategoryID in Category table in database
}
My DbContext class:
public class MyContext : DbContext
{
public virtual DbSet<RegistrationForm> RegistrationForm { get; set; }
public virtual DbSet<Category> Category { get; set; }
public virtual DbSet<RegistrationCategory> RegistrationCategory { get; set; }
}
Here I want to use the default model builder of DbContext.User can select multiple categories in the registration screen so the RegistrationCategory table will have multiple records for each registration. Therefore RegistrationForm and RegistrationCategory are in a one-to-many relationship.
How to write foreign key mappings between the above mentioned models?
How to bind data from Category table data in the mvc view(listbox) so that we can save one record in RegistrationForm table and multiple records in RegistrationCategory table without using loops (using mappings between the c# models) in Entity Framework 6?
The database schema that you have here is a Many to Many relationship between RegistrationForm and Category, with a join table. The RegistrationCategory Table is not necessary to be modeled in Entity Framework at all. You will need to use Entity Framework Fluent API to generate the correct mappings.
First, your RegistrationForm Table:
public class RegistrationForm
{
[Key]
public int RegistrationID { get; set; }
...
// add a navigation property ICollection<Category> to reference the categories
public virtual ICollection<Category> Categories { get; set; }
}
Next, the Category class:
public class Category
{
[Key]
public int CategoryID { get; set; }
public string CategoryName { get; set; }
//Navigation property to reference the RegistrationForms
public virtual ICollection<RegistrationForm> RegistrationForms { get; set; }
}
next, in your DbContext: note the change in pluralization, and the removal of the RegistrationCategory, you do not need a model class for it at all.
public class MyContext : DbContext
{
public virtual DbSet<RegistrationForm> RegistrationForms { get; set; }
public virtual DbSet<Category> Categories { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<RegistrationForm>()
.HasMany(r => r.Categories)
.WithMany(c => c.RegistrationForms)
.Map(
m =>
{
m.MapLeftKey("RegistrationID");
m.MapRightKey("CategoryID");
m.ToTable("RegistrationCategory");
}
);
}
With this in place, you can now query all the Categories of a RegistrationForm or all the RegistrationForms of a Category.
foreach (var category in registrationForm.Categories)
{
//do whatever with each category
}

Mapping child items of same class with Entity Framework Code First

I'm trying to map a fairly "standard" category model using EF Code First
public class Category
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public Category ParentCategory { get; set; }
public List<Category> ChildCategories { get; set; }
}
I've got something along the lines of:
modelBuilder.Entity<Category>()
.HasOptional(t => t.ParentCategory)
.WithMany()
.HasForeignKey(t => t.ParentCategoryID)
.WillCascadeOnDelete();
But this doesn't seem to take care of ChildCategories??
Am I missing something?
To avoid the duplicate question argument, I followed the following, however didn't quite answer my specific query:
Code First Mapping for Entity Framework Hierarchy
Entity Framework CTP5 Code-First Mapping - Foreign Key in same table
Change your Entity to
public class Category
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Name { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual IList<Category> ChildCategories { get; set; }
}
Make ParentID nullable and to allow ChildCategories to be lazy loaded, make it virtual.

Categories

Resources