Dear all I would like to ask a question I am having one property table in which I have (Id,Name,price,TypeID) TypeID is integer here and basically I have property type another table (id,type_name)
now when i am binding my viewmodel like
MYWM.Property=dbcontext.property.ToLuist(), it get all columns values from property table but the issue is for propertyID here i have id which I need to show property type name from property type table how to us include and add that column as well based in TypeID, in this line bcontext.property.ToLuist(), remember i have no forieng key specify only the matching thing is i have property type id in property table which mades with ID column of property type table

Tahir AnsariRank #366 · 6.8K reputationPosted Aug 7, 2024, 11:11 AM EDT
Assume you have two entities: Property and PropertyType
public class Property { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int TypeId { get; set; } // Foreign key // Navigation property to PropertyType public PropertyType PropertyType { get; set; } }public class PropertyType { public int Id { get; set; } public string TypeName { get; set; } }public class AppDbContext : DbContext { public DbSet Properties { get; set; } public DbSet PropertyTypes { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // Configure your database connection string optionsBuilder.UseSqlServer("your_connection_string_here"); } }Fetching Data with Navigation Property
public async Task> GetPropertiesWithTypeNameAsync() { using (var dbContext = new AppDbContext()) { var properties = await dbContext.Properties .Include(p => p.PropertyType) // Include PropertyType entity .Select(p => new PropertyViewModel { Id = p.Id, Name = p.Name, Price = p.Price, TypeName = p.PropertyType.TypeName // Access TypeName from PropertyType }) .ToListAsync(); return properties; } }Create a ViewModel (PropertyViewModel.cs) to hold the data you want to display in your view
public class PropertyViewModel { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public string TypeName { get; set; } }Assume you have an ASP.NET Core MVC Controller where you want to use this method
public class PropertyController : Controller { private readonly AppDbContext _dbContext; public PropertyController(AppDbContext dbContext) { _dbContext = dbContext; } public async Task Index() { var properties = await GetPropertiesWithTypeNameAsync(); return View(properties); } public async Task> GetPropertiesWithTypeNameAsync() { var properties = await _dbContext.Properties .Include(p => p.PropertyType) // Include PropertyType entity .Select(p => new PropertyViewModel { Id = p.Id, Name = p.Name, Price = p.Price, TypeName = p.PropertyType.TypeName // Access TypeName from PropertyType }) .ToListAsync(); return properties; } }Assuming you have a view to display the properties (Index.cshtml)
@model List Properties Id Name Price Type Name @foreach (var property in Model) { @property.Id @property.Name @property.Price @property.TypeName }I hope this example resolved your issue
Happy coding!
Accept the answer if it is resolved your issue