In my repository, I get data from my TransportedMaterial table. I also 'include' TransportedMaterialPacking to retrieve all related data in this linked table. In this last table I also would like to retrieve the related MaterialPacking data. I don't know how?
On the picture above we see that the MaterialPacking is null (it has not been filled).
Here are the models:
public class TransportedMaterialPacking
{
public int TransportedMaterialPackingID { get; set; }
public MaterialPacking MaterialPacking { get; set; }
public double Quantity { get; set; }
public double? Width { get; set; }
public double? Height { get; set; }
public double? Length { get; set; }
}
public class MaterialPacking
{
public int MaterialPackingID { get; set; }
public string DescriptionFr { get; set; }
public string DescriptionNl { get; set; }
}
The relations are like this:
TransportedMaterial >> TransportedMaterialPacking >> MaterialPacking
public static class ORMExtensions
{
public static IQueryable<T> MyInclude<T, C>(this IQueryable<T> source, Expression<Func<T, C>> function)
where C : class
where T : class
{
return source.Include(function);
}
I use Entity framework.
Thanks.
You can 'cascade' include by doing something like foo.Include("NavigationProperty.SubNavigationProperty") in your query.
Edit Using property notation to include navigation subproperties is possible, see this for example.
You may be able to do an .include("nav1.nav2") to load related entities
I don't know if .include(x => x.nav1.nav2) will work
Related
I was looking to find a simple way to map a class property to a table column and I found Dapper fluent map which I thought was going to be a great alternative to the considerable boilerplate code that one would need to write simply to map a property to a column. But it does not seem to work.
FluentMapper.Initialize(c =>
{
c.AddMap(new MarginSummaryMap());
});
[Serializable]
[Table("PbCash")]
public class MarginSummary : Entity
{
public long RunId { get; set; }
public DateTime? BusinessDate { get; set; }
[Write(false)]
public DateTime ProcessDate { get; set; }
public string Broker { get; set; }
public string BrokerAccountId { get; set; }
public string Account { get; set; }
public string Folio { get; set; }
public decimal? ActualExcessOrDeficit { get; set; }
[Write(false)]
public decimal? ProjectedExcessOrDeficit { get; set; }
public string Comment { get; set; }
}
public class MarginSummaryMap : EntityMap<MarginSummary>
{
public MarginSummaryMap()
{
Map(c => c.ActualExcessOrDeficit).ToColumn("ExcessOrDeficit");
}
}
Then when I am trying to save data I get an exception that the column ActualExcessOrDeficit does not exist in the table. Well it does not and the mapping is supposed to convert to the actual column ExcessOrDeficit.
Looks like this is a bug in Dapper.FluentMap since the problem goes way if I change type from decimal? to decimal.
Did anyone have this working - I would love to know how to fix this.
I am using Entity Framework 6 in a project and am having trouble creating a query.
Say my classes are defined like:
public class MyContext : DbContext
{
public MyContext(string connectionString) : base(connectionString)
{
}
public DbSet<EntityXXX> XXXSet { get; set; }
public DbSet<EntityYYY> YYYSet { get; set; }
}
public class EntityXXX
{
public string XXXName { get; set; }
public int id { get; set; }
public int YYYid { get; set; }
}
public class EntityYYY
{
public string YYYName { get; set; }
public int id { get; set; }
}
The YYYid property of EntityXXX is the 'id' of the EntityYYY instance that it relates to.
I want to be able to fill a Grid with rows where the first Column is XXXName and the second column is YYYName (from its related EntityYYY), but I can't see how to do this?
I'm sure it's really simple, but I'm new to EF.
You need to put a virtual navigation property on your EntityXXX
public virtual EntityYYY YYY { get; set; }
Then you can do a projection:
db.XXXSet
.Select(x => new { x.XXXName, YYYName = x.YYY.YYYName })
.ToList();
Which will get you the list you need.
I'm trying to get automapper working for .NET core, and I seem to be missing something important.
I have my automapper set up as follows:
In Startup.cs in my ConfigureServices method, I have the following line:
services.AddAutoMapper(typeof(Startup), typeof(ApplicationDbContext));
I also have a class called MappingProfile which resembles this:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<CfGroup, GroupModel>()
.IgnoreAllUnmapped();
CreateMap<GroupModel, CfGroup>()
.IgnoreAllUnmapped();
...
}
}
The IgnoreAllUnmapped was suggested by another SO question, and it looks like this:
public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(
this IMappingExpression<TSource, TDest> expression)
{
expression.ForAllMembers(opt => opt.Ignore());
return expression;
}
My CfGroup looks like this:
[Table("Groups")]
public class CfGroup
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[StringLength(100)]
[Required]
public string Name { get; set; }
[DataType(DataType.ImageUrl)]
[StringLength(250)]
public string ImageUrl { get; set; }
[Required]
public int FounderId { get; set; }
[ForeignKey("FounderId")]
public virtual CfUser Founder { get; set; }
[Required]
public bool IsActive { get; set; }
[Required]
public DateTime CreatedTimestampUtc { get; set; }
[StringLength(3000)]
public string About { get; set; }
public virtual ISet<UserGroupMember> Members { get; set; }
public virtual ISet<UserGroupManager> Managers { get; set; }
}
... and my GroupModel looks like this:
public class GroupModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ImageUrl { get; set; }
public string About { get; set; }
}
When calling my web api endpoint, I get a collection of CfGroup entities from the database just fine. I then run a command like so:
var dtos = entities.Select(_mapper.Map<GroupModel>);
...but my dtos are "empty"... IOW, the instance is created, but Id value is set to 0 and all the string values are set to null. That is definitely not what is in the entities variable.
I have also tried to explicitly convert it like so:
var dtos = entities.Select(x => _mapper.Map<GroupModel>(x));
and, postulating that it's an IEnumerable issue, tried making a List out of it:
var dtos = entities.Select(x => _mapper.Map<GroupModel>(x)).ToList();
...but no matter what, the DTO's remain instantiated, but with all properties set to their default values.
What am I doing wrong?
I suspect it may be because you are mapping inside your linq expression. try something like this:
var dtos = _mapper.Map<IEnumerable<GroupModel>>(entities.ToList());
So it turns out that the IgnoreAllUnmapped was not the correct way to go; in fact, the mapping seemed to work fine without it. I took out the extra "exception handling" code and it worked as I wanted it to. I was overthinking it.
How to: Do nested lists in sqlite-net-extensions
Answer Found: Keeping the question as an example of how to do it.
The problem i encountered was not sqlite-net-extensions related, but i'm keeping the question for context.
[Old Question]
I've got a problem with TwinCoders SQLite-net extensions.
I'm trying to insert a Series object into my database:
I'm using the Db.InsertWithChildren(SelectedSeriesObject,recursive:true) method.
The Series object is added accordingly with it's attributes.
All the Episodes are added as well, no problems there.
The problem is the BaseSeason.
It will only insert one Season object, which is (for some reason) the last Season Object of the list of Seasons in the Series
public class BaseSeries : BaseMedia
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[Indexed]
public int ShowId { get; set; }
public string FirstAirDate { get; set; }
public string LastAirDate { get; set; }
public string Status { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseSeason> Seasons { get; set; }
/// <summary>
/// TvShow = 0, Anime = 1
/// </summary>
public int SeriesType { get; set; }
}
public class BaseSeason
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(BaseSeries))]
public int SeasonId { get; set; }
public int SeasonNumber { get; set; }
public int NumberOfEpisodes { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public string AirDate { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<BaseEpisode> Episodes { get; set; }
[ManyToOne]
public BaseSeries BaseSeries { get; set; }
}
public class BaseEpisode
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(BaseSeason))]
public int EpisodeId { get; set; }
public string Title { get; set; }
public string Plot { get; set; }
public string Poster { get; set; } //still path
public string AirDate { get; set; }
public int EpisodeNumber { get; set; }
public int SeasonNumber { get; set; }
public string SeriesName { get; set; }
[ManyToOne]
public BaseSeason BaseSeason { get; set; }
}
Is there anyone with experience regarding nested relationships in sqlite-net-extensions that knows how to make this work or see what i did wrong?
So regarding writing nested lists in sqlite-net-extensions:
My problem turned out the be related to how I handle the creation of these objects, this is by no means related to sqlite-net extensions. So my bad!
Which means that the questions example is valid and works. (I tested it of course)
Setting up the entities for the database:
The example shown in my question, with a Series class, Season class and Episode class, is the correct way of setting it up.
Inserting into the database:
If you're wondering how to insert an object similar to my Series object (with nested lists), use:
db.InsertWitchChildren(yourObject, recursion: true)
Here's an extended example:
public void AddSeries()
{
MediaDB.db.CreateTable<BaseSeries>();
MediaDB.db.CreateTable<BaseSeason>();
MediaDB.db.CreateTable<BaseEpisode>();
MediaDB.db.InsertWithChildren(SelectedSeries, recursion: true);
}
Side Note:
The example uses a static property on class with the connection string. Like so:
public class MediaDB
{
public static SQLiteConnection db => new SQLiteConnection(new SQLitePlatformGeneric(),"Media.db");
}
Refrain from doing this it is not really the best thing to do, since you should use using for the SQLiteConnection, making sure it's disposed once you're done with it.
more info on: sqlite-net-extentions
[UPDATE]: Further expansion of handling nested lists in sqlite-net extensions:
Deleting tables with children:
This is quite simple, but i spent a good hour and half figuring it out anyways.
Just use:
For lists/arrays: db.DeleteAll(yourCollection, recursion: true)
For single objects: db.Delete(yourObject, true);
As an exmaple: here's my implementation of a method that will delete a List
(BaseSeries is the class shown in the original question question):
public static void RemoveCollection<T>(List<T> collection)
{
using (db)
{
if (typeof(T) == typeof(BaseMovie))
{
db.DeleteAll(collection);
}
if (typeof(T) == typeof(BaseSeries))
{
db.DeleteAll(collection, recursion: true);
}
}
}
The BaseMovie class is a simple single entity, recursion is not needed since it holds no children.
I have to produce an output from 3 separate tables(with a couple of fields from each table) into 1 output. I have a class that represents that output. The data is pulled from linq query of EF 6.1.x ObjectContext(Im stuck with using ObjectContext due to the nature of my clients needs....) entities (the 3 classes properly joined in the query) to a list of the new class (List<>). I populate a grid and all is fine. However the user wants to edit the data in the grid and now I need to push those new changes back.
My question is this: Can I map my new class back to the entities field to field? Or am I stuck with iterating through the collection and updating the tables individually? I thought I could map but I haven't run across anything that substantiates this.
Could you not do this using the "Proxy" pattern?
I've done a 2 entity + Wrapper example pseudo example below.
EF would "Save" the SuperWrapper.DeptProxy and the SuperWrapper.EmpProxy.
public partial class DepartmentEFEntity {
public virtual Guid? DepartmentUUID { get; set; }
public virtual string DepartmentName { get; set; }
public virtual ICollection<EmployeeEFEntity> Employees { get; set; }
}
public partial class EmployeeEFEntity
{
public virtual Guid? ParentDepartmentUUID { get; set; }
public virtual Guid? EmployeeUUID { get; set; }
public virtual DepartmentEFEntity ParentDepartment { get; set; }
public virtual string SSN { get; set; }
}
public class SuperWrapper
{
internal DepartmentEFEntity DeptProxy { get; private set; }
internal EmployeeEFEntity EmpProxy { get; private set; }
public SuperWrapper(DepartmentEFEntity dept, EmployeeEFEntity emp)
{
this.DeptProxy = dept;
this.EmpProxy = emp;
}
public string DepartmentName
{
get { return null == this.DeptProxy ? string.Empty : this.DeptProxy.DepartmentName; }
set { if(null!=this.DeptProxy{this.DeptProxy.DepartmentName =value;}}
}
public string EmployeeSSN
{
get { return null == this.EmpProxy ? string.Empty : this.EmpProxy.SSN; }
set { if(null!=this.EmpProxy{this.EmpProxy.SSN =value;}}
}
}