Include() in LINQ to Entities query - c#

I have the following models in my ASP.NET MVC 3 project:
public class Task
{
public int Id { get; set; }
public DateTime CreatedOn { get; set; }
public TaskStatus Status { get; set; }
}
public class TaskStatus
{
public int Id { get; set; }
public string Description { get; set; }
}
For reference, here is my DbContext class:
public class TaskManagerSets : DbContext
{
public DbSet<Task> TaskSet { get; set; }
public DbSet<TaskStatus> TaskStatusSet { get; set; }
}
Then I have a List Action in my TaskController:
TaskManagerSets dbcontext = new TaskManagerSets();
public ActionResult List()
{
var tasks = from tsk in dbcontext.TaskSet.Include("TaskStatusSet")
select tsk;
return View(tasks.ToList());
}
Finally I have the Task List View:
#model IEnumerable<TaskManager.Models.Task>
<ul>
#foreach (var tsk in Model)
{
<li>#tsk.Id | #tsk.CreatedOn | #tsk.Status.Description</li>
}
</ul>
When I execute my project I get the following error:
A specified Include path is not valid. The EntityType 'CodeFirstNamespace.Task' does not declare a navigation property with the name 'TaskStatus'.
The problem is definitely on the Include("TaskStatusSet") but how should I fix this?

The navigation property name in your Task class is Status. So, you would have to use:
var tasks = from tsk in dbcontext.TaskSet.Include("Status")
select tsk;
But since you are working with the DbContext API a better option is to use the type-safe overload of Include:
using System.Data.Entity;
// You must add a using statement for this namespace to have the following
// lambda version of Include available
//...
var tasks = from tsk in dbcontext.TaskSet.Include(t => t.Status)
select tsk;
You will get Intellisense and compile-time checks which helps to avoid issues with wrong strings like you had.

Related

How to avoid the circular referencing asp.net core mvc [HttpPost("add-recipe")] web api

