The Include method is missing on an EntityFramework Linq Query - c#

Models
public class CreamModel
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int? Type_Id { get; set; }
public CreamTypeModel CreamType { get; set; }
}
public class CreamTypeModel
{
[Key]
public int Id { get; set; }
public string Type { get; set; }
}
DbContext
internal class CreamEFDbContext : DbContext
{
public DbSet<CreamTypeModel> CreamTypeModels { get; set; }
public DbSet<CreamModel> CreamModels { get; set; }
}
SQL query
Second table
In my repository I want to take the list of creams and their type using the Include method but it's missing.
In my last project, the same method was available and working but now it isn't.
public class CreamRepository : ICreamRepository
{
private CreamEFDbContext context = new CreamEFDbContext();
public IEnumerable<CreamModel> CreamList
{
get { return context.CreamModels.Include(x => x.CreamType); }//have red line
}
}
The following images show what I have in my current project vs the old project where the Include method worked:
Current Project
Last Project

Include is an extension method from the QueryableExtensions class in the EntityFramework DLL. So you need to add a using statement to System.Data.Entity:
using System.Data.Entity;

Related

Is it possible to add array of data in child entity?

I have issue that I hope someone here will be able to help me.For this exercise I`m using aspnetboilerplate framework.
So I create 3 entity and they ALL work good (at least I guess they work good)
Little bit recap of project
One Recepie HAVE one or more Ingredient
Ingrident.cs
public class Ingrident : Entity
{
public string Name { get; set; }
public Master Master { get; set; }
public int? MasterId { get; set; }
public Recepie Recepie { get; set; }
public int? RecepieId { get; set; }
}
Master.cs
public class Master : Entity
{
public string Name { get; set; }
public string? Image { get; set; }
public string? ShortDescription { get; set; }
public string? FullDescription { get; set; }
public string? Keywords { get; set; }
public string Slug { get; set; }
public int? Count { get; set; }
public List<Ingrident> Ingridents { get; set; }
}
Recepie.cs
public class Recepie : Entity
{
public string RecepieName { get; set; }
public Ingrident Ingridents { get; set; }
}
With this database structure I can add Recepie and add only one ingredient when I try to send [] of Ingredient its show me DTO error.
And here is RecepieAppService.cs
public class RecepieAppService : AsyncCrudAppService<IndexIngrident.Entities.Recepie,RecepieDto>
{
private readonly IRepository<IndexIngrident.Entities.Recepie> _repository;
private readonly IRepository<IndexIngrident.Entities.Ingrident> _ingRepository;
public RecepieAppService(IRepository<IndexIngrident.Entities.Recepie> repository, IRepository<IndexIngrident.Entities.Ingrident> ingRepository)
: base(repository)
{
_repository = repository;
_ingRepository = ingRepository;
}
public List<RecepieFullGetDto> GetAllIncluded()
{
var result = _repository.GetAllIncluding(x => x.Ingridents , x => x.Ingridents.Master);
Debug.WriteLine(result);
return ObjectMapper.Map<List<RecepieFullGetDto>>(result);
}
}
RecepieDto.cs
[AutoMap(typeof(IndexIngrident.Entities.Recepie))]
public class RecepieDto : EntityDto
{
public string RecepieName { get; set; }
public IngridentRecepieDto Ingridents { get; set; }
}
public class IngridentRecepieDto : EntityDto
{
public string Name { get; set; }
public int RecepieId { get; set; }
}
Using AsyncCrudAppService my CRUD is automatically generated and I`m able to create new Recepie but when I try to do something like this
I get error
you need to add the mapping of objects in both directions, [AutoMapTo (typeof (RecepieDto))] and [AutoMapFrom (typeof (Recepie))], and verify that you have the class, inherits AutomapperProfile

Entity Framework 6 - Database schema is not updating

