How do I chain properties with Code First entity framework? - entity-framework

I'm trying to do what seems fairly simple but I'm getting a null reference....
I have a null on the assoc files property in the last statement...
TestInfo.AggregateRoutes.MainBlogEntry = new Blog { BlogType = 1, Title = TestInfo.UniqueRecordIdentifier, Description = TestInfo.UniqueRecordIdentifier, DateAdded = DateTime.Now, User = TestInfo.UniqueRecordIdentifier };
IBlogRepository blogRepo = new BlogRepository();
var assocFile = new AssocFile { Name = TestInfo.UniqueRecordIdentifier, Url = TestInfo.UniqueRecordIdentifier };
TestInfo.AggregateRoutes.MainBlogEntry.AssocFiles.Add(assocFile);
This is the code I have written to support what I'm trying to do...
public class PteDotNetContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<AssocFile> AssocFiles { get; set; }
}
public class Blog
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int BlogId { get; set; }
public int BlogType { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime DateAdded { get; set; }
public string User { get; set; }
public virtual ICollection<AssocFile> AssocFiles { get; set; }
}
public class AssocFile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AssocFileId { get; set; }
public int BlogId { get; set; }
public string Url { get; set; }
public string Name { get; set; }
public virtual Category Category { get; set; }
}
I thought the whole point in declaring it virtual was that it would create a foreign key constraint?

When you instantiate an entity you also need to initialize the collection navigational properties before you access it for the first time. In your case MainBlogEntry.AssocFiles = new List<AssocFile>();. The reason for this is, your property implementation does not contain any logic to initialize the collection.
When EF creates new instances of your entities, it sub classes your entities (ie Proxy Creation) and over ride the default functionality of your properies.
TestInfo.AggregateRoutes.MainBlogEntry = new Blog { BlogType = 1, Title = TestInfo.UniqueRecordIdentifier, Description = TestInfo.UniqueRecordIdentifier, DateAdded = DateTime.Now, User = TestInfo.UniqueRecordIdentifier };
IBlogRepository blogRepo = new BlogRepository();
var assocFile = new AssocFile { Name = TestInfo.UniqueRecordIdentifier, Url = TestInfo.UniqueRecordIdentifier };
TestInfo.AggregateRoutes.MainBlogEntry.AssocFiles = new List<AssocFile>();
TestInfo.AggregateRoutes.MainBlogEntry.AssocFiles.Add(assocFile);

Related

Include object of child object using MongoDB driver .net core