My project is Application with Recipes (cooking) .NET Core 5.0
And i have problem with adding a new recipe (HttpPost) web api
On postman my response is:
"A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles."
When i'm creating a new recipe it should use recipeToCreateDto instead of Recipe - which contains all properties (circular referencing)
Could you help me how to make it working properly. How to map etc.
https://i.postimg.cc/Mphv7zRH/screen.png <- screen here
I'm using AutoMapper for mapping classes and Repository Pattern.
public class AppUser
{
public int Id { get; set; }
public string UserName { get; set; }
public ICollection<Recipe> Recipes {get; set;}
}
}
User has many recipes.
public class Recipe
{
public int Id { get; set; }
public string Name { get; set; }
public AppUser AppUser { get; set; }
public int AppUserId { get; set; }
}
Data Transfer Object
public class RecipeForCreateDto
{
[Required]
[StringLength(50, MinimumLength = 3, ErrorMessage = "You must specify name between 3 and 50 characters")]
public string Name { get; set; }
public int AppUserId { get; set; }
public int AuthorId { get; set; }
}
In my AutoMapperProfiles.cs
public class AutoMapperProfiles : Profile
{
public AutoMapperProfiles()
{
CreateMap<RecipeForCreateDto, Recipe>();
}
Recipe Interface
public interface IRecipeRepository
{
Task<Recipe> AddNewRecipe(Recipe recipe);
}
public class RecipeRepository : IRecipeRepository
{
private readonly DataContext _context;
private readonly IMapper _autoMapper;
public RecipeRepository(DataContext context, IMapper autoMapper)
{
_autoMapper = autoMapper;
_context = context;
}
public async Task<Recipe> AddNewRecipe(Recipe recipe)
{
await _context.Recipes.AddAsync(recipe);
await _context.SaveChangesAsync();
return recipe;
}
}
Users Controller:
User.GetUsername() is static method that is getting User's username.
[HttpPost("add-recipe")]
public async Task<ActionResult> AddNewRecipe(RecipeForCreateDto recipeForCreateDto)
{
var userFromRepo = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
recipeForCreateDto.Name = recipeForCreateDto.Name.ToLower();
if (await _recipeRepository.RecipeExists(recipeForCreateDto.Name))
return BadRequest("Recipe with that name already exists!");
var recipeToCreate = _autoMapper.Map<Recipe>(recipeForCreateDto);
recipeToCreate.AppUserId = userFromRepo.Id;
var createdRecipe = await _recipeRepository.AddNewRecipe(recipeToCreate); // here is problem
var recipeToReturn = _autoMapper.Map<RecipeForDetailDto>(createdRecipe);
return CreatedAtRoute("GetRecipe", new { controller = "Recipes", id = createdRecipe.Id }, recipeToReturn);
}
"A possible object cycle was detected. This can either be due to a
cycle or if the object depth is larger than the maximum allowed depth
of 32. Consider using ReferenceHandler.Preserve on
JsonSerializerOptions to support cycles."
For this issue , you can add the following code in startup.cs ConfigureServices method:
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
[HttpPost("{recipeForCreateDto}")]
public async Task < ActionResult > AddNewRecipe([FromBody] RecipeForCreateDto recipeForCreateDto) {
var userFromRepo = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
recipeForCreateDto.Name = recipeForCreateDto.Name.ToLower();
if (await _recipeRepository.RecipeExists(recipeForCreateDto.Name)) return BadRequest("Recipe with that name already exists!");
var recipeToCreate = _autoMapper.Map < Recipe > (recipeForCreateDto);
recipeToCreate.AppUserId = userFromRepo.Id;
var createdRecipe = await _recipeRepository.AddNewRecipe(recipeToCreate); // here is problem
var recipeToReturn = _autoMapper.Map < RecipeForDetailDto > (createdRecipe);
return CreatedAtRoute("GetRecipe", new {
controller = "Recipes",
id = createdRecipe.Id
},
recipeToReturn);
}
It is important to note that the default serializer in .Net is now from the System.Text.Json.Serialization namespace. This serializer actually does a great job of both serializing and deserializing circular references.
To turn this feature on place the following code in your Startup.cs:
services
.AddControllers()
.AddJsonOptions(c => c.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
Documented here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-preserve-references

Copying data between models and saving children without entities duplicating themselves in Entity Framework

I am having trouble saving children entities via Entity Framework / ASP Identity. It seems to be adding duplicates of everything that is added.
I have tried using a detached graph of the DrivingLicenceModel by TeamMember.DrivingLicence = null in the TeamMemberModel and then working with a detached graph by looking if there is new or old DrivingLicenceCategories but because DrivingLicence links back to TeamMember it causes TeamMember.DrivingLicenceId to be null as it cannot link back to TeamMember.
I have tried Manually adding the EntityState to the DrivingLicence and DrivingLicenceCategories but when I do that it complains that it cannot save two entities with the same primary key.
I assume this is because they way I am copying the entities but I after a lot of looking I am drawing a blank.
If there anyway to copy from TeamMemberRequestModel to TeamMemberModel and then save without the children trying to create clone copies of themselves?
Models
public class TeamMemberModel : IdentityUser
{
public virtual DrivingLicenceModel DrivingLicence { get; set; }
public void ShallowCopy(TeamMemberRequestModel src)
{
this.DateOfBirth = src.DateOfBirth;
if (src.DrivingLicence != null)
{
if (this.DrivingLicence == null)
{
this.DrivingLicence = new DrivingLicenceModel(src.DrivingLicence);
}
else
{
this.DrivingLicence.ShallowCopy(src.DrivingLicence);
}
}
}
public TeamMemberModel() { }
}
public class DrivingLicenceModel
{
[Key]
public int Id { get; set; }
[ForeignKey("TeamMember")]
public string TeamMemberId { get; set; }
[JsonIgnore]
public TeamMemberModel TeamMember { get; set; }
public virtual List<DrivingLicenceCategoryModel> DrivingLicenceCategories { get; set; }
public DrivingLicenceModel() { }
public DrivingLicenceModel(DrivingLicenceModel src)
{
this.ShallowCopy(src);
}
public void ShallowCopy(DrivingLicenceModel src)
{
this.Id = src.Id;
this.IsFullLicence = src.IsFullLicence;
this.IssueDate = src.IssueDate;
this.ExpiryDate = src.ExpiryDate;
this.IssuingAuthority = src.IssuingAuthority;
this.LicenceNumber = src.LicenceNumber;
this.DrivingLicenceCategories = src.DrivingLicenceCategories;
this.DrivingLicencePoints = src.DrivingLicencePoints;
}
}
public class DrivingLicenceCategoryModel
{
[Key]
public int Id { get; set; }
[ForeignKey("DrivingLicence")]
public int DrivingLicenceId { get; set; }
[JsonIgnore]
public DrivingLicenceModel DrivingLicence { get; set; }
}
public class TeamMemberRequestModel
{
public string Id { get; set; }
public virtual DrivingLicenceModel DrivingLicence { get; set; }
}
Context
public class TIERDBContext : IdentityDbContext<TeamMemberModel, RoleModel, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public TIERDBContext() : base("SARDBConnection") { }
public DbSet<DrivingLicenceModel> DrivingLicences { get; set; }
public DbSet<DrivingLicenceCategoryModel> DrivingLicenceCategories { get; set; }
}
Controller
public async Task<IHttpActionResult> Put(string id, TeamMemberRequestModel teamMember)
{
TeamMemberModel CurrentTeamMember = await this.TIERUserManager.FindByIdAsync(id);
CurrentTeamMember.ShallowCopy(teamMember);
await this.TIERUserManager.UpdateAsync(CurrentTeamMember);
}
you have to create clone property into context class
.
In the context clases you could to use clone method that retiran the entity you send by parameters this duplicarse any entity you pass. Sorry for my english
hope you help
After far to many hours working over this. I have come to an answer. The best way to deal with this is to simply deal with it is to add or attach all entities down the tree.
The controller now attaches all children unless they have an ID of 0, therefore new and uses add instead. Then I use this very useful extension I found here http://yassershaikh.com/c-exceptby-extension-method/ to compare lists to see added and deleted entities in the list. While I don't need the added part as the entity will already be marked to an add state as I use add() it does not harm and I want to use it later with add and delete state changing.
Controller
public async Task<IHttpActionResult> Put(string id, TeamMemberRequestModel teamMember)
{
TIERDBContext IdentityContext = (TIERDBContext)this.TIERUserManager.UserStore().Context;
foreach (DrivingLicenceCategoryModel DrivingLicenceCategory in teamMember.DrivingLicence.DrivingLicenceCategories)
{
if (DrivingLicenceCategory.Id == 0)
{
IdentityContext.DrivingLicenceCategories.Add(DrivingLicenceCategory);
}
else
{
IdentityContext.DrivingLicenceCategories.Attach(DrivingLicenceCategory);
}
}
foreach (DrivingLicencePointModel DrivingLicencePoint in teamMember.DrivingLicence.DrivingLicencePoints)
{
if (DrivingLicencePoint.Id == 0)
{
IdentityContext.DrivingLicencePoints.Add(DrivingLicencePoint);
}
else
{
IdentityContext.DrivingLicencePoints.Attach(DrivingLicencePoint);
}
}
this.DetectAddedOrRemoveAndSetEntityState(CurrentTeamMember.DrivingLicence.DrivingLicenceCategories.AsQueryable(),teamMember.DrivingLicence.DrivingLicenceCategories, IdentityContext);
this.DetectAddedOrRemoveAndSetEntityState(CurrentTeamMember.DrivingLicence.DrivingLicencePoints.AsQueryable(),teamMember.DrivingLicence.DrivingLicencePoints, IdentityContext);
CurrentTeamMember.ShallowCopy(teamMember);
await this.TIERUserManager.UpdateAsync(CurrentTeamMember);
}
I then use a generic that uses ExceptBy to work out what is added and delete from the old team member model to the new team member model.
protected void DetectAddedOrRemoveAndSetEntityState<T>(IQueryable<T> old, List<T> current, TIERDBContext context) where T : class, IHasIntID
{
List<T> OldList = old.ToList();
List<T> Added = current.ExceptBy(OldList, x => x.Id).ToList();
List<T> Deleted = OldList.ExceptBy(current, x => x.Id).ToList();
Added.ForEach(x => context.Entry(x).State = EntityState.Added);
Deleted.ForEach(x => context.Entry(x).State = EntityState.Deleted);
}
It works but it is far from great. It takes two DB queries, getting the original and updating. I just cannot think of any better way to do this.

Return ViewModel instead of Model

The following method returns a IEnumerable MODEL called PROF.
Instead of using the model, I want to use a viewModel which contains the same fields as the Model PROF. How can i make changes to the following function to return my VIEWMODEL(which is called MyProfViewModel ) instead of the model (which is called PROF ).
public async Task<ActionResult> Index()
{
var cp= db.PROF.Include(c => c.ACCOUNTS);
return View(await cp.ToListAsync());
}
ViewModel
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes{ get; set; }
}
Model
public class PROF
{
public int ID{ get; set; }
public string DESCRIPTION { get; set; }
}
Joel's answer is pretty much it. But since you mentioned you are starting I will provide a more deltailed answer to make more clear how to use.
First you define a conversion, in this case I put it in the ViewModel, but you can put it somewhere else that would make more sense in your project:
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes { get; set; }
public static MyProfViewModel FromModel(PROF model)
{
var viewModel = new MyProfViewModel()
{
myprofID = model.ID,
myprofDes = model.DESCRIPTION
};
return viewModel;
}
}
Then you just apply the conversion before returning:
public async Task<ActionResult> Index()
{
var cp = PROF.Include(c => c.ACCOUNTS);
var models = await cp.ToListAsync();
var viewModels = models.Select(MyProfViewModel.FromModel);
return View(viewModels);
}
By the way, you can make the tranformation to models as well by adding this to your ViewModel class:
public PROF ToModel()
{
return new PROF()
{
ID = this.myprofID,
DESCRIPTION = this.myprofDes
};
}
Hope this helps!
Add a select statement which maps one type to another:
db.PROF.Include(c => c.ACCOUNTS)
.Select(x=> new MyProfViewModel(){myprofID = x.ID, ...});
Depending on if this select statement is supported by your ORM layer (Entity Framework?), I would do it before, or after .ListAsync();
Pros of before: could lead to better optimized SQL queries.