I am learning EF
I wanted to play with the different types of inheritance using code first.
I initially started off with a few classes and ran my application. I saw the database get created with all my classes represented as tables.
However, when I add new classes or fields and run the application again, I do not see the changes in my database schema.
I have used the "DropCreateDatabaseAlways", so I don't understand why my database is not being updated with the proper schema as I add fields and classes. Can someone explain what I am doing wrong?
Initial Code:
namespace Domain
{
public class Good
{
public int Id { get; set; }
public string Name { get; set; }
public double BaseValue { get; set; }
}
public class Manufacturer {
public int Id {get; set;}
public ICollection<ManufacturingCapability> ManufacturingCapabilities { get; set; }
}
public class ManufacturingCapability {
public int Id { get; set; }
public ICollection<ManufacturingInput> Inputs { get; set; }
public ICollection<ManufacturingOutput> Outputs { get; set; }
public TimeSpan CycleTime { get; set; }
}
public class ManufacturingInput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
public class ManufacturingOutput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
public class ManufacturingDbContext :DbContext {
public DbSet<Good> Goods { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public ManufacturingDbContext() : base("name=EFLearnConnectionString") {
Database.SetInitializer<ManufacturingDbContext>(new DropCreateDatabaseAlways<ManufacturingDbContext>());
}
}
}
namespace EFLearning {
class Program {
static void Main(string[] args) {
using (var manufacturingDbContext = new ManufacturingDbContext()) {
var good = new Good() { Name = "Water" };
manufacturingDbContext.Goods.Add(good);
manufacturingDbContext.SaveChanges();
}
}
}
}
Database Tables in Management Studio after running:
Added code:
public class Station : Manufacturer {
public string Name { get; set; }
public Vector3 Location { get; set; }
}
and I added this to context:
public DbSet<Station> Stations { get; set; }
Database Tables in Management Studio after running:
Your strategy for domain design to station and manufactor entity is Table per Hierarchy.so stations properties are now in manufactors table.
go to this link for more information.
if you you want Station entity have a seperate table must use of Table per Type
go to this link for Table per Type

Error generating migration with EF and Dot Net Core 1 (PostgreSQL database)

I followed a tutorial by damienbod ( https://damienbod.com/2016/01/11/asp-net-5-with-postgresql-and-entity-framework-7/ ). I managed to get a connection to my PostgreSql database, and it creates the EntityMigration history table, the DataEventRecord table and the SourceInfo table. There are a total of three projects (two are needed for Postgre, and the third is my actual project): DataAccessPostgreSqlProvider, DomainModel, and iConnect.
I've created a Model, code below, and then execute: add-migration NestProtectDevices -Context NestProtectDevices
I have to specify the -Context or I get an error, and if I specify the -Context of DomainModelPostgreSqlContext, it just generates an empty migration file (as opposed to generating one from my model). That is why I specified the -Context as the Model name. Below are the code for the Model and the error I am getting. let me know if any other code would be needed to help in debugging.
using DataAccessPostgreSqlProvider;
using DomainModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using DomainModel.Model;
namespace iConnect.Models.Nest
{
public class NestProtectDevices : DomainModelPostgreSqlContext
{
public NestProtectDevices(DbContextOptions<DomainModelPostgreSqlContext> options) : base(options)
{
}
public long DeviceId { get; set; }
public long UserId { get; set; }
public int CompanyId { get; set; }
public long StructureId { get; set; }
public long WhereId { get; set; }
public string Name { get; set; }
public string NameLong { get; set; }
public bool IsOnline { get; set; }
public int BatteryHealth { get; set; }
public int CoAlarmState { get; set; }
public int SmokeAlarmState { get; set; }
public string UiColorState { get; set; }
public bool IsManualTestActive { get; set; }
public DateTime LastManualTestTime { get; set; }
public DateTime Timestamp { get; set;}
}
}
Command with error:
PM> add-migration NestProtectDevices -Context NestProtectDevices
No parameterless constructor was found on 'NestProtectDevices'. Either add a parameterless constructor to 'NestProtectDevices' or add an implementation of 'IDbContextFactory<NestProtectDevices>' in the same assembly as 'NestProtectDevices'.
using DataAccessPostgreSqlProvider;
using DomainModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using DomainModel.Model;
namespace iConnect.Models.Nest
{
public class NestProtectDevices : DomainModelPostgreSqlContext
{
public NestProtectDevices(DbContextOptions<DomainModelPostgreSqlContext> options) : base(options)
{
}
public DbSet<Device> Devices { get; set; }
}
public class Device
{
public long DeviceId { get; set; }
public long UserId { get; set; }
public int CompanyId { get; set; }
public long StructureId { get; set; }
public long WhereId { get; set; }
public string Name { get; set; }
public string NameLong { get; set; }
public bool IsOnline { get; set; }
public int BatteryHealth { get; set; }
public int CoAlarmState { get; set; }
public int SmokeAlarmState { get; set; }
public string UiColorState { get; set; }
public bool IsManualTestActive { get; set; }
public DateTime LastManualTestTime { get; set; }
public DateTime Timestamp { get; set;}
}
}
Unless this DataAccessPostgreSqlProvider is completely different than the regular Sql provider then I believe it should be set up more like this. By the way that link in your post is no good.
The error you're getting seems pretty helpful. Add a parameterless constructor to your class IN ADDITION TO the constructor you already have.
public NestProtectDevices()
{
}

MVC Model doesnt support ToPagedList

I have a model like this
public class SearchVM
{
[DisplayName("Type")]
public string Type { get; set; }
[DisplayName("Beds")]
public string NumberOfBeds { get; set; }
[DisplayName("Baths")]
public string NumberOfBaths { get; set; }
[DisplayName("Prices From")]
public string PriceFrom { get; set; }
[DisplayName("Prices To")]
public string PriceTo { get; set; }
public IEnumerable<Rental> Rentals { get; set; }
public IEnumerable<Sale> Sales { get; set; }
public IEnumerable<Rental> FeatureRentals { get; set; }
public IEnumerable<Sale> FeatureSales { get; set; }
public Rental NewRentals { get; set; }
public Sale NewSales { get; set; }
}
In my controller when trying to use this model SearchVM i cannot use the ToPagedList method
public ActionResult Search(SearchVM searchvm, int page = 1)
{
var query = from c in context.Rentals
select c;
searchvm.Rentals = query;
return View("RentProperty", query.ToPagedList(page,9));
}
I noticed that searchvm.ToPagedList doesnt work. Can some one please help
You are either missing a library reference in your code or if you already have that, you need to add a relevant using statement. For example, you may need this at the top of your code:
using PagedList;
Without this line, the compiler doesn't know where to get the ToPagedList() extension method.

Navigate properties when using Dapper Extensions

Im using DapperExtensions library for simple CRUD operations.
When I add a navigate property to my model, I get an error message that this column is not in the database. Can you in any way change this so that Dapper Extensions ignores this property?
Example of my model
public class Order : EntityBase
{
public int OrderId { get; set; }
public int MarketId { get; set; }
public int ModelId { get; set; }
public int ContactId { get; set; }
public string Project { get; set; }
public decimal Undertaking { get; set; }
public virtual Model Model { get; set; }
public virtual Contact Contact { get; set; }
}
Use the Write attribute above the property
[Write(false)]
add the package for dapperextentions
AutoMap(); will map all other properties as long as you have the same name for the field.
public class CustomMapper : DapperExtensions.Mapper.ClassMapper<Photo>
{
public CustomMapper()
{
Table("TableName if diffrent than the Model calss name");
Map(f => f.SomePropertyIDontCareAbout).Ignore();
AutoMap();
}
}

Categories