I want to include object in the main collection's child object, I tried below code but it didn't work.
Can someone help?
Here I want to include "Sample" object in "Template" when getting collection of "Instance".
public class Instance
{
public long IId { get; set; }
public string Name { get; set; }
public long TemplateId { get; set; }
public Template Template { get; set; }
}
public class Template
{
public long TId { get; set; }
public string Name { get; set; }
public long SampleId { get; set; }
public Sample Sample { get; set; }
}
public class Sample
{
public long SId { get; set; }
public string Name { get; set; }
}
string connectionString = "mongodb://localhost:27017";
var client = new MongoClient(connectionString);
var db = client.GetDatabase("test");
var instances = db.GetCollection<Instance>("Instances");
var resultOfJoin = instances.Aggregate()
.Lookup("Template", "TemplateId", "TId", #as: "Template")
.Lookup("Sample", "SampleId", "SId", #as: "Template.Sample")
.Unwind("Template")
.Unwind("Template.Sample")
.As<Instance>()
.ToList();
I found the solution by try and error, We need to add a local filed key with the object name as below,
.Lookup("Sample", "Template.SampleId", "SId", #as: "Template.Sample")
var resultOfJoin = instances.Aggregate()
.Lookup("Template", "TemplateId", "TId", #as: "Template")
.Lookup("Sample", "Template.SampleId", "SId", #as: "Template.Sample")
.Unwind("Template")
.Unwind("Template.Sample")
.As<Instance>()
.ToList();

Entity Framework .Core treating foreign key as a primary key

I have the two classes shown below. When trying to insert two HeroEntry with the same IdentityId, the context is only saving one of the two entries. Any ideas what I am doing wrong?
public class Identity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdentityId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
[JsonIgnore]
public virtual HeroEntry HeroEntry { get; set; }
}
public class HeroEntry
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int HeroEntryId { get; set; }
public int IdentityId { get; set; }
public SuperHeroEnum HeroType { get; set; }
public DateTime Startdate { get; set; }
public DateTime EndDate { get; set; }
[ForeignKey("IdentityId")]
public virtual Identity Identity { get; set; }
public virtual List<SuperHeroBreak> SuperHeroBreaks { get; set; }
}
Add code:
var b = new HeroEntry()
{
HeroType = SuperHeroEnum.BATMAN,
Startdate = DateTime.Parse("12/24/2018"),
EndDate = DateTime.Parse("01/04/2019"),
IdentityId = 1,
};
var r = new HeroEntry()
{
HeroType = SuperHeroEnum.ROBIN,
Startdate = DateTime.Parse("12/24/2018"),
EndDate = DateTime.Parse("01/04/2019"),
IdentityId = 1,
};
context.SuperHeroes.AddRange(b, r);
var affected = context.SaveChanges();
Cristian was correct. Adding the navigation property to the identity class solved my issue. Thank you for the help!

Method based linq queries

I have five tables in database whose Entity classes are as follows -
Product
public int ProductId { get; set; }
public string Name { get; set; }
public int Category { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
public virtual ICollection<ProductImage> ProductImages { get; set; }
public virtual ICollection<ProductVariantMapping> ProductVariantMappings
ProductCategory
public int CategoryId { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
ProductImage
public int ProductImageId { get; set; }
public int ProductId { get; set; }
public byte[] Image { get; set; }
public virtual Product Product { get; set; }
ProductVariantMapping
public int MappingId { get; set; }
public int ProductId { get; set; }
public int ProductVariantId { get; set; }
public string Value { get; set; }
public System.Guid GUID { get; set; }
public virtual Product Product { get; set; }
public virtual ProductVariant ProductVariant { get; set; }
ProductVariant
public int ProductVariantId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<ProductVariantMapping> ProductVariantMappings
I want to get Product Details which should include ProductId, ProductName, Category, Description, Brand, Image(Only 1 for now), and Variants*
*Variants would be a list of all the variants of a product. A single variant can be a combination of all the VariantIds with same GUIDs. (VariantName is in ProductVariant table and VariantValue is in ProductVariantMapping table and Price is in inventory table).
So, I used method-based linq for this purpose.
EkartEntities ekartEntities = new EkartEntities();
var productDetails = ekartEntities.Products.Include(p =>
p.ProductVariantMappings).Include(p => p.ProductImages).Include(p =>
p.ProductCategory).Where(p => p.ProductId ==
productDetailDTO.ProductId).ToList();
Now I have to convert my product into a ProductDetailDTO.
ProductDetailDTO
public class ProductDetailDTO
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public byte[] Image { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public List<Variant> Variants { get; set; }
}
public class Variant
{
public string Name { get; set; }
public string Value { get; set; }
public System.Guid Guid { get; set; }
public decimal Price { get; set; }
}
I started doing this like this -
void ToDTO(List<Product> products)
{
EkartEntities ekartEntities = new EkartEntities();
ProductDetailDTO productDetailDTO = new ProductDetailDTO();
foreach (var item in products)
{
productDetailDTO.ProductId = item.ProductId;
productDetailDTO.Name = item.Name;
productDetailDTO.Category = item.ProductCategory.Name;
productDetailDTO.Description = item.Description;
productDetailDTO.Brand = item.Brand;
productDetailDTO.Image = item.ProductImages.ElementAt(0).Image;
foreach (var variant in item.ProductVariantMappings)
{
productDetailDTO.Variants = variant.ProductVariant // ?
}
}
}
I don't know how do I proceed further. How can I extract the variant based on the GUIDs?
The logic of combining of ProductVariant entries with same GUID in mapping table doesn't seem clear from the question, however you can group entries in ProductVariantMappings by GUID and then add any logc you like on group. Here is an example where I take first name and value in a groub of variant with the same GUID:
void ToDTO(List<Product> products)
{
EkartEntities ekartEntities = new EkartEntities();
ProductDetailDTO productDetailDTO = new ProductDetailDTO();
foreach (var item in products)
{
productDetailDTO.ProductId = item.ProductId;
productDetailDTO.Name = item.Name;
productDetailDTO.Category = item.ProductCategory.Name;
productDetailDTO.Description = item.Description;
productDetailDTO.Brand = item.Brand;
productDetailDTO.Image = item.ProductImages.ElementAt(0).Image;
productDetailDTO.Variants = item.ProductVariantMappings
.GroupBy(pm => pm.GUID)
.Select(g => new Variant
{
Guid = g.Key,
// Here should be some logic for getting a name of the combination of Variants
// I just take first
Name = g.FirstOrDefault()?.ProductVariant?.Name,
// Here should be some logic for getting a value of the combination of Variants
// Take first again
Value = g.FirstOrDefault()?.Value,
Price = // need inventory table to compute price
})
.ToList();
}
}
Also note that you need somehow add relation to inventory table, which is not presented in question. Hope it helps.

Entity Framework - DbContext SaveChanges()

Can someone tell me is it possible, and if it is how to avoid using two times _context.SaveChanges() in this code?
Message m = new Message
{
Title = message.Title,
Body = message.Body,
Date = DateTime.Now
};
_context.Messages.Add(m);
_context.SaveChanges();
UserMessage messageToUser = new UserMessage
{
MessageID = m.ID,
ProductID = message.ProductID,
SenderID = message.SenderID,
RecieverID = reciever.Id
};
_context.UserMessages.Add(messageToUser);
_context.SaveChanges();
This is how my Entities look like
public class UserMessage
{
public int ID { get; set; }
public string SenderID { get; set; }
public string RecieverID { get; set; }
public int? ProductID { get; set; }
public int MessageID { get; set; }
public User Sender { get; set; }
public User Reciever { get; set; }
public Product Product { get; set; }
public Message Message { get; set; }
}
public class Message
{
public int ID { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public DateTime Date { get; set; }
}
On your UserMessage class, you set the reference instead of the foreign key, as the foreign key is not known yet.
In your code, that would mean:
Message m = new Message
{
Title = message.Title,
Body = message.Body,
Date = DateTime.Now
};
_context.Messages.Add(m);
UserMessage messageToUser = new UserMessage
{
ProductID = message.ProductID,
SenderID = message.SenderID,
RecieverID = reciever.Id,
Message = m
};
_context.UserMessages.Add(messageToUser);
_context.SaveChanges();

seeding one to many relationship with entity framework

I've looked through quite a few threads about seeding a one to many relationship with EF but can't seem to find the answer to what seems like a simple question. How to do it? I have the following code where I'm trying to create an AuctionItem entity and then add AuctionImage entities to it. But I get a null exception for auctionOne.AuctionImages on the line auctionOne.AuctionImages.Add()... Can anyone tell me what I'm doing wrong? Thanks!
protected override void Seed(AuctionDbContext db)
{
var auctionOne = new AuctionItem()
{
AuctionComplete = string.Empty,
AuctionDate = DateTime.Now.AddDays(-1),
CurrentPrice = 2,
EndPrice = 5,
InitialPrice = 1,
InitialQuantity = 1,
LongDescription = "Long description",
PriceDrops = 2,
QuantityRemaining = 2,
ReserveQuantity = 1,
RetailPrice = 4,
ShortDescription = "Short description",
Title = "Auction one"
};
auctionOne.AuctionImages.Add(new AuctionImage
{
Description = "Beautiful picture",
Filename = "picture.jpg"
});
db.AuctionItems.Add(auctionOne);
base.Seed(db);
}
And here are my classes.
public class AuctionItem
{
[Key]
public int AuctionItemID { get; set; }
[Column(TypeName = "varchar"), MaxLength(256)]
public string Title { get; set; }
public decimal InitialPrice { get; set; }
public int InitialQuantity { get; set; }
public DateTime? AuctionDate { get; set; }
[Column(TypeName = "varchar(max)")]
public string ShortDescription { get; set; }
[Column(TypeName = "varchar(max)")]
public string LongDescription { get; set; }
public decimal RetailPrice { get; set; }
public decimal? EndPrice { get; set; }
public decimal CurrentPrice { get; set; }
public int PriceDrops { get; set; }
public int QuantityRemaining { get; set; }
public int ReserveQuantity { get; set; }
[Column(TypeName = "char"), MaxLength(10)]
public string AuctionComplete { get; set; }
[Column(TypeName = "xml")]
public string Metadata { get; set; }
public virtual ICollection<AuctionImage> AuctionImages { get; set; }
}
public class AuctionImage
{
[Key]
public int AuctionImageID { get; set; }
[ForeignKey("AuctionItem")]
public int AuctionItemID { get; set; }
public virtual AuctionItem AuctionItem { get; set; }
[Column(TypeName = "varchar"), MaxLength(256)]
public string Description { get; set; }
[Column(TypeName = "varchar(max)")]
public string Filename { get; set; }
[Column(TypeName = "xml")]
public string MetaData { get; set; }
}
You haven't allocated the collection for AuctionImages before you are referencing it with the Add, which is why you get the null exception. (When the object is first created, the property will have a null value).
For seeding, it's usually just easiest to do something like:
var auctionOne = new AuctionItem()
{
AuctionComplete = string.Empty,
AuctionDate = DateTime.Now.AddDays(-1),
// ...
AuctionImages = new List<AuctionImage> {
new AuctionImage { Description="", Filename="" },
new AuctionImage { Description="", Filename="" }
}
};
If you want to do it as two separate steps, just allocate the AuctionImages property as a new List<> (or other ICollection) before adding to it:
var auctionOne = new AuctionItem()
{
// ...
Title = "Auction one"
};
auctionOne.AuctionImages = new List<AuctionImage>(); // add this
auctionOne.AuctionImages.Add(new AuctionImage
{
Description = "Beautiful picture",
Filename = "picture.jpg"
});
db.AuctionItems.Add(auctionOne);