Based on class that has no key defined mvc5

I try to add view from this controller . I only need this view to show data not for insert or update or delete
public ActionResult Index()
{
var CartObj = ShoppingCart.GetCart(this.HttpContext);
var classshop = new New
{
CartItems = CartObj.GetCartItems(),
CartTotal = CartObj.GetSum()
};
return View(classshop);
}
namespace MusicStore.Models
{
public class ShoppingCart
{
MusicStoreEntities dbo = new MusicStoreEntities();
string ShoppingCartID { get; set; }
public const string CartSessionKey = "CartId";
public static ShoppingCart GetCart(HttpContextBase Context)
{
var cart = new ShoppingCart();
cart.ShoppingCartID = cart.GetCardId(Context);
return cart;
}
public static ShoppingCart GetCart(Controller controller)
{
return GetCart(controller.HttpContext);
}
public List<Cart> GetCartItems()
{
return dbo.Carts.Where(a => a.CartId == ShoppingCartID).ToList();
}
public decimal? GetSum()
{
decimal? Sum = (from items in dbo.Carts
where items.CartId == ShoppingCartID
select (int)items.Count * items.album.Price).Sum();
return Sum ?? decimal.Zero;
}
}
}
and then I got this error:
there was an error running the selected code generator:
'unable to retrieve metadata for 'Musicstore.Model.new'
one or more validation error were detected during model generation
musicstore,models.New :entity type'New' has no key defined .
define the key of entityType
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MusicStore.Models
{
public class New
{
public List<Cart> CartItems { get; set; }
public decimal? CartTotal { get; set; }
}
}
There are two options here. First, if this class is mapped to a table in your database, every model in entity framework requires a primary key. Add this into your model:
[Key]
public int Id { get; set; }
This creates a new property called Id and the [Key] attribute makes it a primary key. Technically you don't need the attribute as EF will pick up Id property and use it as a key, but I prefer to be explicit.
Alternatively, if you don't want this class to be a table in your database, add the NotMapped attribute to the class like this:
[NotMapped]
public class New
{
public List<Cart> CartItems { get; set; }
public decimal? CartTotal { get; set; }
}
I know this is old, but I just ran across this issue.
What happen is when I created a class, CreateEmployeeViewModel, inside the Models folder Visual Studio "smartly" put a line in my DB Context class
public System.Data.Entity.DbSet<eManager.Web.Models.CreateEmployeeViewModel>
CreateEmployeeViewModels { get; set; }
So a table was created on the next update-migration. Removing this line removed the requirement for a key field.
Note: You may also have to add the line AutomaticMigrationDataLossAllowed = true; to your DBMigrationConfiguration Class if the table was created.

MVC3 linq joins

As a novice am trying my hands on MVC3,razor, EF I have Three connected Tables that I want to produce a view from it. In a simpleton's brief the following are about the tables
PJUsers - ID, memUID(this unique Id from membership),FirstName,LastName
PJAwards - user nominates another user for an award, this links with awardtypesID as foreign key ( awardId,bool:awardok)
PJAwartypes - (awardtypeID, awardName)
The query in the controller is like this
var lists =
from tl in db.PJawards
join u in db.PJUsers on tl.nomineeId equals u.ID into tl_u
join i in db.PJUsers on tl.nominatorId equals i.MemUID into tl_i
where tl.awardOk
orderby tl.awardDated ascending
from u in tl_u.DefaultIfEmpty()
from i in tl_i.DefaultIfEmpty()
select new
{
Status = tl.awardOk,
nomineeFname = u.FirstName,
nomineeLname = u.LastName,
award = tl.PJawards.awardName,
Dated = tl.awardDated,
nominatorFname = i.FirstName,
nominatorLname = i.LastName,
nomineeCountry = u.Citizen,
nomineeResidence = u.Residence,
awardtypeId = tl.ID
};
somewhere i read that i have to construct a model class similar to the query in the controller
{
public class AwardUserInfo
{
public AwardUserInfo() { }
public bool Status { get; set; }
public string nomineeFname { get; set; }
public string nomineeLname { get; set; }
public string award { get; set; }
public string Dated { get; set; }
public string nominatorFname { get; set; }
public string nominatorLname { get; set; }
public string nomineeCountry { get; set; }
public string nomineeResidence { get; set; }
public int awardtypeId { get; set; }
}
}
Please I learn by examples so to be able to help me assume I don't know anything
somewhere i read that i have to construct a model class similar to the query in the controller
Try this.
I guess your ef-model is similar to
So You can create a ViewModel class
public class PJAwardsViewModel
{
public int Id { get; set; }
public string NominatorFName { get; set; }
public string NomineeFname { get; set; }
public string AwardName { get; set; }
public bool IsAwarded { get; set; }
}
It will be also good if You add some service class
public class PJAwardsService
{
public static List<PJAwards> GetAll()
{
using (var context = new YourDBEntities())
{
return context.PJAwards
.Include(x => x.PJUsers)
.Include(x => x.PJUsers1)
.Include(x => x.PJAwartypes).ToList();
}
}
}
(Don't forget to write using System.Data.Entity; )
Then You can add a ViewModelHelper class
public class PJAwardsViewModelHelper
{
public static PJAwardsViewModel PopulatePJAwardsViewModel(PJAwards pjaward)
{
return new PJAwardsViewModel
{
Id = pjaward.Id,
NominatorFName = pjaward.PJUsers.FirstName,
NomineeFname = pjaward.PJUsers1.FirstName,
AwardName = pjaward.PJAwartypes.AwardName,
IsAwarded = pjaward.IsAwarded
};
}
public static List<PJAwardsViewModel> PopulatePJAwardsViewModelList(List<PJAwards> pjawardsList)
{
return pjawardsList.Select(x => PopulatePJAwardsViewModel(x)).ToList();
}
}
At the end Your controller index method will look like this
public ActionResult Index()
{
var pjawards = PJAwardsViewModelHelper.PopulatePJAwardsViewModelList(PJAwardsService.GetAll().ToList());
return View(pjawards);
}
The only thing You should do is add a view (build the project before). Choose PJAwardsViewModel as a Model class and List as a scaffold template.
Enjoy it.
Here is a step by step guide by Steven Sanderson on how to use Asp.net MVC3, EF Code First with MVCScaffolding (powershell automation).
http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/
It is a multipart blog post takes you through the exciting journey of MVC3.
All the best.

Categories